go-server: Support additional properties as command line arguments (#1555)

* Support additionalPorperties as command line arguments

* Move Readme into main dir

* Update Mustache

* Update sample

* Test coverage

* Move cli options

* Revert options

* Remove tabs

* openapi 3 examples

* Update readme

* serverPort to int

* Move package version cli option to AbstractGoCodegen

* Update samples

* Tab to spaces

* Update docs and sample with xml

* Manual update doc

* Another doc try

* Regenerate go-gin-server doc
This commit is contained in:
Jochen Scheib
2019-03-25 14:19:06 +01:00
committed by William Cheng
parent 5346eb4c34
commit c918d7ad64
14 changed files with 299 additions and 94 deletions

View File

@@ -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|

View File

@@ -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|

View File

@@ -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|

View File

@@ -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()));
}

View File

@@ -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)"));

View File

@@ -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;
}
}

View File

@@ -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

View File

@@ -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() {

View File

@@ -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

View File

@@ -1,6 +1,6 @@
FROM golang:1.10 AS build
WORKDIR /go/src
COPY go ./go
COPY ./
COPY main.go .
ENV CGO_ENABLED=0

View File

@@ -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:

View File

@@ -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"`
}

View File

@@ -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"`
}

View File

@@ -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() {