diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoServerCodegen.java index 179111235048..2c216ab78273 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoServerCodegen.java @@ -136,8 +136,6 @@ public class GoServerCodegen extends AbstractGoCodegen { @Override public void processOpts() { super.processOpts(); - - /* * Additional Properties. These values can be passed to the templates and * are available in models, apis, and supporting files @@ -201,6 +199,8 @@ public class GoServerCodegen extends AbstractGoCodegen { supportingFiles.add(new SupportingFile("go.mod.mustache", "", "go.mod")); supportingFiles.add(new SupportingFile("routers.mustache", sourceFolder, "routers.go")); supportingFiles.add(new SupportingFile("logger.mustache", sourceFolder, "logger.go")); + supportingFiles.add(new SupportingFile("impl.mustache",sourceFolder, "impl.go")); + supportingFiles.add(new SupportingFile("helpers.mustache", sourceFolder, "helpers.go")); supportingFiles.add(new SupportingFile("api.mustache", sourceFolder, "api.go")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md") .doNotOverwrite()); diff --git a/modules/openapi-generator/src/main/resources/go-server/api.mustache b/modules/openapi-generator/src/main/resources/go-server/api.mustache index 91e1265d2e59..8249ffb38f66 100644 --- a/modules/openapi-generator/src/main/resources/go-server/api.mustache +++ b/modules/openapi-generator/src/main/resources/go-server/api.mustache @@ -7,6 +7,7 @@ import ( "{{import}}"{{/imports}}{{/apis}}{{/apiInfo}} ) + {{#apiInfo}}{{#apis}} // {{classname}}Router defines the required methods for binding the api requests to a responses for the {{classname}} // The {{classname}}Router implementation should parse necessary information from the http request, @@ -21,5 +22,5 @@ type {{classname}}Router interface { {{#operations}}{{#operation}} // while the service implementation can ignored with the .openapi-generator-ignore file // and updated with the logic required for the API. type {{classname}}Servicer interface { {{#operations}}{{#operation}} - {{operationId}}(context.Context{{#allParams}}, {{dataType}}{{/allParams}}) (interface{}, error){{/operation}}{{/operations}} + {{operationId}}(context.Context{{#allParams}}, {{dataType}}{{/allParams}}) (ImplResponse, error){{/operation}}{{/operations}} }{{/apis}}{{/apiInfo}} 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 7b01940a64ec..215b99a3038b 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 @@ -93,10 +93,12 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re } {{/isBodyParam}}{{/allParams}} result, err := c.service.{{nickname}}(r.Context(){{#allParams}}, {{#isBodyParam}}*{{/isBodyParam}}{{paramName}}{{/allParams}}) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) }{{/operation}}{{/operations}} diff --git a/modules/openapi-generator/src/main/resources/go-server/helpers.mustache b/modules/openapi-generator/src/main/resources/go-server/helpers.mustache new file mode 100644 index 000000000000..2441678ab3ba --- /dev/null +++ b/modules/openapi-generator/src/main/resources/go-server/helpers.mustache @@ -0,0 +1,8 @@ +{{>partial_header}} +package {{packageName}} + +//Response return a ImplResponse struct filled +func Response(code int, body interface{}) ImplResponse { + return ImplResponse{Code: code, Body: body} +} + diff --git a/modules/openapi-generator/src/main/resources/go-server/impl.mustache b/modules/openapi-generator/src/main/resources/go-server/impl.mustache new file mode 100644 index 000000000000..3333cdecdc2a --- /dev/null +++ b/modules/openapi-generator/src/main/resources/go-server/impl.mustache @@ -0,0 +1,8 @@ +{{>partial_header}} +package {{packageName}} + +//Implementation response defines an error code with the associated body +type ImplResponse struct { + Code int + Body interface{} +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/go-server/service.mustache b/modules/openapi-generator/src/main/resources/go-server/service.mustache index bd9f28ec4423..510bc01ab89f 100644 --- a/modules/openapi-generator/src/main/resources/go-server/service.mustache +++ b/modules/openapi-generator/src/main/resources/go-server/service.mustache @@ -3,6 +3,7 @@ package {{packageName}} import ( "context" + "net/http" "errors"{{#imports}} "{{import}}"{{/imports}} ) @@ -19,8 +20,22 @@ func New{{classname}}Service() {{classname}}Servicer { }{{#operations}}{{#operation}} // {{nickname}} - {{summary}} -func (s *{{classname}}Service) {{nickname}}(ctx context.Context{{#allParams}}, {{paramName}} {{dataType}}{{/allParams}}) (interface{}, error) { +func (s *{{classname}}Service) {{nickname}}(ctx context.Context{{#allParams}}, {{paramName}} {{dataType}}{{/allParams}}) (ImplResponse, error) { // TODO - update {{nickname}} with the required logic for this service method. // Add {{classFilename}}_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - return nil, errors.New("service method '{{nickname}}' not implemented") + +{{#responses}} +{{#dataType}} + //TODO: Uncomment the next line to return response Response({{code}}, {{dataType}}{}) or use other options such as http.Ok ... + //return Response({{code}}, {{dataType}}{}), nil + +{{/dataType}} +{{^dataType}} + //TODO: Uncomment the next line to return response Response({{code}}, {{dataType}}{}) or use other options such as http.Ok ... + //return Response({{code}}, nil),nil + +{{/dataType}} +{{/responses}} + return Response(http.StatusNotImplemented, nil), errors.New("{{nickname}} method not implemented") }{{/operation}}{{/operations}} + diff --git a/samples/server/petstore/go-api-server/.openapi-generator/FILES b/samples/server/petstore/go-api-server/.openapi-generator/FILES index 40a9445dadf3..2fe863dc7ff1 100644 --- a/samples/server/petstore/go-api-server/.openapi-generator/FILES +++ b/samples/server/petstore/go-api-server/.openapi-generator/FILES @@ -9,6 +9,8 @@ go/api_store.go go/api_store_service.go go/api_user.go go/api_user_service.go +go/helpers.go +go/impl.go go/logger.go go/model_api_response.go go/model_category.go diff --git a/samples/server/petstore/go-api-server/go/api.go b/samples/server/petstore/go-api-server/go/api.go index 7c3037304dc9..64321a00b2e7 100644 --- a/samples/server/petstore/go-api-server/go/api.go +++ b/samples/server/petstore/go-api-server/go/api.go @@ -16,6 +16,7 @@ import ( ) + // 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. @@ -58,14 +59,14 @@ type UserApiRouter interface { // while the service implementation can ignored with the .openapi-generator-ignore file // and updated with the logic required for the API. type PetApiServicer interface { - AddPet(context.Context, Pet) (interface{}, error) - DeletePet(context.Context, int64, string) (interface{}, error) - FindPetsByStatus(context.Context, []string) (interface{}, error) - FindPetsByTags(context.Context, []string) (interface{}, error) - GetPetById(context.Context, int64) (interface{}, error) - UpdatePet(context.Context, Pet) (interface{}, error) - UpdatePetWithForm(context.Context, int64, string, string) (interface{}, error) - UploadFile(context.Context, int64, string, *os.File) (interface{}, error) + AddPet(context.Context, Pet) (ImplResponse, error) + DeletePet(context.Context, int64, string) (ImplResponse, error) + FindPetsByStatus(context.Context, []string) (ImplResponse, error) + FindPetsByTags(context.Context, []string) (ImplResponse, error) + GetPetById(context.Context, int64) (ImplResponse, error) + UpdatePet(context.Context, Pet) (ImplResponse, error) + UpdatePetWithForm(context.Context, int64, string, string) (ImplResponse, error) + UploadFile(context.Context, int64, string, *os.File) (ImplResponse, error) } @@ -74,10 +75,10 @@ type PetApiServicer interface { // while the service implementation can ignored with the .openapi-generator-ignore file // and updated with the logic required for the API. type StoreApiServicer interface { - DeleteOrder(context.Context, string) (interface{}, error) - GetInventory(context.Context) (interface{}, error) - GetOrderById(context.Context, int64) (interface{}, error) - PlaceOrder(context.Context, Order) (interface{}, error) + DeleteOrder(context.Context, string) (ImplResponse, error) + GetInventory(context.Context) (ImplResponse, error) + GetOrderById(context.Context, int64) (ImplResponse, error) + PlaceOrder(context.Context, Order) (ImplResponse, error) } @@ -86,12 +87,12 @@ type StoreApiServicer interface { // while the service implementation can ignored with the .openapi-generator-ignore file // and updated with the logic required for the API. type UserApiServicer interface { - CreateUser(context.Context, User) (interface{}, error) - CreateUsersWithArrayInput(context.Context, []User) (interface{}, error) - CreateUsersWithListInput(context.Context, []User) (interface{}, error) - DeleteUser(context.Context, string) (interface{}, error) - GetUserByName(context.Context, string) (interface{}, error) - LoginUser(context.Context, string, string) (interface{}, error) - LogoutUser(context.Context) (interface{}, error) - UpdateUser(context.Context, string, User) (interface{}, error) + CreateUser(context.Context, User) (ImplResponse, error) + CreateUsersWithArrayInput(context.Context, []User) (ImplResponse, error) + CreateUsersWithListInput(context.Context, []User) (ImplResponse, error) + DeleteUser(context.Context, string) (ImplResponse, error) + GetUserByName(context.Context, string) (ImplResponse, error) + LoginUser(context.Context, string, string) (ImplResponse, error) + LogoutUser(context.Context) (ImplResponse, error) + UpdateUser(context.Context, string, User) (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 9a589b7e6641..f5e78aa9fd7d 100644 --- a/samples/server/petstore/go-api-server/go/api_pet.go +++ b/samples/server/petstore/go-api-server/go/api_pet.go @@ -90,12 +90,14 @@ func (c *PetApiController) AddPet(w http.ResponseWriter, r *http.Request) { } result, err := c.service.AddPet(r.Context(), *pet) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // DeletePet - Deletes a pet @@ -108,12 +110,14 @@ func (c *PetApiController) DeletePet(w http.ResponseWriter, r *http.Request) { } apiKey := r.Header.Get("apiKey") result, err := c.service.DeletePet(r.Context(), petId, apiKey) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // FindPetsByStatus - Finds Pets by status @@ -121,12 +125,14 @@ func (c *PetApiController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque query := r.URL.Query() status := strings.Split(query.Get("status"), ",") result, err := c.service.FindPetsByStatus(r.Context(), status) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // FindPetsByTags - Finds Pets by tags @@ -134,12 +140,14 @@ func (c *PetApiController) FindPetsByTags(w http.ResponseWriter, r *http.Request query := r.URL.Query() tags := strings.Split(query.Get("tags"), ",") result, err := c.service.FindPetsByTags(r.Context(), tags) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // GetPetById - Find pet by ID @@ -151,12 +159,14 @@ func (c *PetApiController) GetPetById(w http.ResponseWriter, r *http.Request) { return } result, err := c.service.GetPetById(r.Context(), petId) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // UpdatePet - Update an existing pet @@ -168,12 +178,14 @@ func (c *PetApiController) UpdatePet(w http.ResponseWriter, r *http.Request) { } result, err := c.service.UpdatePet(r.Context(), *pet) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // UpdatePetWithForm - Updates a pet in the store with form data @@ -193,12 +205,14 @@ func (c *PetApiController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ name := r.FormValue("name") status := r.FormValue("status") result, err := c.service.UpdatePetWithForm(r.Context(), petId, name, status) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // UploadFile - uploads an image @@ -223,10 +237,12 @@ func (c *PetApiController) UploadFile(w http.ResponseWriter, r *http.Request) { } result, err := c.service.UploadFile(r.Context(), petId, additionalMetadata, file) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, 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 6b156140b9d0..bd9ab8f0e408 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 @@ -11,6 +11,7 @@ package petstoreserver import ( "context" + "net/http" "errors" "os" ) @@ -27,57 +28,114 @@ func NewPetApiService() PetApiServicer { } // AddPet - Add a new pet to the store -func (s *PetApiService) AddPet(ctx context.Context, pet Pet) (interface{}, error) { +func (s *PetApiService) AddPet(ctx context.Context, pet Pet) (ImplResponse, error) { // TODO - update AddPet 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. - return nil, errors.New("service method 'AddPet' not implemented") + + //TODO: Uncomment the next line to return response Response(200, Pet{}) or use other options such as http.Ok ... + //return Response(200, Pet{}), nil + + //TODO: Uncomment the next line to return response Response(405, {}) or use other options such as http.Ok ... + //return Response(405, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("AddPet method not implemented") } // DeletePet - Deletes a pet -func (s *PetApiService) DeletePet(ctx context.Context, petId int64, apiKey string) (interface{}, error) { +func (s *PetApiService) DeletePet(ctx context.Context, petId int64, apiKey string) (ImplResponse, error) { // TODO - update DeletePet 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. - return nil, errors.New("service method 'DeletePet' not implemented") + + //TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ... + //return Response(400, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("DeletePet method not implemented") } // FindPetsByStatus - Finds Pets by status -func (s *PetApiService) FindPetsByStatus(ctx context.Context, status []string) (interface{}, error) { +func (s *PetApiService) FindPetsByStatus(ctx context.Context, status []string) (ImplResponse, error) { // TODO - update FindPetsByStatus 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. - return nil, errors.New("service method 'FindPetsByStatus' not implemented") + + //TODO: Uncomment the next line to return response Response(200, []Pet{}) or use other options such as http.Ok ... + //return Response(200, []Pet{}), nil + + //TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ... + //return Response(400, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("FindPetsByStatus method not implemented") } // FindPetsByTags - Finds Pets by tags -func (s *PetApiService) FindPetsByTags(ctx context.Context, tags []string) (interface{}, error) { +func (s *PetApiService) FindPetsByTags(ctx context.Context, tags []string) (ImplResponse, error) { // TODO - update FindPetsByTags 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. - return nil, errors.New("service method 'FindPetsByTags' not implemented") + + //TODO: Uncomment the next line to return response Response(200, []Pet{}) or use other options such as http.Ok ... + //return Response(200, []Pet{}), nil + + //TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ... + //return Response(400, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("FindPetsByTags method not implemented") } // GetPetById - Find pet by ID -func (s *PetApiService) GetPetById(ctx context.Context, petId int64) (interface{}, error) { +func (s *PetApiService) GetPetById(ctx context.Context, petId int64) (ImplResponse, error) { // TODO - update GetPetById 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. - return nil, errors.New("service method 'GetPetById' not implemented") + + //TODO: Uncomment the next line to return response Response(200, Pet{}) or use other options such as http.Ok ... + //return Response(200, Pet{}), nil + + //TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ... + //return Response(400, nil),nil + + //TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ... + //return Response(404, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("GetPetById method not implemented") } // UpdatePet - Update an existing pet -func (s *PetApiService) UpdatePet(ctx context.Context, pet Pet) (interface{}, error) { +func (s *PetApiService) UpdatePet(ctx context.Context, pet Pet) (ImplResponse, error) { // TODO - update UpdatePet 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. - return nil, errors.New("service method 'UpdatePet' not implemented") + + //TODO: Uncomment the next line to return response Response(200, Pet{}) or use other options such as http.Ok ... + //return Response(200, Pet{}), nil + + //TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ... + //return Response(400, nil),nil + + //TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ... + //return Response(404, nil),nil + + //TODO: Uncomment the next line to return response Response(405, {}) or use other options such as http.Ok ... + //return Response(405, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("UpdatePet method not implemented") } // UpdatePetWithForm - Updates a pet in the store with form data -func (s *PetApiService) UpdatePetWithForm(ctx context.Context, petId int64, name string, status string) (interface{}, error) { +func (s *PetApiService) UpdatePetWithForm(ctx context.Context, petId int64, name string, status string) (ImplResponse, error) { // TODO - update UpdatePetWithForm 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. - return nil, errors.New("service method 'UpdatePetWithForm' not implemented") + + //TODO: Uncomment the next line to return response Response(405, {}) or use other options such as http.Ok ... + //return Response(405, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("UpdatePetWithForm method not implemented") } // UploadFile - uploads an image -func (s *PetApiService) UploadFile(ctx context.Context, petId int64, additionalMetadata string, file *os.File) (interface{}, error) { +func (s *PetApiService) UploadFile(ctx context.Context, petId int64, additionalMetadata string, file *os.File) (ImplResponse, error) { // TODO - update UploadFile 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. - return nil, errors.New("service method 'UploadFile' not implemented") + + //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("UploadFile method not implemented") } + 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 fdfec50dd15e..fa5f6ce74839 100644 --- a/samples/server/petstore/go-api-server/go/api_store.go +++ b/samples/server/petstore/go-api-server/go/api_store.go @@ -62,23 +62,27 @@ func (c *StoreApiController) DeleteOrder(w http.ResponseWriter, r *http.Request) params := mux.Vars(r) orderId := params["orderId"] result, err := c.service.DeleteOrder(r.Context(), orderId) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // GetInventory - Returns pet inventories by status func (c *StoreApiController) GetInventory(w http.ResponseWriter, r *http.Request) { result, err := c.service.GetInventory(r.Context()) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // GetOrderById - Find purchase order by ID @@ -90,12 +94,14 @@ func (c *StoreApiController) GetOrderById(w http.ResponseWriter, r *http.Request return } result, err := c.service.GetOrderById(r.Context(), orderId) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // PlaceOrder - Place an order for a pet @@ -107,10 +113,12 @@ func (c *StoreApiController) PlaceOrder(w http.ResponseWriter, r *http.Request) } result, err := c.service.PlaceOrder(r.Context(), *order) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } diff --git a/samples/server/petstore/go-api-server/go/api_store_service.go b/samples/server/petstore/go-api-server/go/api_store_service.go index ecdace978336..8ac7f04e5448 100644 --- a/samples/server/petstore/go-api-server/go/api_store_service.go +++ b/samples/server/petstore/go-api-server/go/api_store_service.go @@ -11,6 +11,7 @@ package petstoreserver import ( "context" + "net/http" "errors" ) @@ -26,29 +27,58 @@ func NewStoreApiService() StoreApiServicer { } // DeleteOrder - Delete purchase order by ID -func (s *StoreApiService) DeleteOrder(ctx context.Context, orderId string) (interface{}, error) { +func (s *StoreApiService) DeleteOrder(ctx context.Context, orderId string) (ImplResponse, error) { // TODO - update DeleteOrder with the required logic for this service method. // Add api_store_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - return nil, errors.New("service method 'DeleteOrder' not implemented") + + //TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ... + //return Response(400, nil),nil + + //TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ... + //return Response(404, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("DeleteOrder method not implemented") } // GetInventory - Returns pet inventories by status -func (s *StoreApiService) GetInventory(ctx context.Context) (interface{}, error) { +func (s *StoreApiService) GetInventory(ctx context.Context) (ImplResponse, error) { // TODO - update GetInventory with the required logic for this service method. // Add api_store_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - return nil, errors.New("service method 'GetInventory' not implemented") + + //TODO: Uncomment the next line to return response Response(200, map[string]int32{}) or use other options such as http.Ok ... + //return Response(200, map[string]int32{}), nil + + return Response(http.StatusNotImplemented, nil), errors.New("GetInventory method not implemented") } // GetOrderById - Find purchase order by ID -func (s *StoreApiService) GetOrderById(ctx context.Context, orderId int64) (interface{}, error) { +func (s *StoreApiService) GetOrderById(ctx context.Context, orderId int64) (ImplResponse, error) { // TODO - update GetOrderById with the required logic for this service method. // Add api_store_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - return nil, errors.New("service method 'GetOrderById' not implemented") + + //TODO: Uncomment the next line to return response Response(200, Order{}) or use other options such as http.Ok ... + //return Response(200, Order{}), nil + + //TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ... + //return Response(400, nil),nil + + //TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ... + //return Response(404, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("GetOrderById method not implemented") } // PlaceOrder - Place an order for a pet -func (s *StoreApiService) PlaceOrder(ctx context.Context, order Order) (interface{}, error) { +func (s *StoreApiService) PlaceOrder(ctx context.Context, order Order) (ImplResponse, error) { // TODO - update PlaceOrder with the required logic for this service method. // Add api_store_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - return nil, errors.New("service method 'PlaceOrder' not implemented") + + //TODO: Uncomment the next line to return response Response(200, Order{}) or use other options such as http.Ok ... + //return Response(200, Order{}), nil + + //TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ... + //return Response(400, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("PlaceOrder method not implemented") } + 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 5063f1f432f3..dccaabba7d0e 100644 --- a/samples/server/petstore/go-api-server/go/api_user.go +++ b/samples/server/petstore/go-api-server/go/api_user.go @@ -90,12 +90,14 @@ func (c *UserApiController) CreateUser(w http.ResponseWriter, r *http.Request) { } result, err := c.service.CreateUser(r.Context(), *user) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // CreateUsersWithArrayInput - Creates list of users with given input array @@ -107,12 +109,14 @@ func (c *UserApiController) CreateUsersWithArrayInput(w http.ResponseWriter, r * } result, err := c.service.CreateUsersWithArrayInput(r.Context(), *user) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // CreateUsersWithListInput - Creates list of users with given input array @@ -124,12 +128,14 @@ func (c *UserApiController) CreateUsersWithListInput(w http.ResponseWriter, r *h } result, err := c.service.CreateUsersWithListInput(r.Context(), *user) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // DeleteUser - Delete user @@ -137,12 +143,14 @@ func (c *UserApiController) DeleteUser(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) username := params["username"] result, err := c.service.DeleteUser(r.Context(), username) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // GetUserByName - Get user by user name @@ -150,12 +158,14 @@ func (c *UserApiController) GetUserByName(w http.ResponseWriter, r *http.Request params := mux.Vars(r) username := params["username"] result, err := c.service.GetUserByName(r.Context(), username) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // LoginUser - Logs user into the system @@ -164,23 +174,27 @@ func (c *UserApiController) LoginUser(w http.ResponseWriter, r *http.Request) { username := query.Get("username") password := query.Get("password") result, err := c.service.LoginUser(r.Context(), username, password) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // LogoutUser - Logs out current logged in user session func (c *UserApiController) LogoutUser(w http.ResponseWriter, r *http.Request) { result, err := c.service.LogoutUser(r.Context()) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } // UpdateUser - Updated user @@ -194,10 +208,12 @@ func (c *UserApiController) UpdateUser(w http.ResponseWriter, r *http.Request) { } result, err := c.service.UpdateUser(r.Context(), username, *user) + //If an error occured, encode the error with the status code if err != nil { - w.WriteHeader(500) + EncodeJSONResponse(err.Error(), &result.Code, w) return } + //If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) - EncodeJSONResponse(result, nil, w) } diff --git a/samples/server/petstore/go-api-server/go/api_user_service.go b/samples/server/petstore/go-api-server/go/api_user_service.go index 77b7312a0d2f..5f694d025516 100644 --- a/samples/server/petstore/go-api-server/go/api_user_service.go +++ b/samples/server/petstore/go-api-server/go/api_user_service.go @@ -11,6 +11,7 @@ package petstoreserver import ( "context" + "net/http" "errors" ) @@ -26,57 +27,105 @@ func NewUserApiService() UserApiServicer { } // CreateUser - Create user -func (s *UserApiService) CreateUser(ctx context.Context, user User) (interface{}, error) { +func (s *UserApiService) CreateUser(ctx context.Context, user User) (ImplResponse, error) { // TODO - update CreateUser with the required logic for this service method. // Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - return nil, errors.New("service method 'CreateUser' not implemented") + + //TODO: Uncomment the next line to return response Response(0, {}) or use other options such as http.Ok ... + //return Response(0, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("CreateUser method not implemented") } // CreateUsersWithArrayInput - Creates list of users with given input array -func (s *UserApiService) CreateUsersWithArrayInput(ctx context.Context, user []User) (interface{}, error) { +func (s *UserApiService) CreateUsersWithArrayInput(ctx context.Context, user []User) (ImplResponse, error) { // TODO - update CreateUsersWithArrayInput with the required logic for this service method. // Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - return nil, errors.New("service method 'CreateUsersWithArrayInput' not implemented") + + //TODO: Uncomment the next line to return response Response(0, {}) or use other options such as http.Ok ... + //return Response(0, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("CreateUsersWithArrayInput method not implemented") } // CreateUsersWithListInput - Creates list of users with given input array -func (s *UserApiService) CreateUsersWithListInput(ctx context.Context, user []User) (interface{}, error) { +func (s *UserApiService) CreateUsersWithListInput(ctx context.Context, user []User) (ImplResponse, error) { // TODO - update CreateUsersWithListInput with the required logic for this service method. // Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - return nil, errors.New("service method 'CreateUsersWithListInput' not implemented") + + //TODO: Uncomment the next line to return response Response(0, {}) or use other options such as http.Ok ... + //return Response(0, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("CreateUsersWithListInput method not implemented") } // DeleteUser - Delete user -func (s *UserApiService) DeleteUser(ctx context.Context, username string) (interface{}, error) { +func (s *UserApiService) DeleteUser(ctx context.Context, username string) (ImplResponse, error) { // TODO - update DeleteUser with the required logic for this service method. // Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - return nil, errors.New("service method 'DeleteUser' not implemented") + + //TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ... + //return Response(400, nil),nil + + //TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ... + //return Response(404, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("DeleteUser method not implemented") } // GetUserByName - Get user by user name -func (s *UserApiService) GetUserByName(ctx context.Context, username string) (interface{}, error) { +func (s *UserApiService) GetUserByName(ctx context.Context, username string) (ImplResponse, error) { // TODO - update GetUserByName with the required logic for this service method. // Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - return nil, errors.New("service method 'GetUserByName' not implemented") + + //TODO: Uncomment the next line to return response Response(200, User{}) or use other options such as http.Ok ... + //return Response(200, User{}), nil + + //TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ... + //return Response(400, nil),nil + + //TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ... + //return Response(404, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("GetUserByName method not implemented") } // LoginUser - Logs user into the system -func (s *UserApiService) LoginUser(ctx context.Context, username string, password string) (interface{}, error) { +func (s *UserApiService) LoginUser(ctx context.Context, username string, password string) (ImplResponse, error) { // TODO - update LoginUser with the required logic for this service method. // Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - return nil, errors.New("service method 'LoginUser' not implemented") + + //TODO: Uncomment the next line to return response Response(200, string{}) or use other options such as http.Ok ... + //return Response(200, string{}), nil + + //TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ... + //return Response(400, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("LoginUser method not implemented") } // LogoutUser - Logs out current logged in user session -func (s *UserApiService) LogoutUser(ctx context.Context) (interface{}, error) { +func (s *UserApiService) LogoutUser(ctx context.Context) (ImplResponse, error) { // TODO - update LogoutUser with the required logic for this service method. // Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - return nil, errors.New("service method 'LogoutUser' not implemented") + + //TODO: Uncomment the next line to return response Response(0, {}) or use other options such as http.Ok ... + //return Response(0, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("LogoutUser method not implemented") } // UpdateUser - Updated user -func (s *UserApiService) UpdateUser(ctx context.Context, username string, user User) (interface{}, error) { +func (s *UserApiService) UpdateUser(ctx context.Context, username string, user User) (ImplResponse, error) { // TODO - update UpdateUser with the required logic for this service method. // Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - return nil, errors.New("service method 'UpdateUser' not implemented") + + //TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ... + //return Response(400, nil),nil + + //TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ... + //return Response(404, nil),nil + + return Response(http.StatusNotImplemented, nil), errors.New("UpdateUser method not implemented") } + diff --git a/samples/server/petstore/go-api-server/go/helpers.go b/samples/server/petstore/go-api-server/go/helpers.go new file mode 100644 index 000000000000..449cbb7be35b --- /dev/null +++ b/samples/server/petstore/go-api-server/go/helpers.go @@ -0,0 +1,16 @@ +/* + * 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 + +//Response return a ImplResponse struct filled +func Response(code int, body interface{}) ImplResponse { + return ImplResponse{Code: code, Body: body} +} + diff --git a/samples/server/petstore/go-api-server/go/impl.go b/samples/server/petstore/go-api-server/go/impl.go new file mode 100644 index 000000000000..034db6e1cda4 --- /dev/null +++ b/samples/server/petstore/go-api-server/go/impl.go @@ -0,0 +1,16 @@ +/* + * 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 + +//Implementation response defines an error code with the associated body +type ImplResponse struct { + Code int + Body interface{} +} \ No newline at end of file