Skip to content

Commit

Permalink
Merge pull request OpenAPITools#44 from CiscoM31/refresh/master
Browse files Browse the repository at this point in the history
pull in golang restrict read-only props changes from upstream
  • Loading branch information
vvb authored Aug 25, 2021
2 parents 3ba948b + 56f32d5 commit 058f491
Show file tree
Hide file tree
Showing 179 changed files with 883 additions and 222 deletions.
1 change: 1 addition & 0 deletions bin/configs/go-petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ additionalProperties:
packageName: petstore
disallowAdditionalPropertiesIfNotPresent: false
generateInterfaces: true
skipReadonlyPropertiesInInt: false
1 change: 1 addition & 0 deletions docs/generators/go-experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sidebar_label: go-experimental
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
|structPrefix|whether to prefix struct with the class name. e.g. DeletePetOpts => PetApiDeletePetOpts| |false|
|useOneOfDiscriminatorLookup|Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and onlye one match in oneOf's schemas) will be skipped.| |false|
|skipReadonlyPropertiesInInt|Skip default values to the readOnly properties in the model init function./ /false/
|withAWSV4Signature|whether to include AWS v4 signature support| |false|
|withGoCodegenComment|whether to include Go codegen comment to disable Go Lint and collapse by default in GitHub 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|
Expand Down
1 change: 1 addition & 0 deletions docs/generators/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|packageName|Go package name (convention: lowercase).| |openapi|
|packageVersion|Go package version.| |1.0.0|
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
|skipReadonlyPropertiesInInit|Skip default values to the readOnly properties in the model init function| |true|
|structPrefix|whether to prefix struct with the class name. e.g. DeletePetOpts => PetApiDeletePetOpts| |false|
|useOneOfDiscriminatorLookup|Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped.| |false|
|withAWSV4Signature|whether to include AWS v4 signature support| |false|
Expand Down
2 changes: 1 addition & 1 deletion docs/generators/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|scmDeveloperConnection|SCM developer connection in generated pom.xml| |scm:git:git@github.com:openapitools/openapi-generator.git|
|scmUrl|SCM URL in generated pom.xml| |https://github.com/openapitools/openapi-generator|
|serializableModel|boolean - toggle "implements Serializable" for generated models| |false|
|serializationLibrary|Serialization library, default depends from the library|<dl><dt>**jsonb**</dt><dd>Use JSON-B as serialization library</dd><dt>**jackson**</dt><dd>Use Jackson as serialization library</dd><dt>**gson**</dt><dd>Use Gson as serialization library</dd></dl>|null|
|serializationLibrary|Serialization library, default depends on value of the option library|<dl><dt>**jsonb**</dt><dd>Use JSON-B as serialization library</dd><dt>**jackson**</dt><dd>Use Jackson as serialization library</dd><dt>**gson**</dt><dd>Use Gson as serialization library</dd></dl>|null|
|snapshotVersion|Uses a SNAPSHOT version.|<dl><dt>**true**</dt><dd>Use a SnapShot Version</dd><dt>**false**</dt><dd>Use a Release Version</dd></dl>|null|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,7 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
"If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.";
public static final String USE_ONEOF_DISCRIMINATOR_LOOKUP = "useOneOfDiscriminatorLookup";
public static final String USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC = "Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped.";

public static final String SKIP_READONLY_PROPERTIES_IN_INIT = "skipReadonlyPropertiesInInit";
public static final String SKIP_READONLY_PROPERTIES_IN_INIT_DESC = "Skip default values to the readOnly properties in the model init function";
}
Original file line number Diff line number Diff line change
Expand Up @@ -3906,9 +3906,9 @@ public CodegenOperation fromOperation(String path,
}
}
op.responses.sort((a, b) -> {
int aDefault = "0".equals(a.code) ? 1 : 0;
int bDefault = "0".equals(b.code) ? 1 : 0;
return aDefault - bDefault;
int aScore = a.isWildcard() ? 2 : a.isRange() ? 1 : 0;
int bScore = b.isWildcard() ? 2 : b.isRange() ? 1 : 0;
return Integer.compare(aScore, bScore);
});

