From d669624eca9f2bd2ca06f15e047022a73966b018 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 12 Oct 2023 19:23:02 +0800 Subject: [PATCH] [go-server] Add tests, minor format change (#16805) * add tests for #16787, minor format change * revert * fix typo * use pet instead of fake * update samples --- .../go-server/controller-api.mustache | 14 +++--- .../resources/3_0/go-server/petstore.yaml | 41 ++++++++++++++++ .../petstore/go/go-petstore/go/api_pet.go | 7 ++- .../petstore/go-api-server/api/openapi.yaml | 45 ++++++++++++++++++ .../server/petstore/go-api-server/go/api.go | 2 + .../petstore/go-api-server/go/api_pet.go | 47 +++++++++++++++++-- .../go-api-server/go/api_pet_service.go | 11 +++++ .../petstore/go-chi-server/api/openapi.yaml | 45 ++++++++++++++++++ .../server/petstore/go-chi-server/go/api.go | 2 + .../petstore/go-chi-server/go/api_pet.go | 46 ++++++++++++++++-- .../go-chi-server/go/api_pet_service.go | 11 +++++ 11 files changed, 253 insertions(+), 18 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go-server/controller-api.mustache b/modules/openapi-generator/src/main/resources/go-server/controller-api.mustache index 5012d68d1287..55ca127692b8 100644 --- a/modules/openapi-generator/src/main/resources/go-server/controller-api.mustache +++ b/modules/openapi-generator/src/main/resources/go-server/controller-api.mustache @@ -3,12 +3,12 @@ package {{packageName}} import ( "encoding/json" - {{#isBodyParam}} - {{^required}} + {{#isBodyParam}} + {{^required}} "errors" "io" - {{/required}} - {{/isBodyParam}} + {{/required}} + {{/isBodyParam}} "net/http" "strings" @@ -361,9 +361,11 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re {{/isNumber}} {{/isQueryParam}} {{#isFormParam}} - {{#isFile}}{{#isArray}} + {{#isFile}} + {{#isArray}} {{paramName}}Param, err := ReadFormFilesToTempFiles(r, "{{baseName}}") - {{/isArray}}{{^isArray}} + {{/isArray}} + {{^isArray}} {{paramName}}Param, err := ReadFormFileToTempFile(r, "{{baseName}}") {{/isArray}} if err != nil { diff --git a/modules/openapi-generator/src/test/resources/3_0/go-server/petstore.yaml b/modules/openapi-generator/src/test/resources/3_0/go-server/petstore.yaml index fce2c52cbde4..3a3af8e98a8f 100644 --- a/modules/openapi-generator/src/test/resources/3_0/go-server/petstore.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/go-server/petstore.yaml @@ -579,6 +579,47 @@ paths: description: User not found security: - api_key: [] + '/fake/uploadImage/array of_file': + post: + tags: + - pet + summary: uploads images (array of files) + description: '' + operationId: uploadFileArrayOfFiles + parameters: + - name: petId + in: path + description: ID of pet to update + required: true + schema: + type: integer + format: int64 + responses: + '200': + description: successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/ApiResponse' + security: + - petstore_auth: + - 'write:pets' + - 'read:pets' + requestBody: + content: + multipart/form-data: + schema: + type: object + properties: + additionalMetadata: + description: Additional data to pass to server + type: string + files: + description: files to upload + type: array + items: + type: string + format: binary externalDocs: description: Find out more about Swagger url: 'http://swagger.io' diff --git a/samples/openapi3/server/petstore/go/go-petstore/go/api_pet.go b/samples/openapi3/server/petstore/go/go-petstore/go/api_pet.go index 0efd81a77e9f..612040bb79c2 100644 --- a/samples/openapi3/server/petstore/go/go-petstore/go/api_pet.go +++ b/samples/openapi3/server/petstore/go/go-petstore/go/api_pet.go @@ -231,10 +231,10 @@ func (c *PetAPIController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ c.errorHandler(w, r, &ParsingError{Err: err}, nil) return } - + nameParam := r.FormValue("name") - + statusParam := r.FormValue("status") result, err := c.service.UpdatePetWithForm(r.Context(), petIdParam, nameParam, statusParam) @@ -261,10 +261,9 @@ func (c *PetAPIController) UploadFile(w http.ResponseWriter, r *http.Request) { c.errorHandler(w, r, &ParsingError{Err: err}, nil) return } - - additionalMetadataParam := r.FormValue("additionalMetadata") + additionalMetadataParam := r.FormValue("additionalMetadata") fileParam, err := ReadFormFileToTempFile(r, "file") if err != nil { c.errorHandler(w, r, &ParsingError{Err: err}, nil) diff --git a/samples/server/petstore/go-api-server/api/openapi.yaml b/samples/server/petstore/go-api-server/api/openapi.yaml index cc433a6afd59..0402307cbaf8 100644 --- a/samples/server/petstore/go-api-server/api/openapi.yaml +++ b/samples/server/petstore/go-api-server/api/openapi.yaml @@ -598,6 +598,39 @@ paths: summary: Updated user tags: - user + /fake/uploadImage/array of_file: + post: + description: "" + operationId: uploadFileArrayOfFiles + parameters: + - description: ID of pet to update + explode: false + in: path + name: petId + required: true + schema: + format: int64 + type: integer + style: simple + requestBody: + content: + multipart/form-data: + schema: + $ref: '#/components/schemas/uploadFileArrayOfFiles_request' + responses: + "200": + content: + application/json: + schema: + $ref: '#/components/schemas/ApiResponse' + description: successful operation + security: + - petstore_auth: + - write:pets + - read:pets + summary: uploads images (array of files) + tags: + - pet components: requestBodies: UserArray: @@ -1022,6 +1055,18 @@ components: format: binary type: string type: object + uploadFileArrayOfFiles_request: + properties: + additionalMetadata: + description: Additional data to pass to server + type: string + files: + description: files to upload + items: + format: binary + type: string + type: array + type: object an_Object: description: An array 3-deep. example: diff --git a/samples/server/petstore/go-api-server/go/api.go b/samples/server/petstore/go-api-server/go/api.go index 0f6f14dea75f..9edc08f180fe 100644 --- a/samples/server/petstore/go-api-server/go/api.go +++ b/samples/server/petstore/go-api-server/go/api.go @@ -30,6 +30,7 @@ type PetAPIRouter interface { UpdatePet(http.ResponseWriter, *http.Request) UpdatePetWithForm(http.ResponseWriter, *http.Request) UploadFile(http.ResponseWriter, *http.Request) + UploadFileArrayOfFiles(http.ResponseWriter, *http.Request) } // StoreAPIRouter defines the required methods for binding the api requests to a responses for the StoreAPI // The StoreAPIRouter implementation should parse necessary information from the http request, @@ -69,6 +70,7 @@ type PetAPIServicer interface { UpdatePet(context.Context, Pet) (ImplResponse, error) UpdatePetWithForm(context.Context, int64, string, string) (ImplResponse, error) UploadFile(context.Context, int64, string, *os.File) (ImplResponse, error) + UploadFileArrayOfFiles(context.Context, int64, string, []*os.File) (ImplResponse, error) } diff --git a/samples/server/petstore/go-api-server/go/api_pet.go b/samples/server/petstore/go-api-server/go/api_pet.go index 0161ba41d6cf..87338f89b27c 100644 --- a/samples/server/petstore/go-api-server/go/api_pet.go +++ b/samples/server/petstore/go-api-server/go/api_pet.go @@ -90,6 +90,11 @@ func (c *PetAPIController) Routes() Routes { "/v2/pet/{petId}/uploadImage", c.UploadFile, }, + "UploadFileArrayOfFiles": Route{ + strings.ToUpper("Post"), + "/v2/fake/uploadImage/array of_file", + c.UploadFileArrayOfFiles, + }, } } @@ -234,10 +239,10 @@ func (c *PetAPIController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ c.errorHandler(w, r, &ParsingError{Err: err}, nil) return } - + nameParam := r.FormValue("name") - + statusParam := r.FormValue("status") result, err := c.service.UpdatePetWithForm(r.Context(), petIdParam, nameParam, statusParam) @@ -265,10 +270,9 @@ func (c *PetAPIController) UploadFile(w http.ResponseWriter, r *http.Request) { c.errorHandler(w, r, &ParsingError{Err: err}, nil) return } - - additionalMetadataParam := r.FormValue("additionalMetadata") + additionalMetadataParam := r.FormValue("additionalMetadata") fileParam, err := ReadFormFileToTempFile(r, "file") if err != nil { c.errorHandler(w, r, &ParsingError{Err: err}, nil) @@ -285,3 +289,38 @@ func (c *PetAPIController) UploadFile(w http.ResponseWriter, r *http.Request) { // If no error, encode the body and the result code EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) } + +// UploadFileArrayOfFiles - uploads images (array of files) +func (c *PetAPIController) UploadFileArrayOfFiles(w http.ResponseWriter, r *http.Request) { + if err := r.ParseMultipartForm(32 << 20); err != nil { + c.errorHandler(w, r, &ParsingError{Err: err}, nil) + return + } + params := mux.Vars(r) + petIdParam, err := parseNumericParameter[int64]( + params["petId"], + WithRequire[int64](parseInt64), + ) + if err != nil { + c.errorHandler(w, r, &ParsingError{Err: err}, nil) + return + } + + + additionalMetadataParam := r.FormValue("additionalMetadata") + filesParam, err := ReadFormFilesToTempFiles(r, "files") + if err != nil { + c.errorHandler(w, r, &ParsingError{Err: err}, nil) + return + } + + + result, err := c.service.UploadFileArrayOfFiles(r.Context(), petIdParam, additionalMetadataParam, filesParam) + // If an error occurred, encode the error with the status code + if err != nil { + c.errorHandler(w, r, err, &result) + return + } + // If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) +} diff --git a/samples/server/petstore/go-api-server/go/api_pet_service.go b/samples/server/petstore/go-api-server/go/api_pet_service.go index bda4cd897526..d4470e51e9ae 100644 --- a/samples/server/petstore/go-api-server/go/api_pet_service.go +++ b/samples/server/petstore/go-api-server/go/api_pet_service.go @@ -139,3 +139,14 @@ func (s *PetAPIService) UploadFile(ctx context.Context, petId int64, additionalM return Response(http.StatusNotImplemented, nil), errors.New("UploadFile method not implemented") } + +// UploadFileArrayOfFiles - uploads images (array of files) +func (s *PetAPIService) UploadFileArrayOfFiles(ctx context.Context, petId int64, additionalMetadata string, files []*os.File) (ImplResponse, error) { + // TODO - update UploadFileArrayOfFiles with the required logic for this service method. + // Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. + + // TODO: Uncomment the next line to return response Response(200, ApiResponse{}) or use other options such as http.Ok ... + // return Response(200, ApiResponse{}), nil + + return Response(http.StatusNotImplemented, nil), errors.New("UploadFileArrayOfFiles method not implemented") +} diff --git a/samples/server/petstore/go-chi-server/api/openapi.yaml b/samples/server/petstore/go-chi-server/api/openapi.yaml index cc433a6afd59..0402307cbaf8 100644 --- a/samples/server/petstore/go-chi-server/api/openapi.yaml +++ b/samples/server/petstore/go-chi-server/api/openapi.yaml @@ -598,6 +598,39 @@ paths: summary: Updated user tags: - user + /fake/uploadImage/array of_file: + post: + description: "" + operationId: uploadFileArrayOfFiles + parameters: + - description: ID of pet to update + explode: false + in: path + name: petId + required: true + schema: + format: int64 + type: integer + style: simple + requestBody: + content: + multipart/form-data: + schema: + $ref: '#/components/schemas/uploadFileArrayOfFiles_request' + responses: + "200": + content: + application/json: + schema: + $ref: '#/components/schemas/ApiResponse' + description: successful operation + security: + - petstore_auth: + - write:pets + - read:pets + summary: uploads images (array of files) + tags: + - pet components: requestBodies: UserArray: @@ -1022,6 +1055,18 @@ components: format: binary type: string type: object + uploadFileArrayOfFiles_request: + properties: + additionalMetadata: + description: Additional data to pass to server + type: string + files: + description: files to upload + items: + format: binary + type: string + type: array + type: object an_Object: description: An array 3-deep. example: diff --git a/samples/server/petstore/go-chi-server/go/api.go b/samples/server/petstore/go-chi-server/go/api.go index 0f6f14dea75f..9edc08f180fe 100644 --- a/samples/server/petstore/go-chi-server/go/api.go +++ b/samples/server/petstore/go-chi-server/go/api.go @@ -30,6 +30,7 @@ type PetAPIRouter interface { UpdatePet(http.ResponseWriter, *http.Request) UpdatePetWithForm(http.ResponseWriter, *http.Request) UploadFile(http.ResponseWriter, *http.Request) + UploadFileArrayOfFiles(http.ResponseWriter, *http.Request) } // StoreAPIRouter defines the required methods for binding the api requests to a responses for the StoreAPI // The StoreAPIRouter implementation should parse necessary information from the http request, @@ -69,6 +70,7 @@ type PetAPIServicer interface { UpdatePet(context.Context, Pet) (ImplResponse, error) UpdatePetWithForm(context.Context, int64, string, string) (ImplResponse, error) UploadFile(context.Context, int64, string, *os.File) (ImplResponse, error) + UploadFileArrayOfFiles(context.Context, int64, string, []*os.File) (ImplResponse, error) } diff --git a/samples/server/petstore/go-chi-server/go/api_pet.go b/samples/server/petstore/go-chi-server/go/api_pet.go index 0efd81a77e9f..43f67846262c 100644 --- a/samples/server/petstore/go-chi-server/go/api_pet.go +++ b/samples/server/petstore/go-chi-server/go/api_pet.go @@ -90,6 +90,11 @@ func (c *PetAPIController) Routes() Routes { "/v2/pet/{petId}/uploadImage", c.UploadFile, }, + "UploadFileArrayOfFiles": Route{ + strings.ToUpper("Post"), + "/v2/fake/uploadImage/array of_file", + c.UploadFileArrayOfFiles, + }, } } @@ -231,10 +236,10 @@ func (c *PetAPIController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ c.errorHandler(w, r, &ParsingError{Err: err}, nil) return } - + nameParam := r.FormValue("name") - + statusParam := r.FormValue("status") result, err := c.service.UpdatePetWithForm(r.Context(), petIdParam, nameParam, statusParam) @@ -261,10 +266,9 @@ func (c *PetAPIController) UploadFile(w http.ResponseWriter, r *http.Request) { c.errorHandler(w, r, &ParsingError{Err: err}, nil) return } - - additionalMetadataParam := r.FormValue("additionalMetadata") + additionalMetadataParam := r.FormValue("additionalMetadata") fileParam, err := ReadFormFileToTempFile(r, "file") if err != nil { c.errorHandler(w, r, &ParsingError{Err: err}, nil) @@ -281,3 +285,37 @@ func (c *PetAPIController) UploadFile(w http.ResponseWriter, r *http.Request) { // If no error, encode the body and the result code EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) } + +// UploadFileArrayOfFiles - uploads images (array of files) +func (c *PetAPIController) UploadFileArrayOfFiles(w http.ResponseWriter, r *http.Request) { + if err := r.ParseMultipartForm(32 << 20); err != nil { + c.errorHandler(w, r, &ParsingError{Err: err}, nil) + return + } + petIdParam, err := parseNumericParameter[int64]( + chi.URLParam(r, "petId"), + WithRequire[int64](parseInt64), + ) + if err != nil { + c.errorHandler(w, r, &ParsingError{Err: err}, nil) + return + } + + + additionalMetadataParam := r.FormValue("additionalMetadata") + filesParam, err := ReadFormFilesToTempFiles(r, "files") + if err != nil { + c.errorHandler(w, r, &ParsingError{Err: err}, nil) + return + } + + + result, err := c.service.UploadFileArrayOfFiles(r.Context(), petIdParam, additionalMetadataParam, filesParam) + // If an error occurred, encode the error with the status code + if err != nil { + c.errorHandler(w, r, err, &result) + return + } + // If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) +} diff --git a/samples/server/petstore/go-chi-server/go/api_pet_service.go b/samples/server/petstore/go-chi-server/go/api_pet_service.go index bda4cd897526..d4470e51e9ae 100644 --- a/samples/server/petstore/go-chi-server/go/api_pet_service.go +++ b/samples/server/petstore/go-chi-server/go/api_pet_service.go @@ -139,3 +139,14 @@ func (s *PetAPIService) UploadFile(ctx context.Context, petId int64, additionalM return Response(http.StatusNotImplemented, nil), errors.New("UploadFile method not implemented") } + +// UploadFileArrayOfFiles - uploads images (array of files) +func (s *PetAPIService) UploadFileArrayOfFiles(ctx context.Context, petId int64, additionalMetadata string, files []*os.File) (ImplResponse, error) { + // TODO - update UploadFileArrayOfFiles with the required logic for this service method. + // Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. + + // TODO: Uncomment the next line to return response Response(200, ApiResponse{}) or use other options such as http.Ok ... + // return Response(200, ApiResponse{}), nil + + return Response(http.StatusNotImplemented, nil), errors.New("UploadFileArrayOfFiles method not implemented") +}