Skip to content

Commit 291ce35

Browse files
authored
[Go][Server] FormParams - Generic Array Type Handling (#17001)
* If a form param is an array and isn't caught in the previous checks, treat it as a slice of strings. * Add an example of a FormParam that is an array
1 parent e950707 commit 291ce35

10 files changed

Lines changed: 32 additions & 6 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,12 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
495495
{{/isArray}}{{/isInteger}}
496496
{{^isFile}}
497497
{{^isLong}}
498+
{{#isArray}}
499+
{{paramName}}Param := strings.Split(r.FormValue("{{baseName}}"), ",")
500+
{{/isArray}}
501+
{{^isArray}}
498502
{{paramName}}Param := r.FormValue("{{baseName}}")
503+
{{/isArray}}
499504
{{/isLong}}
500505
{{/isFile}}
501506
{{/isFormParam}}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ paths:
346346
additionalMetadata:
347347
description: Additional data to pass to server
348348
type: string
349+
extraOptionalMetadata:
350+
description: More data to pass to server
351+
type: array
352+
items:
353+
type: string
349354
file:
350355
description: file to upload
351356
type: string

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,11 @@ components:
11941194
additionalMetadata:
11951195
description: Additional data to pass to server
11961196
type: string
1197+
extraOptionalMetadata:
1198+
description: More data to pass to server
1199+
items:
1200+
type: string
1201+
type: array
11971202
file:
11981203
description: file to upload
11991204
format: binary

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type PetAPIServicer interface {
7676
GetPetsUsingBooleanQueryParameters(context.Context, bool, bool, bool) (ImplResponse, error)
7777
UpdatePet(context.Context, Pet) (ImplResponse, error)
7878
UpdatePetWithForm(context.Context, int64, string, string) (ImplResponse, error)
79-
UploadFile(context.Context, int64, string, *os.File) (ImplResponse, error)
79+
UploadFile(context.Context, int64, string, []string, *os.File) (ImplResponse, error)
8080
UploadFileArrayOfFiles(context.Context, int64, string, []*os.File) (ImplResponse, error)
8181
}
8282

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,17 @@ func (c *PetAPIController) UploadFile(w http.ResponseWriter, r *http.Request) {
407407

408408

409409
additionalMetadataParam := r.FormValue("additionalMetadata")
410+
411+
412+
extraOptionalMetadataParam := strings.Split(r.FormValue("extraOptionalMetadata"), ",")
410413
fileParam, err := ReadFormFileToTempFile(r, "file")
411414
if err != nil {
412415
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
413416
return
414417
}
415418

416419

417-
result, err := c.service.UploadFile(r.Context(), petIdParam, additionalMetadataParam, fileParam)
420+
result, err := c.service.UploadFile(r.Context(), petIdParam, additionalMetadataParam, extraOptionalMetadataParam, fileParam)
418421
// If an error occurred, encode the error with the status code
419422
if err != nil {
420423
c.errorHandler(w, r, err, &result)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (s *PetAPIService) UpdatePetWithForm(ctx context.Context, petId int64, name
173173
}
174174

175175
// UploadFile - uploads an image
176-
func (s *PetAPIService) UploadFile(ctx context.Context, petId int64, additionalMetadata string, file *os.File) (ImplResponse, error) {
176+
func (s *PetAPIService) UploadFile(ctx context.Context, petId int64, additionalMetadata string, extraOptionalMetadata []string, file *os.File) (ImplResponse, error) {
177177
// TODO - update UploadFile with the required logic for this service method.
178178
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.
179179

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,11 @@ components:
11941194
additionalMetadata:
11951195
description: Additional data to pass to server
11961196
type: string
1197+
extraOptionalMetadata:
1198+
description: More data to pass to server
1199+
items:
1200+
type: string
1201+
type: array
11971202
file:
11981203
description: file to upload
11991204
format: binary

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type PetAPIServicer interface {
7676
GetPetsUsingBooleanQueryParameters(context.Context, bool, bool, bool) (ImplResponse, error)
7777
UpdatePet(context.Context, Pet) (ImplResponse, error)
7878
UpdatePetWithForm(context.Context, int64, string, string) (ImplResponse, error)
79-
UploadFile(context.Context, int64, string, *os.File) (ImplResponse, error)
79+
UploadFile(context.Context, int64, string, []string, *os.File) (ImplResponse, error)
8080
UploadFileArrayOfFiles(context.Context, int64, string, []*os.File) (ImplResponse, error)
8181
}
8282

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,17 @@ func (c *PetAPIController) UploadFile(w http.ResponseWriter, r *http.Request) {
401401

402402

403403
additionalMetadataParam := r.FormValue("additionalMetadata")
404+
405+
406+
extraOptionalMetadataParam := strings.Split(r.FormValue("extraOptionalMetadata"), ",")
404407
fileParam, err := ReadFormFileToTempFile(r, "file")
405408
if err != nil {
406409
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
407410
return
408411
}
409412

410413

411-
result, err := c.service.UploadFile(r.Context(), petIdParam, additionalMetadataParam, fileParam)
414+
result, err := c.service.UploadFile(r.Context(), petIdParam, additionalMetadataParam, extraOptionalMetadataParam, fileParam)
412415
// If an error occurred, encode the error with the status code
413416
if err != nil {
414417
c.errorHandler(w, r, err, &result)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (s *PetAPIService) UpdatePetWithForm(ctx context.Context, petId int64, name
173173
}
174174

175175
// UploadFile - uploads an image
176-
func (s *PetAPIService) UploadFile(ctx context.Context, petId int64, additionalMetadata string, file *os.File) (ImplResponse, error) {
176+
func (s *PetAPIService) UploadFile(ctx context.Context, petId int64, additionalMetadata string, extraOptionalMetadata []string, file *os.File) (ImplResponse, error) {
177177
// TODO - update UploadFile with the required logic for this service method.
178178
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.
179179

0 commit comments

Comments
 (0)