if (methodResponse != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
protected String goImportAlias = "openapiclient";
protected boolean isGoSubmodule = false;
protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup
protected boolean skipReadonlyPropertiesInInt = false;

// A cache to efficiently lookup schema `toModelName()` based on the schema Key
private Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
Expand Down Expand Up @@ -112,6 +113,7 @@ public GoClientCodegen() {
.defaultValue(Boolean.FALSE.toString()));

cliOptions.add(new CliOption(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC).defaultValue("false"));
cliOptions.add(new CliOption(CodegenConstants.SKIP_READONLY_PROPERTIES_IN_INIT, CodegenConstants.SKIP_READONLY_PROPERTIES_IN_INIT_DESC).defaultValue("true"));
// option to change how we process + set the data in the 'additionalProperties' keyword.
CliOption disallowAdditionalPropertiesIfNotPresentOpt = CliOption.newBoolean(
CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT,
Expand Down Expand Up @@ -240,6 +242,12 @@ public void processOpts() {
additionalProperties.put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, getUseOneOfDiscriminatorLookup());
}

if (additionalProperties.containsKey(CodegenConstants.SKIP_READONLY_PROPERTIES_IN_INIT)) {
setSkipReadonlyPropertiesInInit(convertPropertyToBooleanAndWriteBack(CodegenConstants.SKIP_READONLY_PROPERTIES_IN_INIT));
} else {
additionalProperties.put(CodegenConstants.SKIP_READONLY_PROPERTIES_IN_INIT, getSkipReadonlyPropertiesInInit());
}

if (additionalProperties.containsKey(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT)) {
this.setDisallowAdditionalPropertiesIfNotPresent(Boolean.parseBoolean(additionalProperties
.get(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT).toString()));
Expand All @@ -266,6 +274,14 @@ public boolean getUseOneOfDiscriminatorLookup() {
return this.useOneOfDiscriminatorLookup;
}

public void setSkipReadonlyPropertiesInInit(boolean skipReadonlyPropertiesInInt) {
this.skipReadonlyPropertiesInInt = skipReadonlyPropertiesInInt;
}

public boolean getSkipReadonlyPropertiesInInit() {
return this.skipReadonlyPropertiesInInt;
}

public void setGoImportAlias(String goImportAlias) {
this.goImportAlias = goImportAlias;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public JavaClientCodegen() {
cliOptions.add(libraryOption);
setLibrary(OKHTTP_GSON);

CliOption serializationLibrary = new CliOption(CodegenConstants.SERIALIZATION_LIBRARY, "Serialization library, default depends from the library");
CliOption serializationLibrary = new CliOption(CodegenConstants.SERIALIZATION_LIBRARY, "Serialization library, default depends on value of the option library");
Map<String, String> serializationOptions = new HashMap<>();
serializationOptions.put(SERIALIZATION_LIBRARY_GSON, "Use Gson as serialization library");
serializationOptions.put(SERIALIZATION_LIBRARY_JACKSON, "Use Jackson as serialization library");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<String, Object> pathParams, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);
Map<String,Object> uriParams = new HashMap();
Map<String,Object> uriParams = new HashMap<>();
uriParams.putAll(pathParams);
String finalUri = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
String finalUri = builder.build(false).toUriString();
Map<String, Object> uriParams = new HashMap();
Map<String, Object> uriParams = new HashMap<>();
uriParams.putAll(pathParams);
if (queryParams != null && !queryParams.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.http.codec.multipart.Part;
{{/reactive}}
import springfox.documentation.annotations.ApiIgnore;

{{#useBeanValidation}}
import javax.validation.Valid;
Expand Down Expand Up @@ -125,13 +124,13 @@ public interface {{classname}} {
produces = { {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }{{/hasProduces}}{{#hasConsumes}},
consumes = { {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}} }{{/hasConsumes}}{{/singleContentTypes}}
)
{{#jdk8-default-interface}}default {{/jdk8-default-interface}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}},{{/-last}}{{#-last}}{{#reactive}}, {{/reactive}}{{/-last}}{{/allParams}}{{#reactive}}@ApiIgnore final ServerWebExchange exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, @ApiIgnore final org.springframework.data.domain.Pageable pageable{{/vendorExtensions.x-spring-paginated}}){{^jdk8-default-interface}};{{/jdk8-default-interface}}{{#jdk8-default-interface}}{{#unhandledException}} throws Exception{{/unhandledException}} {
{{#jdk8-default-interface}}default {{/jdk8-default-interface}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}},{{/-last}}{{#-last}}{{#reactive}}, {{/reactive}}{{/-last}}{{/allParams}}{{#reactive}}@springfox.documentation.annotations.ApiIgnore final ServerWebExchange exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, @springfox.documentation.annotations.ApiIgnore final org.springframework.data.domain.Pageable pageable{{/vendorExtensions.x-spring-paginated}}){{^jdk8-default-interface}};{{/jdk8-default-interface}}{{#jdk8-default-interface}}{{#unhandledException}} throws Exception{{/unhandledException}} {
{{#delegate-method}}
return {{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, pageable{{/vendorExtensions.x-spring-paginated}});
}

// Override this method
{{#jdk8-default-interface}}default {{/jdk8-default-interface}} {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{^isFile}}{{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{{dataType}}}{{/reactive}}{{#reactive}}{{^isArray}}Mono<{{{dataType}}}>{{/isArray}}{{#isArray}}Flux<{{{baseType}}}>{{/isArray}}{{/reactive}}{{/isBodyParam}}{{/isFile}}{{#isFile}}{{#reactive}}Flux<Part>{{/reactive}}{{^reactive}}MultipartFile{{/reactive}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}@ApiIgnore final ServerWebExchange exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, @ApiIgnore final org.springframework.data.domain.Pageable pageable{{/vendorExtensions.x-spring-paginated}}){{#unhandledException}} throws Exception{{/unhandledException}} {
{{#jdk8-default-interface}}default {{/jdk8-default-interface}} {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{^isFile}}{{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{{dataType}}}{{/reactive}}{{#reactive}}{{^isArray}}Mono<{{{dataType}}}>{{/isArray}}{{#isArray}}Flux<{{{baseType}}}>{{/isArray}}{{/reactive}}{{/isBodyParam}}{{/isFile}}{{#isFile}}{{#reactive}}Flux<Part>{{/reactive}}{{^reactive}}MultipartFile{{/reactive}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}@springfox.documentation.annotations.ApiIgnore final ServerWebExchange exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, @springfox.documentation.annotations.ApiIgnore final org.springframework.data.domain.Pageable pageable{{/vendorExtensions.x-spring-paginated}}){{#unhandledException}} throws Exception{{/unhandledException}} {
{{/delegate-method}}
{{^isDelegate}}
{{>methodBody}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import org.springframework.web.multipart.MultipartFile;
{{#vendorExtensions.x-spring-paginated}}
import org.springframework.data.domain.Pageable;
{{/vendorExtensions.x-spring-paginated}}
import springfox.documentation.annotations.ApiIgnore;

{{#useBeanValidation}}
import javax.validation.constraints.*;
Expand Down Expand Up @@ -114,7 +113,7 @@ public class {{classname}}Controller implements {{classname}} {
{{/externalDocs}}
* @see {{classname}}#{{operationId}}
*/
public {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}},{{/-last}}{{/allParams}}{{#vendorExtensions.x-spring-paginated}}, @ApiIgnore final Pageable pageable{{/vendorExtensions.x-spring-paginated}}) {
public {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}},{{/-last}}{{/allParams}}{{#vendorExtensions.x-spring-paginated}}, @springfox.documentation.annotations.ApiIgnore final Pageable pageable{{/vendorExtensions.x-spring-paginated}}) {
{{^isDelegate}}
{{^async}}
{{>methodBody}}
Expand Down
13 changes: 13 additions & 0 deletions modules/openapi-generator/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,22 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
{{#dataType}}
{{^is1xx}}
{{^is2xx}}
{{#range}}
{{#is3xx}}
if localVarHTTPResponse.StatusCode >= 300 && localVarHTTPResponse.StatusCode < 400 {
{{/is3xx}}
{{#is4xx}}
if localVarHTTPResponse.StatusCode >= 400 && localVarHTTPResponse.StatusCode < 500 {
{{/is4xx}}
{{#is5xx}}
if localVarHTTPResponse.StatusCode >= 500
{{/is5xx}}
{{/range}}
{{^range}}
{{^wildcard}}
if localVarHTTPResponse.StatusCode == {{{code}}} {
{{/wildcard}}
{{/range}}
var v {{{dataType}}}
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func New{{classname}}({{#requiredVars}}{{nameInCamelCase}} {{dataType}}{{^-last}
{{^required}}
{{#defaultValue}}
{{^vendorExtensions.x-golang-is-container}}
{{#skipReadonlyPropertiesInInt}}
{{#isNullable}}
var {{nameInCamelCase}} {{{datatypeWithEnum}}} = {{{.}}}
this.{{name}} = *New{{{dataType}}}(&{{nameInCamelCase}})
Expand All @@ -51,6 +52,19 @@ func New{{classname}}({{#requiredVars}}{{nameInCamelCase}} {{dataType}}{{^-last}
var {{nameInCamelCase}} {{{dataType}}} = {{{.}}}
this.{{name}} = &{{nameInCamelCase}}
{{/isNullable}}
{{/skipReadonlyPropertiesInInt}}
{{^skipReadonlyPropertiesInInt}}
{{^isReadOnly}}
{{#isNullable}}
var {{nameInCamelCase}} {{{datatypeWithEnum}}} = {{{.}}}
this.{{name}} = *New{{{dataType}}}(&{{nameInCamelCase}})
{{/isNullable}}
{{^isNullable}}
var {{nameInCamelCase}} {{{dataType}}} = {{{.}}}
this.{{name}} = &{{nameInCamelCase}}
{{/isNullable}}
{{/isReadOnly}}
{{/skipReadonlyPropertiesInInt}}
{{/vendorExtensions.x-golang-is-container}}
{{/defaultValue}}
{{/required}}
Expand All @@ -66,6 +80,19 @@ func New{{classname}}WithDefaults() *{{classname}} {
{{#vars}}
{{#defaultValue}}
{{^vendorExtensions.x-golang-is-container}}
{{#skipReadonlyPropertiesInInt}}
{{#isNullable}}
{{!we use datatypeWithEnum here, since it will represent the non-nullable name of the datatype, e.g. int64 for NullableInt64}}
var {{nameInCamelCase}} {{{datatypeWithEnum}}} = {{{.}}}
this.{{name}} = *New{{{dataType}}}(&{{nameInCamelCase}})
{{/isNullable}}
{{^isNullable}}
var {{nameInCamelCase}} {{{dataType}}} = {{{.}}}
this.{{name}} = {{^required}}&{{/required}}{{nameInCamelCase}}
{{/isNullable}}
{{/skipReadonlyPropertiesInInt}}
{{^skipReadonlyPropertiesInInt}}
{{^isReadOnly}}
{{#isNullable}}
{{!we use datatypeWithEnum here, since it will represent the non-nullable name of the datatype, e.g. int64 for NullableInt64}}
var {{nameInCamelCase}} {{{datatypeWithEnum}}} = {{{.}}}
Expand All @@ -75,6 +102,8 @@ func New{{classname}}WithDefaults() *{{classname}} {
var {{nameInCamelCase}} {{{dataType}}} = {{{.}}}
this.{{name}} = {{^required}}&{{/required}}{{nameInCamelCase}}
{{/isNullable}}
{{/isReadOnly}}
{{/skipReadonlyPropertiesInInt}}
{{/vendorExtensions.x-golang-is-container}}
{{/defaultValue}}
{{/vars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{>licenseInfo}}

import { Configuration } from "./configuration";
import { RequiredError, RequestArgs } from "./base";
import { RequiredError, RequestArgs } from "./base";
import { AxiosInstance } from 'axios';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ paths:
properties:
string:
$ref: '#/components/schemas/Foo'
4XX:
description: client error
content:
application/json:
schema:
type: object
properties:
string:
$ref: '#/components/schemas/Foo'
404:
description: not found
content:
application/json:
schema:
type: object
properties:
string:
$ref: '#/components/schemas/Foo'
/pet:
servers:
- url: 'http://petstore.swagger.io/v2'
Expand Down Expand Up @@ -1914,3 +1932,30 @@ components:
type: boolean
required:
- lengthCm
ReadOnlyWithDefault:
type: object
properties:
prop1:
type: string
readOnly: true
prop2:
type: string
readOnly: true
default: 'defaultProp2'
prop3:
type: string
default: 'defaultProp3'
boolProp1:
default: false
type: boolean
readOnly: true
boolProp2:
type: boolean
default: true
intProp1:
type: number
default: 100
readOnly: true
intProp2:
type: number
default: 120
Loading

0 comments on commit 058f491

Please sign in to comment.