diff --git a/docs/generators/go-gin-server.md b/docs/generators/go-gin-server.md index 9371bf027fde..6c1fa05997a4 100644 --- a/docs/generators/go-gin-server.md +++ b/docs/generators/go-gin-server.md @@ -8,4 +8,5 @@ sidebar_label: go-gin-server | Option | Description | Values | Default | | ------ | ----------- | ------ | ------- | |packageName|Go package name (convention: lowercase).| |openapi| +|packageVersion|Go package version.| |1.0.0| |hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true| diff --git a/docs/generators/go-server.md b/docs/generators/go-server.md index 5ba67d6b3e60..aa3bcc45d5a6 100644 --- a/docs/generators/go-server.md +++ b/docs/generators/go-server.md @@ -8,4 +8,7 @@ sidebar_label: go-server | Option | Description | Values | Default | | ------ | ----------- | ------ | ------- | |packageName|Go package name (convention: lowercase).| |openapi| +|packageVersion|Go package version.| |1.0.0| |hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true| +|sourceFolder|source folder for generated code| |go| +|serverPort|The network port the generated server binds to| |8080| diff --git a/docs/generators/go.md b/docs/generators/go.md index d336f871529b..07cdb2fdd793 100644 --- a/docs/generators/go.md +++ b/docs/generators/go.md @@ -8,8 +8,8 @@ sidebar_label: go | Option | Description | Values | Default | | ------ | ----------- | ------ | ------- | |packageName|Go package name (convention: lowercase).| |openapi| -|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true| |packageVersion|Go package version.| |1.0.0| +|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true| |withGoCodegenComment|whether to include Go codegen comment to disable Go Lint and collapse by default GitHub in PRs and diffs| |false| |withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false| |prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java index 3698b6d000e6..f6fce0c71400 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java @@ -112,7 +112,8 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege cliOptions.clear(); cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "Go package name (convention: lowercase).") .defaultValue("openapi")); - + cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "Go package version.") + .defaultValue("1.0.0")); cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC) .defaultValue(Boolean.TRUE.toString())); } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java index 9f95aacbab82..d53368036270 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java @@ -51,8 +51,6 @@ public class GoClientCodegen extends AbstractGoCodegen { // default HIDE_GENERATION_TIMESTAMP to true hideGenerationTimestamp = Boolean.TRUE; - cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "Go package version.") - .defaultValue("1.0.0")); cliOptions.add(CliOption.newBoolean(WITH_GO_CODEGEN_COMMENT, "whether to include Go codegen comment to disable Go Lint and collapse by default GitHub in PRs and diffs")); cliOptions.add(CliOption.newBoolean(WITH_XML, "whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)")); 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 326f758c624c..d08add86bb00 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 @@ -17,6 +17,7 @@ package org.openapitools.codegen.languages; +import org.openapitools.codegen.CliOption; import org.openapitools.codegen.CodegenConstants; import org.openapitools.codegen.CodegenType; import org.openapitools.codegen.SupportingFile; @@ -30,16 +31,25 @@ public class GoServerCodegen extends AbstractGoCodegen { private static final Logger LOGGER = LoggerFactory.getLogger(GoServerCodegen.class); - protected String apiVersion = "1.0.0"; + protected String packageVersion = "1.0.0"; protected int serverPort = 8080; protected String projectName = "openapi-server"; - protected String apiPath = "go"; + protected String sourceFolder = "go"; + public GoServerCodegen() { super(); // set the output folder here outputFolder = "generated-code/go"; + + cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC) + .defaultValue(sourceFolder)); + + CliOption optServerPort = new CliOption("serverPort", "The network port the generated server binds to"); + optServerPort.setType("int"); + optServerPort.defaultValue(Integer.toString(serverPort)); + cliOptions.add(optServerPort); /* * Models. You can write model files using the modelTemplateFiles map. @@ -88,21 +98,35 @@ 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 + */ if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) { setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME)); } else { setPackageName("openapi"); } - - /* - * Additional Properties. These values can be passed to the templates and - * are available in models, apis, and supporting files - */ - additionalProperties.put("apiVersion", apiVersion); - additionalProperties.put("serverPort", serverPort); - additionalProperties.put("apiPath", apiPath); - additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName); + + if (additionalProperties.containsKey(CodegenConstants.PACKAGE_VERSION)) { + this.setPackageVersion((String) additionalProperties.get(CodegenConstants.PACKAGE_VERSION)); + } else { + additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion); + } + + if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) { + this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER)); + } else { + additionalProperties.put(CodegenConstants.SOURCE_FOLDER, sourceFolder); + } + + if (additionalProperties.containsKey("serverPort") && additionalProperties.get("serverPort") instanceof Integer) { + this.setServerPort((int) additionalProperties.get("serverPort")); + } else { + additionalProperties.put("serverPort", serverPort); + } modelPackage = packageName; apiPackage = packageName; @@ -115,14 +139,14 @@ public class GoServerCodegen extends AbstractGoCodegen { supportingFiles.add(new SupportingFile("openapi.mustache", "api", "openapi.yaml")); supportingFiles.add(new SupportingFile("main.mustache", "", "main.go")); supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile")); - supportingFiles.add(new SupportingFile("routers.mustache", apiPath, "routers.go")); - supportingFiles.add(new SupportingFile("logger.mustache", apiPath, "logger.go")); - writeOptional(outputFolder, new SupportingFile("README.mustache", apiPath, "README.md")); + supportingFiles.add(new SupportingFile("routers.mustache", sourceFolder, "routers.go")); + supportingFiles.add(new SupportingFile("logger.mustache", sourceFolder, "logger.go")); + writeOptional(outputFolder, new SupportingFile("README.mustache", "", "README.md")); } @Override public String apiPackage() { - return apiPath; + return sourceFolder; } /** @@ -172,5 +196,16 @@ public class GoServerCodegen extends AbstractGoCodegen { public String modelFileFolder() { return outputFolder + File.separator + apiPackage().replace('.', File.separatorChar); } + + public void setSourceFolder(String sourceFolder) { + this.sourceFolder = sourceFolder; + } + public void setPackageVersion(String packageVersion) { + this.packageVersion = packageVersion; + } + + public void setServerPort(int serverPort) { + this.serverPort = serverPort; + } } diff --git a/modules/openapi-generator/src/main/resources/go-server/Dockerfile.mustache b/modules/openapi-generator/src/main/resources/go-server/Dockerfile.mustache index 114243086fa2..b00f7cac1452 100644 --- a/modules/openapi-generator/src/main/resources/go-server/Dockerfile.mustache +++ b/modules/openapi-generator/src/main/resources/go-server/Dockerfile.mustache @@ -1,6 +1,6 @@ FROM golang:1.10 AS build WORKDIR /go/src -COPY {{apiPath}} ./{{apiPath}} +COPY {{sourceFolder}} ./{{sourceFolder}} COPY main.go . ENV CGO_ENABLED=0 diff --git a/modules/openapi-generator/src/main/resources/go-server/main.mustache b/modules/openapi-generator/src/main/resources/go-server/main.mustache index 28eb8861b1c9..a4c7a986163a 100644 --- a/modules/openapi-generator/src/main/resources/go-server/main.mustache +++ b/modules/openapi-generator/src/main/resources/go-server/main.mustache @@ -10,9 +10,9 @@ import ( // once you place this file into your project. // For example, // - // sw "github.com/myname/myrepo/{{apiPath}}" + // sw "github.com/myname/myrepo/{{sourceFolder}}" // - sw "./{{apiPath}}" + sw "./{{sourceFolder}}" ) func main() { diff --git a/samples/client/petstore/go/go-petstore-withXml/api/openapi.yaml b/samples/client/petstore/go/go-petstore-withXml/api/openapi.yaml index 2b13b056b955..7212ef07c279 100644 --- a/samples/client/petstore/go/go-petstore-withXml/api/openapi.yaml +++ b/samples/client/petstore/go/go-petstore-withXml/api/openapi.yaml @@ -1476,16 +1476,112 @@ components: type: object AdditionalPropertiesClass: properties: - map_property: + map_string: additionalProperties: type: string type: object - map_of_map_property: + map_number: + additionalProperties: + type: number + type: object + map_integer: + additionalProperties: + type: integer + type: object + map_boolean: + additionalProperties: + type: boolean + type: object + map_array_integer: + additionalProperties: + items: + type: integer + type: array + type: object + map_array_anytype: + additionalProperties: + items: + properties: {} + type: object + type: array + type: object + map_map_string: additionalProperties: additionalProperties: type: string type: object type: object + map_map_anytype: + additionalProperties: + additionalProperties: + properties: {} + type: object + type: object + type: object + anytype_1: + properties: {} + type: object + anytype_2: + type: object + anytype_3: + properties: {} + type: object + type: object + AdditionalPropertiesString: + additionalProperties: + type: string + properties: + name: + type: string + type: object + AdditionalPropertiesInteger: + additionalProperties: + type: integer + properties: + name: + type: string + type: object + AdditionalPropertiesNumber: + additionalProperties: + type: number + properties: + name: + type: string + type: object + AdditionalPropertiesBoolean: + additionalProperties: + type: boolean + properties: + name: + type: string + type: object + AdditionalPropertiesArray: + additionalProperties: + items: + properties: {} + type: object + type: array + properties: + name: + type: string + type: object + AdditionalPropertiesObject: + additionalProperties: + additionalProperties: + properties: {} + type: object + type: object + properties: + name: + type: string + type: object + AdditionalPropertiesAnyType: + additionalProperties: + properties: {} + type: object + properties: + name: + type: string type: object MixedPropertiesAndAdditionalPropertiesClass: properties: @@ -1932,4 +2028,4 @@ components: type: apiKey http_basic_test: scheme: basic - type: http + type: http \ No newline at end of file diff --git a/samples/server/petstore/go-api-server/Dockerfile b/samples/server/petstore/go-api-server/Dockerfile index cfdfbaed0804..0f512c75104b 100644 --- a/samples/server/petstore/go-api-server/Dockerfile +++ b/samples/server/petstore/go-api-server/Dockerfile @@ -1,6 +1,6 @@ FROM golang:1.10 AS build WORKDIR /go/src -COPY go ./go +COPY ./ COPY main.go . ENV CGO_ENABLED=0 diff --git a/samples/server/petstore/go-api-server/api/openapi.yaml b/samples/server/petstore/go-api-server/api/openapi.yaml index ac82672eb887..b25b5c3fe61c 100644 --- a/samples/server/petstore/go-api-server/api/openapi.yaml +++ b/samples/server/petstore/go-api-server/api/openapi.yaml @@ -1,4 +1,4 @@ -openapi: 3.0.1 +openapi: 3.0.0 info: description: This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. license: @@ -6,6 +6,9 @@ info: url: http://www.apache.org/licenses/LICENSE-2.0.html title: OpenAPI Petstore version: 1.0.0 +externalDocs: + description: Find out more about Swagger + url: http://swagger.io servers: - url: http://petstore.swagger.io/v2 tags: @@ -20,18 +23,9 @@ paths: post: operationId: addPet requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/Pet' - application/xml: - schema: - $ref: '#/components/schemas/Pet' - description: Pet object that needs to be added to the store - required: true + $ref: '#/components/requestBodies/Pet' responses: 405: - content: {} description: Invalid input security: - petstore_auth: @@ -44,24 +38,13 @@ paths: put: operationId: updatePet requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/Pet' - application/xml: - schema: - $ref: '#/components/schemas/Pet' - description: Pet object that needs to be added to the store - required: true + $ref: '#/components/requestBodies/Pet' responses: 400: - content: {} description: Invalid ID supplied 404: - content: {} description: Pet not found 405: - content: {} description: Validation exception security: - petstore_auth: @@ -106,7 +89,6 @@ paths: type: array description: successful operation 400: - content: {} description: Invalid status value security: - petstore_auth: @@ -146,7 +128,6 @@ paths: type: array description: successful operation 400: - content: {} description: Invalid tag value security: - petstore_auth: @@ -159,20 +140,24 @@ paths: delete: operationId: deletePet parameters: - - in: header + - explode: false + in: header name: api_key + required: false schema: type: string + style: simple - description: Pet id to delete + explode: false in: path name: petId required: true schema: format: int64 type: integer + style: simple responses: 400: - content: {} description: Invalid pet value security: - petstore_auth: @@ -186,12 +171,14 @@ paths: operationId: getPetById parameters: - description: ID of pet to return + explode: false in: path name: petId required: true schema: format: int64 type: integer + style: simple responses: 200: content: @@ -203,10 +190,8 @@ paths: $ref: '#/components/schemas/Pet' description: successful operation 400: - content: {} description: Invalid ID supplied 404: - content: {} description: Pet not found security: - api_key: [] @@ -217,13 +202,16 @@ paths: operationId: updatePetWithForm parameters: - description: ID of pet that needs to be updated + explode: false in: path name: petId required: true schema: format: int64 type: integer + style: simple requestBody: + $ref: '#/components/requestBodies/inline_object' content: application/x-www-form-urlencoded: schema: @@ -234,9 +222,9 @@ paths: status: description: Updated status of the pet type: string + type: object responses: 405: - content: {} description: Invalid input security: - petstore_auth: @@ -250,13 +238,16 @@ paths: operationId: uploadFile parameters: - description: ID of pet to update + explode: false in: path name: petId required: true schema: format: int64 type: integer + style: simple requestBody: + $ref: '#/components/requestBodies/inline_object_1' content: multipart/form-data: schema: @@ -268,6 +259,7 @@ paths: description: file to upload format: binary type: string + type: object responses: 200: content: @@ -306,7 +298,7 @@ paths: operationId: placeOrder requestBody: content: - '*/*': + application/json: schema: $ref: '#/components/schemas/Order' description: order placed for purchasing the pet @@ -322,7 +314,6 @@ paths: $ref: '#/components/schemas/Order' description: successful operation 400: - content: {} description: Invalid Order summary: Place an order for a pet tags: @@ -334,17 +325,17 @@ paths: operationId: deleteOrder parameters: - description: ID of the order that needs to be deleted + explode: false in: path name: orderId required: true schema: type: string + style: simple responses: 400: - content: {} description: Invalid ID supplied 404: - content: {} description: Order not found summary: Delete purchase order by ID tags: @@ -354,6 +345,7 @@ paths: operationId: getOrderById parameters: - description: ID of pet that needs to be fetched + explode: false in: path name: orderId required: true @@ -362,6 +354,7 @@ paths: maximum: 5 minimum: 1 type: integer + style: simple responses: 200: content: @@ -373,10 +366,8 @@ paths: $ref: '#/components/schemas/Order' description: successful operation 400: - content: {} description: Invalid ID supplied 404: - content: {} description: Order not found summary: Find purchase order by ID tags: @@ -387,14 +378,13 @@ paths: operationId: createUser requestBody: content: - '*/*': + application/json: schema: $ref: '#/components/schemas/User' description: Created user object required: true responses: default: - content: {} description: successful operation summary: Create user tags: @@ -404,17 +394,9 @@ paths: post: operationId: createUsersWithArrayInput requestBody: - content: - '*/*': - schema: - items: - $ref: '#/components/schemas/User' - type: array - description: List of user object - required: true + $ref: '#/components/requestBodies/UserArray' responses: default: - content: {} description: successful operation summary: Creates list of users with given input array tags: @@ -424,17 +406,9 @@ paths: post: operationId: createUsersWithListInput requestBody: - content: - '*/*': - schema: - items: - $ref: '#/components/schemas/User' - type: array - description: List of user object - required: true + $ref: '#/components/requestBodies/UserArray' responses: default: - content: {} description: successful operation summary: Creates list of users with given input array tags: @@ -445,17 +419,21 @@ paths: operationId: loginUser parameters: - description: The user name for login + explode: true in: query name: username required: true schema: type: string + style: form - description: The password for login in clear text + explode: true in: query name: password required: true schema: type: string + style: form responses: 200: content: @@ -469,16 +447,19 @@ paths: headers: X-Rate-Limit: description: calls per hour allowed by the user + explode: false schema: format: int32 type: integer + style: simple X-Expires-After: description: date in UTC when toekn expires + explode: false schema: format: date-time type: string + style: simple 400: - content: {} description: Invalid username/password supplied summary: Logs user into the system tags: @@ -488,7 +469,6 @@ paths: operationId: logoutUser responses: default: - content: {} description: successful operation summary: Logs out current logged in user session tags: @@ -499,17 +479,17 @@ paths: operationId: deleteUser parameters: - description: The name that needs to be deleted + explode: false in: path name: username required: true schema: type: string + style: simple responses: 400: - content: {} description: Invalid username supplied 404: - content: {} description: User not found summary: Delete user tags: @@ -518,11 +498,13 @@ paths: operationId: getUserByName parameters: - description: The name that needs to be fetched. Use user1 for testing. + explode: false in: path name: username required: true schema: type: string + style: simple responses: 200: content: @@ -534,10 +516,8 @@ paths: $ref: '#/components/schemas/User' description: successful operation 400: - content: {} description: Invalid username supplied 404: - content: {} description: User not found summary: Get user by user name tags: @@ -547,30 +527,60 @@ paths: operationId: updateUser parameters: - description: name that need to be deleted + explode: false in: path name: username required: true schema: type: string + style: simple requestBody: content: - '*/*': + application/json: schema: $ref: '#/components/schemas/User' description: Updated user object required: true responses: 400: - content: {} description: Invalid user supplied 404: - content: {} description: User not found summary: Updated user tags: - user x-codegen-request-body-name: body components: + requestBodies: + UserArray: + content: + application/json: + schema: + items: + $ref: '#/components/schemas/User' + type: array + description: List of user object + required: true + Pet: + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + application/xml: + schema: + $ref: '#/components/schemas/Pet' + description: Pet object that needs to be added to the store + required: true + inline_object: + content: + application/x-www-form-urlencoded: + schema: + $ref: '#/components/schemas/inline_object' + inline_object_1: + content: + multipart/form-data: + schema: + $ref: '#/components/schemas/inline_object_1' schemas: Order: description: An order for a pets from the pet store @@ -743,6 +753,25 @@ components: type: string title: An uploaded response type: object + inline_object: + properties: + name: + description: Updated name of the pet + type: string + status: + description: Updated status of the pet + type: string + type: object + inline_object_1: + properties: + additionalMetadata: + description: Additional data to pass to server + type: string + file: + description: file to upload + format: binary + type: string + type: object securitySchemes: petstore_auth: flows: diff --git a/samples/server/petstore/go-api-server/go/model_inline_object.go b/samples/server/petstore/go-api-server/go/model_inline_object.go new file mode 100644 index 000000000000..ee13c18c29aa --- /dev/null +++ b/samples/server/petstore/go-api-server/go/model_inline_object.go @@ -0,0 +1,19 @@ +/* + * 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 + +type InlineObject struct { + + // Updated name of the pet + Name string `json:"name,omitempty"` + + // Updated status of the pet + Status string `json:"status,omitempty"` +} diff --git a/samples/server/petstore/go-api-server/go/model_inline_object_1.go b/samples/server/petstore/go-api-server/go/model_inline_object_1.go new file mode 100644 index 000000000000..a41e0bff7d95 --- /dev/null +++ b/samples/server/petstore/go-api-server/go/model_inline_object_1.go @@ -0,0 +1,23 @@ +/* + * 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 ( + "os" +) + +type InlineObject1 struct { + + // Additional data to pass to server + AdditionalMetadata string `json:"additionalMetadata,omitempty"` + + // file to upload + File **os.File `json:"file,omitempty"` +} diff --git a/samples/server/petstore/go-api-server/main.go b/samples/server/petstore/go-api-server/main.go index 0c8750779476..2e6999c2f480 100644 --- a/samples/server/petstore/go-api-server/main.go +++ b/samples/server/petstore/go-api-server/main.go @@ -18,9 +18,9 @@ import ( // once you place this file into your project. // For example, // - // sw "github.com/myname/myrepo/go" + // sw "github.com/myname/myrepo/" // - sw "./go" + sw "./" ) func main() {