Skip to content

Commit 2f655f1

Browse files
authored
Time parameters in the path resulted in code that would not compile (#17021)
This is probably a pretty rare case as it just seems weird to have a time path parameter, but it's good to fix.
1 parent 5693eee commit 2f655f1

10 files changed

Lines changed: 138 additions & 1 deletion

File tree

modules/openapi-generator/src/main/resources/go-server/controller-api.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
169169
}
170170
{{/isInteger}}
171171
{{#isDateTime}}
172-
{{paramName}}Param, err := time.Parse(time.RFC3339, {{#routers}}{{#mux}}params["{{baseName}}"]{{/mux}}{{#chi}}chi.URLParam(r, "{{baseName}}"){{/chi}}{{/routers}})
172+
{{paramName}}Param, err := parseTime({{#routers}}{{#mux}}params["{{baseName}}"]{{/mux}}{{#chi}}chi.URLParam(r, "{{baseName}}"){{/chi}}{{/routers}})
173173
if err != nil {
174174
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
175175
return
@@ -180,6 +180,7 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
180180
{{^isDouble}}
181181
{{^isLong}}
182182
{{^isInteger}}
183+
{{^isDateTime}}
183184
{{^isEnumOrRef}}
184185
{{paramName}}Param := {{#routers}}{{#mux}}params["{{baseName}}"]{{/mux}}{{#chi}}chi.URLParam(r, "{{baseName}}"){{/chi}}{{/routers}}
185186
{{/isEnumOrRef}}
@@ -190,6 +191,7 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
190191
return
191192
}
192193
{{/isEnumOrRef}}
194+
{{/isDateTime}}
193195
{{/isInteger}}
194196
{{/isLong}}
195197
{{/isDouble}}

modules/openapi-generator/src/test/resources/3_0/go-server/petstore.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,26 @@ paths:
745745
application/json:
746746
schema:
747747
$ref: '#/components/schemas/ApiResponse'
748+
/pets/byTime/{createdTime}:
749+
get:
750+
tags:
751+
- pet
752+
summary: Get the pets by time
753+
operationId: GetPetsByTime
754+
parameters:
755+
- in: path
756+
name: createdTime
757+
required: true
758+
schema:
759+
type: string
760+
format: date-time
761+
responses:
762+
'200':
763+
description: successful operation
764+
content:
765+
application/json:
766+
schema:
767+
$ref: '#/components/schemas/ApiResponse'
748768
externalDocs:
749769
description: Find out more about Swagger
750770
url: 'http://swagger.io'

samples/server/petstore/go-api-server/api/openapi.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,28 @@ paths:
761761
summary: Get the pets by only using boolean query parameters
762762
tags:
763763
- pet
764+
/pets/byTime/{createdTime}:
765+
get:
766+
operationId: GetPetsByTime
767+
parameters:
768+
- explode: false
769+
in: path
770+
name: createdTime
771+
required: true
772+
schema:
773+
format: date-time
774+
type: string
775+
style: simple
776+
responses:
777+
"200":
778+
content:
779+
application/json:
780+
schema:
781+
$ref: '#/components/schemas/ApiResponse'
782+
description: successful operation
783+
summary: Get the pets by time
784+
tags:
785+
- pet
764786
components:
765787
requestBodies:
766788
UserArray:

samples/server/petstore/go-api-server/go/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type PetAPIRouter interface {
3030
FindPetsByTags(http.ResponseWriter, *http.Request)
3131
GetPetById(http.ResponseWriter, *http.Request)
3232
GetPetImageById(http.ResponseWriter, *http.Request)
33+
GetPetsByTime(http.ResponseWriter, *http.Request)
3334
GetPetsUsingBooleanQueryParameters(http.ResponseWriter, *http.Request)
3435
UpdatePet(http.ResponseWriter, *http.Request)
3536
UpdatePetWithForm(http.ResponseWriter, *http.Request)
@@ -73,6 +74,7 @@ type PetAPIServicer interface {
7374
FindPetsByTags(context.Context, []string, time.Time, time.Time) (ImplResponse, error)
7475
GetPetById(context.Context, int64) (ImplResponse, error)
7576
GetPetImageById(context.Context, int64) (ImplResponse, error)
77+
GetPetsByTime(context.Context, time.Time) (ImplResponse, error)
7678
GetPetsUsingBooleanQueryParameters(context.Context, bool, bool, bool) (ImplResponse, error)
7779
UpdatePet(context.Context, Pet) (ImplResponse, error)
7880
UpdatePetWithForm(context.Context, int64, string, string) (ImplResponse, error)

samples/server/petstore/go-api-server/go/api_pet.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ func (c *PetAPIController) Routes() Routes {
8585
"/v2/pet/{petId}/uploadImage",
8686
c.GetPetImageById,
8787
},
88+
"GetPetsByTime": Route{
89+
strings.ToUpper("Get"),
90+
"/v2/pets/byTime/{createdTime}",
91+
c.GetPetsByTime,
92+
},
8893
"GetPetsUsingBooleanQueryParameters": Route{
8994
strings.ToUpper("Get"),
9095
"/v2/pets/boolean/parsing",
@@ -294,6 +299,24 @@ func (c *PetAPIController) GetPetImageById(w http.ResponseWriter, r *http.Reques
294299
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
295300
}
296301

302+
// GetPetsByTime - Get the pets by time
303+
func (c *PetAPIController) GetPetsByTime(w http.ResponseWriter, r *http.Request) {
304+
params := mux.Vars(r)
305+
createdTimeParam, err := parseTime(params["createdTime"])
306+
if err != nil {
307+
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
308+
return
309+
}
310+
result, err := c.service.GetPetsByTime(r.Context(), createdTimeParam)
311+
// If an error occurred, encode the error with the status code
312+
if err != nil {
313+
c.errorHandler(w, r, err, &result)
314+
return
315+
}
316+
// If no error, encode the body and the result code
317+
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
318+
}
319+
297320
// GetPetsUsingBooleanQueryParameters - Get the pets by only using boolean query parameters
298321
func (c *PetAPIController) GetPetsUsingBooleanQueryParameters(w http.ResponseWriter, r *http.Request) {
299322
query := r.URL.Query()

samples/server/petstore/go-api-server/go/api_pet_service.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ func (s *PetAPIService) GetPetImageById(ctx context.Context, petId int64) (ImplR
130130
return Response(http.StatusNotImplemented, nil), errors.New("GetPetImageById method not implemented")
131131
}
132132

133+
// GetPetsByTime - Get the pets by time
134+
func (s *PetAPIService) GetPetsByTime(ctx context.Context, createdTime time.Time) (ImplResponse, error) {
135+
// TODO - update GetPetsByTime with the required logic for this service method.
136+
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.
137+
138+
// TODO: Uncomment the next line to return response Response(200, ApiResponse{}) or use other options such as http.Ok ...
139+
// return Response(200, ApiResponse{}), nil
140+
141+
return Response(http.StatusNotImplemented, nil), errors.New("GetPetsByTime method not implemented")
142+
}
143+
133144
// GetPetsUsingBooleanQueryParameters - Get the pets by only using boolean query parameters
134145
func (s *PetAPIService) GetPetsUsingBooleanQueryParameters(ctx context.Context, expr bool, grouping bool, inactive bool) (ImplResponse, error) {
135146
// TODO - update GetPetsUsingBooleanQueryParameters with the required logic for this service method.

samples/server/petstore/go-chi-server/api/openapi.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,28 @@ paths:
761761
summary: Get the pets by only using boolean query parameters
762762
tags:
763763
- pet
764+
/pets/byTime/{createdTime}:
765+
get:
766+
operationId: GetPetsByTime
767+
parameters:
768+
- explode: false
769+
in: path
770+
name: createdTime
771+
required: true
772+
schema:
773+
format: date-time
774+
type: string
775+
style: simple
776+
responses:
777+
"200":
778+
content:
779+
application/json:
780+
schema:
781+
$ref: '#/components/schemas/ApiResponse'
782+
description: successful operation
783+
summary: Get the pets by time
784+
tags:
785+
- pet
764786
components:
765787
requestBodies:
766788
UserArray:

samples/server/petstore/go-chi-server/go/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type PetAPIRouter interface {
3030
FindPetsByTags(http.ResponseWriter, *http.Request)
3131
GetPetById(http.ResponseWriter, *http.Request)
3232
GetPetImageById(http.ResponseWriter, *http.Request)
33+
GetPetsByTime(http.ResponseWriter, *http.Request)
3334
GetPetsUsingBooleanQueryParameters(http.ResponseWriter, *http.Request)
3435
UpdatePet(http.ResponseWriter, *http.Request)
3536
UpdatePetWithForm(http.ResponseWriter, *http.Request)
@@ -73,6 +74,7 @@ type PetAPIServicer interface {
7374
FindPetsByTags(context.Context, []string, time.Time, time.Time) (ImplResponse, error)
7475
GetPetById(context.Context, int64) (ImplResponse, error)
7576
GetPetImageById(context.Context, int64) (ImplResponse, error)
77+
GetPetsByTime(context.Context, time.Time) (ImplResponse, error)
7678
GetPetsUsingBooleanQueryParameters(context.Context, bool, bool, bool) (ImplResponse, error)
7779
UpdatePet(context.Context, Pet) (ImplResponse, error)
7880
UpdatePetWithForm(context.Context, int64, string, string) (ImplResponse, error)

samples/server/petstore/go-chi-server/go/api_pet.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ func (c *PetAPIController) Routes() Routes {
8585
"/v2/pet/{petId}/uploadImage",
8686
c.GetPetImageById,
8787
},
88+
"GetPetsByTime": Route{
89+
strings.ToUpper("Get"),
90+
"/v2/pets/byTime/{createdTime}",
91+
c.GetPetsByTime,
92+
},
8893
"GetPetsUsingBooleanQueryParameters": Route{
8994
strings.ToUpper("Get"),
9095
"/v2/pets/boolean/parsing",
@@ -290,6 +295,23 @@ func (c *PetAPIController) GetPetImageById(w http.ResponseWriter, r *http.Reques
290295
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
291296
}
292297

298+
// GetPetsByTime - Get the pets by time
299+
func (c *PetAPIController) GetPetsByTime(w http.ResponseWriter, r *http.Request) {
300+
createdTimeParam, err := parseTime(chi.URLParam(r, "createdTime"))
301+
if err != nil {
302+
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
303+
return
304+
}
305+
result, err := c.service.GetPetsByTime(r.Context(), createdTimeParam)
306+
// If an error occurred, encode the error with the status code
307+
if err != nil {
308+
c.errorHandler(w, r, err, &result)
309+
return
310+
}
311+
// If no error, encode the body and the result code
312+
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
313+
}
314+
293315
// GetPetsUsingBooleanQueryParameters - Get the pets by only using boolean query parameters
294316
func (c *PetAPIController) GetPetsUsingBooleanQueryParameters(w http.ResponseWriter, r *http.Request) {
295317
query := r.URL.Query()

samples/server/petstore/go-chi-server/go/api_pet_service.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ func (s *PetAPIService) GetPetImageById(ctx context.Context, petId int64) (ImplR
130130
return Response(http.StatusNotImplemented, nil), errors.New("GetPetImageById method not implemented")
131131
}
132132

133+
// GetPetsByTime - Get the pets by time
134+
func (s *PetAPIService) GetPetsByTime(ctx context.Context, createdTime time.Time) (ImplResponse, error) {
135+
// TODO - update GetPetsByTime with the required logic for this service method.
136+
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.
137+
138+
// TODO: Uncomment the next line to return response Response(200, ApiResponse{}) or use other options such as http.Ok ...
139+
// return Response(200, ApiResponse{}), nil
140+
141+
return Response(http.StatusNotImplemented, nil), errors.New("GetPetsByTime method not implemented")
142+
}
143+
133144
// GetPetsUsingBooleanQueryParameters - Get the pets by only using boolean query parameters
134145
func (s *PetAPIService) GetPetsUsingBooleanQueryParameters(ctx context.Context, expr bool, grouping bool, inactive bool) (ImplResponse, error) {
135146
// TODO - update GetPetsUsingBooleanQueryParameters with the required logic for this service method.

0 commit comments

Comments
 (0)