From 929531cc186cb143fb82f8f5ba4bd0771daf3fed Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 12 Oct 2023 16:26:13 +0800 Subject: [PATCH] add tests for #16787, minor format change --- .../go-server/controller-api.mustache | 16 ++-- .../resources/3_0/go-server/petstore.yaml | 41 ++++++++ .../go-api-server/.openapi-generator/FILES | 2 + .../petstore/go-api-server/api/openapi.yaml | 45 +++++++++ .../server/petstore/go-api-server/go/api.go | 16 ++++ .../petstore/go-api-server/go/api_fake.go | 94 +++++++++++++++++++ .../go-api-server/go/api_fake_service.go | 39 ++++++++ .../petstore/go-api-server/go/api_pet.go | 9 +- .../petstore/go-api-server/go/api_store.go | 2 +- .../petstore/go-api-server/go/api_user.go | 2 +- samples/server/petstore/go-api-server/main.go | 5 +- 11 files changed, 256 insertions(+), 15 deletions(-) create mode 100644 samples/server/petstore/go-api-server/go/api_fake.go create mode 100644 samples/server/petstore/go-api-server/go/api_fake_service.go 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..46346a179ad9 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" @@ -41,7 +41,7 @@ func With{{classname}}ErrorHandler(h ErrorHandler) {{classname}}Option { // New{{classname}}Controller creates a default api controller func New{{classname}}Controller(s {{classname}}Servicer, opts ...{{classname}}Option) Router { controller := &{{classname}}Controller{ - service: s, + service: s, errorHandler: DefaultErrorHandler, } @@ -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..e01d304a5d63 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: + - fake + summary: uploads images (array of files0 + 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/server/petstore/go-api-server/.openapi-generator/FILES b/samples/server/petstore/go-api-server/.openapi-generator/FILES index 4c3a2a3a7c74..83bdb40f3026 100644 --- a/samples/server/petstore/go-api-server/.openapi-generator/FILES +++ b/samples/server/petstore/go-api-server/.openapi-generator/FILES @@ -3,6 +3,8 @@ README.md api/openapi.yaml go.mod go/api.go +go/api_fake.go +go/api_fake_service.go go/api_pet.go go/api_pet_service.go go/api_store.go diff --git a/samples/server/petstore/go-api-server/api/openapi.yaml b/samples/server/petstore/go-api-server/api/openapi.yaml index cc433a6afd59..c19941d7b89e 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 files0 + tags: + - fake 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..75377a7b5cc4 100644 --- a/samples/server/petstore/go-api-server/go/api.go +++ b/samples/server/petstore/go-api-server/go/api.go @@ -13,10 +13,17 @@ import ( "context" "net/http" "os" + "os" ) +// FakeAPIRouter defines the required methods for binding the api requests to a responses for the FakeAPI +// The FakeAPIRouter implementation should parse necessary information from the http request, +// pass the data to a FakeAPIServicer to perform the required actions, then write the service results to the http response. +type FakeAPIRouter interface { + UploadFileArrayOfFiles(http.ResponseWriter, *http.Request) +} // PetAPIRouter defines the required methods for binding the api requests to a responses for the PetAPI // The PetAPIRouter implementation should parse necessary information from the http request, // pass the data to a PetAPIServicer to perform the required actions, then write the service results to the http response. @@ -55,6 +62,15 @@ type UserAPIRouter interface { } +// FakeAPIServicer defines the api actions for the FakeAPI service +// This interface intended to stay up to date with the openapi yaml used to generate it, +// while the service implementation can be ignored with the .openapi-generator-ignore file +// and updated with the logic required for the API. +type FakeAPIServicer interface { + UploadFileArrayOfFiles(context.Context, int64, string, []*os.File) (ImplResponse, error) +} + + // PetAPIServicer defines the api actions for the PetAPI service // This interface intended to stay up to date with the openapi yaml used to generate it, // while the service implementation can be ignored with the .openapi-generator-ignore file diff --git a/samples/server/petstore/go-api-server/go/api_fake.go b/samples/server/petstore/go-api-server/go/api_fake.go new file mode 100644 index 000000000000..28bd32af7757 --- /dev/null +++ b/samples/server/petstore/go-api-server/go/api_fake.go @@ -0,0 +1,94 @@ +/* + * OpenAPI Petstore + * + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * API version: 1.0.0 + * Generated by: OpenAPI Generator (https://openapi-generator.tech) + */ + +package petstoreserver + +import ( + "encoding/json" + "net/http" + "strings" + + "github.com/gorilla/mux" +) + +// FakeAPIController binds http requests to an api service and writes the service results to the http response +type FakeAPIController struct { + service FakeAPIServicer + errorHandler ErrorHandler +} + +// FakeAPIOption for how the controller is set up. +type FakeAPIOption func(*FakeAPIController) + +// WithFakeAPIErrorHandler inject ErrorHandler into controller +func WithFakeAPIErrorHandler(h ErrorHandler) FakeAPIOption { + return func(c *FakeAPIController) { + c.errorHandler = h + } +} + +// NewFakeAPIController creates a default api controller +func NewFakeAPIController(s FakeAPIServicer, opts ...FakeAPIOption) Router { + controller := &FakeAPIController{ + service: s, + errorHandler: DefaultErrorHandler, + } + + for _, opt := range opts { + opt(controller) + } + + return controller +} + +// Routes returns all the api routes for the FakeAPIController +func (c *FakeAPIController) Routes() Routes { + return Routes{ + "UploadFileArrayOfFiles": Route{ + strings.ToUpper("Post"), + "/v2/fake/uploadImage/array of_file", + c.UploadFileArrayOfFiles, + }, + } +} + +// UploadFileArrayOfFiles - uploads images (array of files0 +func (c *FakeAPIController) 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_fake_service.go b/samples/server/petstore/go-api-server/go/api_fake_service.go new file mode 100644 index 000000000000..937317955eee --- /dev/null +++ b/samples/server/petstore/go-api-server/go/api_fake_service.go @@ -0,0 +1,39 @@ +/* + * OpenAPI Petstore + * + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * API version: 1.0.0 + * Generated by: OpenAPI Generator (https://openapi-generator.tech) + */ + +package petstoreserver + +import ( + "context" + "net/http" + "errors" + "os" +) + +// FakeAPIService is a service that implements the logic for the FakeAPIServicer +// This service should implement the business logic for every endpoint for the FakeAPI API. +// Include any external packages or services that will be required by this service. +type FakeAPIService struct { +} + +// NewFakeAPIService creates a default api service +func NewFakeAPIService() FakeAPIServicer { + return &FakeAPIService{} +} + +// UploadFileArrayOfFiles - uploads images (array of files0 +func (s *FakeAPIService) 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_fake_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-api-server/go/api_pet.go b/samples/server/petstore/go-api-server/go/api_pet.go index 0161ba41d6cf..86adccd2e4c1 100644 --- a/samples/server/petstore/go-api-server/go/api_pet.go +++ b/samples/server/petstore/go-api-server/go/api_pet.go @@ -36,7 +36,7 @@ func WithPetAPIErrorHandler(h ErrorHandler) PetAPIOption { // NewPetAPIController creates a default api controller func NewPetAPIController(s PetAPIServicer, opts ...PetAPIOption) Router { controller := &PetAPIController{ - service: s, + service: s, errorHandler: DefaultErrorHandler, } @@ -234,10 +234,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 +265,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/go/api_store.go b/samples/server/petstore/go-api-server/go/api_store.go index df87b17defa6..ca1fa161b71e 100644 --- a/samples/server/petstore/go-api-server/go/api_store.go +++ b/samples/server/petstore/go-api-server/go/api_store.go @@ -36,7 +36,7 @@ func WithStoreAPIErrorHandler(h ErrorHandler) StoreAPIOption { // NewStoreAPIController creates a default api controller func NewStoreAPIController(s StoreAPIServicer, opts ...StoreAPIOption) Router { controller := &StoreAPIController{ - service: s, + service: s, errorHandler: DefaultErrorHandler, } diff --git a/samples/server/petstore/go-api-server/go/api_user.go b/samples/server/petstore/go-api-server/go/api_user.go index cb2aff7dd434..16cb62b88405 100644 --- a/samples/server/petstore/go-api-server/go/api_user.go +++ b/samples/server/petstore/go-api-server/go/api_user.go @@ -36,7 +36,7 @@ func WithUserAPIErrorHandler(h ErrorHandler) UserAPIOption { // NewUserAPIController creates a default api controller func NewUserAPIController(s UserAPIServicer, opts ...UserAPIOption) Router { controller := &UserAPIController{ - service: s, + service: s, errorHandler: DefaultErrorHandler, } diff --git a/samples/server/petstore/go-api-server/main.go b/samples/server/petstore/go-api-server/main.go index c3153dbd8f94..6c2d19ea7e6e 100644 --- a/samples/server/petstore/go-api-server/main.go +++ b/samples/server/petstore/go-api-server/main.go @@ -19,6 +19,9 @@ import ( func main() { log.Printf("Server started") + FakeAPIService := petstoreserver.NewFakeAPIService() + FakeAPIController := petstoreserver.NewFakeAPIController(FakeAPIService) + PetAPIService := petstoreserver.NewPetAPIService() PetAPIController := petstoreserver.NewPetAPIController(PetAPIService) @@ -28,7 +31,7 @@ func main() { UserAPIService := petstoreserver.NewUserAPIService() UserAPIController := petstoreserver.NewUserAPIController(UserAPIService) - router := petstoreserver.NewRouter(PetAPIController, StoreAPIController, UserAPIController) + router := petstoreserver.NewRouter(FakeAPIController, PetAPIController, StoreAPIController, UserAPIController) log.Fatal(http.ListenAndServe(":8080", router)) }