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 0e249cae5d9e..7a7c158efb79 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 @@ -849,11 +849,37 @@ paths: application/json: schema: $ref: '#/components/schemas/ApiResponse' + /fake/collection/test: + post: + summary: POST a test batch + operationId: fakePostTest + tags: + - fake + requestBody: + $ref: '#/components/requestBodies/TestBody' + responses: + '200': + $ref: '#/components/responses/SuccessfulOp' externalDocs: description: Find out more about Swagger url: 'http://swagger.io' components: + responses: + SuccessfulOp: + description: Successful Operation + content: + application/json: + schema: + type: bool requestBodies: + TestBody: + description: Test body + required: true + content: + text/plain: + schema: + type: string + format: byte UserArray: content: application/json: diff --git a/samples/server/petstore/go-api-server/.openapi-generator/FILES b/samples/server/petstore/go-api-server/.openapi-generator/FILES index a02f1779f262..4b544af6e6f4 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 f309214557fe..eee830042cb9 100644 --- a/samples/server/petstore/go-api-server/api/openapi.yaml +++ b/samples/server/petstore/go-api-server/api/openapi.yaml @@ -893,8 +893,31 @@ paths: summary: Get the pets by time tags: - pet + /fake/collection/test: + post: + operationId: fakePostTest + requestBody: + $ref: '#/components/requestBodies/TestBody' + responses: + "200": + content: + application/json: + schema: + type: bool + description: Successful Operation + summary: POST a test batch + tags: + - fake components: requestBodies: + TestBody: + content: + text/plain: + schema: + format: byte + type: string + description: Test body + required: true UserArray: content: application/json: @@ -914,6 +937,13 @@ components: $ref: '#/components/schemas/Pet' description: Pet object that needs to be added to the store required: true + responses: + SuccessfulOp: + content: + application/json: + schema: + type: bool + description: Successful Operation schemas: OrderInfo: description: An order info for a pets from the pet store diff --git a/samples/server/petstore/go-api-server/go/api.go b/samples/server/petstore/go-api-server/go/api.go index b6f9945682d4..c53cf2f35d40 100644 --- a/samples/server/petstore/go-api-server/go/api.go +++ b/samples/server/petstore/go-api-server/go/api.go @@ -19,6 +19,12 @@ import ( +// 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 { + FakePostTest(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. @@ -63,6 +69,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 { + FakePostTest(context.Context, string) (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..934438973434 --- /dev/null +++ b/samples/server/petstore/go-api-server/go/api_fake.go @@ -0,0 +1,77 @@ +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +/* + * 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 + */ + +package petstoreserver + +import ( + "encoding/json" + "net/http" + "strings" +) + +// 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) *FakeAPIController { + 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{ + "FakePostTest": Route{ + strings.ToUpper("Post"), + "/v2/fake/collection/test", + c.FakePostTest, + }, + } +} + +// FakePostTest - POST a test batch +func (c *FakeAPIController) FakePostTest(w http.ResponseWriter, r *http.Request) { + var bodyParam string + d := json.NewDecoder(r.Body) + d.DisallowUnknownFields() + if err := d.Decode(&bodyParam); err != nil { + c.errorHandler(w, r, &ParsingError{Err: err}, nil) + return + } + result, err := c.service.FakePostTest(r.Context(), bodyParam) + // 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..60d6f390f8b2 --- /dev/null +++ b/samples/server/petstore/go-api-server/go/api_fake_service.go @@ -0,0 +1,39 @@ +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +/* + * 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 + */ + +package petstoreserver + +import ( + "context" + "net/http" + "errors" +) + +// 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() *FakeAPIService { + return &FakeAPIService{} +} + +// FakePostTest - POST a test batch +func (s *FakeAPIService) FakePostTest(ctx context.Context, body string) (ImplResponse, error) { + // TODO - update FakePostTest 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, bool{}) or use other options such as http.Ok ... + // return Response(200, bool{}), nil + + return Response(http.StatusNotImplemented, nil), errors.New("FakePostTest method not implemented") +} diff --git a/samples/server/petstore/go-api-server/main.go b/samples/server/petstore/go-api-server/main.go index e5494ee86323..f1a58c7a8d33 100644 --- a/samples/server/petstore/go-api-server/main.go +++ b/samples/server/petstore/go-api-server/main.go @@ -20,6 +20,9 @@ import ( func main() { log.Printf("Server started") + FakeAPIService := petstoreserver.NewFakeAPIService() + FakeAPIController := petstoreserver.NewFakeAPIController(FakeAPIService) + PetAPIService := petstoreserver.NewPetAPIService() PetAPIController := petstoreserver.NewPetAPIController(PetAPIService) @@ -29,7 +32,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)) } diff --git a/samples/server/petstore/go-chi-server/.openapi-generator/FILES b/samples/server/petstore/go-chi-server/.openapi-generator/FILES index a02f1779f262..4b544af6e6f4 100644 --- a/samples/server/petstore/go-chi-server/.openapi-generator/FILES +++ b/samples/server/petstore/go-chi-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-chi-server/api/openapi.yaml b/samples/server/petstore/go-chi-server/api/openapi.yaml index f309214557fe..eee830042cb9 100644 --- a/samples/server/petstore/go-chi-server/api/openapi.yaml +++ b/samples/server/petstore/go-chi-server/api/openapi.yaml @@ -893,8 +893,31 @@ paths: summary: Get the pets by time tags: - pet + /fake/collection/test: + post: + operationId: fakePostTest + requestBody: + $ref: '#/components/requestBodies/TestBody' + responses: + "200": + content: + application/json: + schema: + type: bool + description: Successful Operation + summary: POST a test batch + tags: + - fake components: requestBodies: + TestBody: + content: + text/plain: + schema: + format: byte + type: string + description: Test body + required: true UserArray: content: application/json: @@ -914,6 +937,13 @@ components: $ref: '#/components/schemas/Pet' description: Pet object that needs to be added to the store required: true + responses: + SuccessfulOp: + content: + application/json: + schema: + type: bool + description: Successful Operation schemas: OrderInfo: description: An order info for a pets from the pet store diff --git a/samples/server/petstore/go-chi-server/go/api.go b/samples/server/petstore/go-chi-server/go/api.go index b6f9945682d4..c53cf2f35d40 100644 --- a/samples/server/petstore/go-chi-server/go/api.go +++ b/samples/server/petstore/go-chi-server/go/api.go @@ -19,6 +19,12 @@ import ( +// 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 { + FakePostTest(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. @@ -63,6 +69,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 { + FakePostTest(context.Context, string) (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-chi-server/go/api_fake.go b/samples/server/petstore/go-chi-server/go/api_fake.go new file mode 100644 index 000000000000..934438973434 --- /dev/null +++ b/samples/server/petstore/go-chi-server/go/api_fake.go @@ -0,0 +1,77 @@ +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +/* + * 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 + */ + +package petstoreserver + +import ( + "encoding/json" + "net/http" + "strings" +) + +// 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) *FakeAPIController { + 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{ + "FakePostTest": Route{ + strings.ToUpper("Post"), + "/v2/fake/collection/test", + c.FakePostTest, + }, + } +} + +// FakePostTest - POST a test batch +func (c *FakeAPIController) FakePostTest(w http.ResponseWriter, r *http.Request) { + var bodyParam string + d := json.NewDecoder(r.Body) + d.DisallowUnknownFields() + if err := d.Decode(&bodyParam); err != nil { + c.errorHandler(w, r, &ParsingError{Err: err}, nil) + return + } + result, err := c.service.FakePostTest(r.Context(), bodyParam) + // 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_fake_service.go b/samples/server/petstore/go-chi-server/go/api_fake_service.go new file mode 100644 index 000000000000..60d6f390f8b2 --- /dev/null +++ b/samples/server/petstore/go-chi-server/go/api_fake_service.go @@ -0,0 +1,39 @@ +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +/* + * 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 + */ + +package petstoreserver + +import ( + "context" + "net/http" + "errors" +) + +// 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() *FakeAPIService { + return &FakeAPIService{} +} + +// FakePostTest - POST a test batch +func (s *FakeAPIService) FakePostTest(ctx context.Context, body string) (ImplResponse, error) { + // TODO - update FakePostTest 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, bool{}) or use other options such as http.Ok ... + // return Response(200, bool{}), nil + + return Response(http.StatusNotImplemented, nil), errors.New("FakePostTest method not implemented") +} diff --git a/samples/server/petstore/go-chi-server/main.go b/samples/server/petstore/go-chi-server/main.go index e5494ee86323..f1a58c7a8d33 100644 --- a/samples/server/petstore/go-chi-server/main.go +++ b/samples/server/petstore/go-chi-server/main.go @@ -20,6 +20,9 @@ import ( func main() { log.Printf("Server started") + FakeAPIService := petstoreserver.NewFakeAPIService() + FakeAPIController := petstoreserver.NewFakeAPIController(FakeAPIService) + PetAPIService := petstoreserver.NewPetAPIService() PetAPIController := petstoreserver.NewPetAPIController(PetAPIService) @@ -29,7 +32,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)) }