From a96ad59aabf4d2cb22753079354fa2c783313da1 Mon Sep 17 00:00:00 2001 From: Thibault Duperron Date: Tue, 26 Jan 2021 21:28:52 +0100 Subject: [PATCH 1/3] [Spring] Add version management Must fix #8545 --- .../codegen/languages/SpringCodegen.java | 36 ++++++++++++++++++- .../main/resources/JavaSpring/api.mustache | 9 +++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java index fa8304e3dedd..4cd562dc325c 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java @@ -71,6 +71,9 @@ public class SpringCodegen extends AbstractJavaCodegen public static final String HATEOAS = "hateoas"; public static final String RETURN_SUCCESS_CODE = "returnSuccessCode"; public static final String UNHANDLED_EXCEPTION_HANDLING = "unhandledException"; + public static final String VERSION_TYPE = "versionType"; + public static final String VERSION_TOKEN = "versionToken"; + public static final String VERSION_TOKEN_DEFAULT = "X-VERSION"; public static final String OPEN_BRACE = "{"; public static final String CLOSE_BRACE = "}"; @@ -179,7 +182,13 @@ public SpringCodegen() { CliOption library = new CliOption(CodegenConstants.LIBRARY, CodegenConstants.LIBRARY_DESC).defaultValue(SPRING_BOOT); library.setEnum(supportedLibraries); cliOptions.add(library); - + cliOptions.add(new CliOption(VERSION_TYPE, "Add a new criteria to filter requests handled") + .defaultValue(VersionType.NONE.name()) + .addEnum(VersionType.NONE.name(), "No filter") + .addEnum(VersionType.PATH.name(), "Filter with path prefix") + .addEnum(VersionType.HEADER.name(), "Filter with custom header") + .addEnum(VersionType.QUERY_PARAM.name(), "Filter with custom query-param")); + cliOptions.add(new CliOption(VERSION_TOKEN, "Token used for version type header or queryParam").defaultValue(VERSION_TOKEN_DEFAULT)); } private void updateJava8CliOptions() { @@ -496,6 +505,25 @@ public void processOpts() { additionalProperties.put("lambdaTrimWhitespace", new TrimWhitespaceLambda()); additionalProperties.put("lambdaSplitString", new SplitStringLambda()); + + VersionType versionType = VersionType.valueOf(additionalProperties.getOrDefault(VERSION_TYPE, VersionType.NONE.name()).toString()); + String versionToken = (String) additionalProperties.getOrDefault(VERSION_TOKEN, VERSION_TOKEN_DEFAULT); + switch (versionType) { + case PATH: + additionalProperties.put("versionWithPath", true); + break; + case QUERY_PARAM: + additionalProperties.put("versionWithQueryParam", true); + additionalProperties.put(VERSION_TOKEN, versionToken); + break; + case HEADER: + additionalProperties.put("versionWithHeader", true); + additionalProperties.put(VERSION_TOKEN, versionToken); + break; + case NONE: + default: + break; + } } @Override @@ -890,4 +918,10 @@ public void postProcessParameter(CodegenParameter p) { } } + public enum VersionType { + NONE, + PATH, + QUERY_PARAM, + HEADER + } } diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache index ffcfc878d83e..3db233d91b8d 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache @@ -62,6 +62,9 @@ import java.util.concurrent.{{^jdk8}}Callable{{/jdk8}}{{#jdk8}}CompletableFuture {{#virtualService}} @VirtualService {{/virtualService}} +{{#versionWithPath}} +@RequestMapping("/{{version}}") +{{/versionWithPath}} public interface {{classname}} { {{#jdk8-default-interface}} {{^isDelegate}} @@ -124,6 +127,12 @@ public interface {{classname}} { consumes = "{{{vendorExtensions.x-contentType}}}"{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}}, produces = { {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }{{/hasProduces}}{{#hasConsumes}}, consumes = { {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}} }{{/hasConsumes}}{{/singleContentTypes}} + {{#versionWithHeader}} + ,headers = "{{versionToken}}={{version}}" + {{/versionWithHeader}} + {{#versionWithQueryParam}} + ,params = "{{versionToken}}={{version}}" + {{/versionWithQueryParam}} ) {{#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 Pageable pageable{{/vendorExtensions.x-spring-paginated}}){{^jdk8-default-interface}};{{/jdk8-default-interface}}{{#jdk8-default-interface}}{{#unhandledException}} throws Exception{{/unhandledException}} { {{#delegate-method}} From 00783be04eeac7347299ae22f62561eb61ff42ab Mon Sep 17 00:00:00 2001 From: Thibault Duperron Date: Tue, 26 Jan 2021 22:06:25 +0100 Subject: [PATCH 2/3] Sample --- .../openapi3/client/petstore/python-legacy/dev-requirements.txt | 0 .../openapi3/client/petstore/python-legacy/docs/ApiResponse.md | 0 .../openapi3/client/petstore/python-legacy/docs/Capitalization.md | 0 samples/openapi3/client/petstore/python-legacy/docs/CatAllOf.md | 0 samples/openapi3/client/petstore/python-legacy/docs/ClassModel.md | 0 samples/openapi3/client/petstore/python-legacy/docs/Client.md | 0 samples/openapi3/client/petstore/python-legacy/docs/DogAllOf.md | 0 samples/openapi3/client/petstore/python-legacy/docs/File.md | 0 .../client/petstore/python-legacy/docs/HasOnlyReadOnly.md | 0 .../openapi3/client/petstore/python-legacy/docs/InlineObject.md | 0 .../openapi3/client/petstore/python-legacy/docs/InlineObject4.md | 0 .../client/petstore/python-legacy/docs/InlineResponseDefault.md | 0 samples/openapi3/client/petstore/python-legacy/docs/List.md | 0 .../client/petstore/python-legacy/docs/Model200Response.md | 0 .../openapi3/client/petstore/python-legacy/docs/ModelReturn.md | 0 samples/openapi3/client/petstore/python-legacy/docs/Name.md | 0 samples/openapi3/client/petstore/python-legacy/docs/NumberOnly.md | 0 .../openapi3/client/petstore/python-legacy/docs/OuterComposite.md | 0 samples/openapi3/client/petstore/python-legacy/docs/OuterEnum.md | 0 .../client/petstore/python-legacy/docs/OuterEnumDefaultValue.md | 0 .../client/petstore/python-legacy/docs/OuterEnumInteger.md | 0 .../petstore/python-legacy/docs/OuterEnumIntegerDefaultValue.md | 0 .../openapi3/client/petstore/python-legacy/docs/ReadOnlyFirst.md | 0 .../client/petstore/python-legacy/docs/SpecialModelName.md | 0 samples/openapi3/client/petstore/python-legacy/setup.cfg | 0 samples/openapi3/client/petstore/python-legacy/test_python2.sh | 0 .../openapi3/client/petstore/python-legacy/test_python2_and_3.sh | 0 27 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/dev-requirements.txt mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/ApiResponse.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/Capitalization.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/CatAllOf.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/ClassModel.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/Client.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/DogAllOf.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/File.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/HasOnlyReadOnly.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/InlineObject.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/InlineObject4.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/InlineResponseDefault.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/List.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/Model200Response.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/ModelReturn.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/Name.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/NumberOnly.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/OuterComposite.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/OuterEnum.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/OuterEnumDefaultValue.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/OuterEnumInteger.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/OuterEnumIntegerDefaultValue.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/ReadOnlyFirst.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/docs/SpecialModelName.md mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/setup.cfg mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/test_python2.sh mode change 100755 => 100644 samples/openapi3/client/petstore/python-legacy/test_python2_and_3.sh diff --git a/samples/openapi3/client/petstore/python-legacy/dev-requirements.txt b/samples/openapi3/client/petstore/python-legacy/dev-requirements.txt old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/ApiResponse.md b/samples/openapi3/client/petstore/python-legacy/docs/ApiResponse.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/Capitalization.md b/samples/openapi3/client/petstore/python-legacy/docs/Capitalization.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/CatAllOf.md b/samples/openapi3/client/petstore/python-legacy/docs/CatAllOf.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/ClassModel.md b/samples/openapi3/client/petstore/python-legacy/docs/ClassModel.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/Client.md b/samples/openapi3/client/petstore/python-legacy/docs/Client.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/DogAllOf.md b/samples/openapi3/client/petstore/python-legacy/docs/DogAllOf.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/File.md b/samples/openapi3/client/petstore/python-legacy/docs/File.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/HasOnlyReadOnly.md b/samples/openapi3/client/petstore/python-legacy/docs/HasOnlyReadOnly.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/InlineObject.md b/samples/openapi3/client/petstore/python-legacy/docs/InlineObject.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/InlineObject4.md b/samples/openapi3/client/petstore/python-legacy/docs/InlineObject4.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/InlineResponseDefault.md b/samples/openapi3/client/petstore/python-legacy/docs/InlineResponseDefault.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/List.md b/samples/openapi3/client/petstore/python-legacy/docs/List.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/Model200Response.md b/samples/openapi3/client/petstore/python-legacy/docs/Model200Response.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/ModelReturn.md b/samples/openapi3/client/petstore/python-legacy/docs/ModelReturn.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/Name.md b/samples/openapi3/client/petstore/python-legacy/docs/Name.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/NumberOnly.md b/samples/openapi3/client/petstore/python-legacy/docs/NumberOnly.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/OuterComposite.md b/samples/openapi3/client/petstore/python-legacy/docs/OuterComposite.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/OuterEnum.md b/samples/openapi3/client/petstore/python-legacy/docs/OuterEnum.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/OuterEnumDefaultValue.md b/samples/openapi3/client/petstore/python-legacy/docs/OuterEnumDefaultValue.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/OuterEnumInteger.md b/samples/openapi3/client/petstore/python-legacy/docs/OuterEnumInteger.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/OuterEnumIntegerDefaultValue.md b/samples/openapi3/client/petstore/python-legacy/docs/OuterEnumIntegerDefaultValue.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/ReadOnlyFirst.md b/samples/openapi3/client/petstore/python-legacy/docs/ReadOnlyFirst.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/docs/SpecialModelName.md b/samples/openapi3/client/petstore/python-legacy/docs/SpecialModelName.md old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/setup.cfg b/samples/openapi3/client/petstore/python-legacy/setup.cfg old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/test_python2.sh b/samples/openapi3/client/petstore/python-legacy/test_python2.sh old mode 100755 new mode 100644 diff --git a/samples/openapi3/client/petstore/python-legacy/test_python2_and_3.sh b/samples/openapi3/client/petstore/python-legacy/test_python2_and_3.sh old mode 100755 new mode 100644 From 31cbdb504206a9fcd5d98079735c07dec62ca267 Mon Sep 17 00:00:00 2001 From: Thibault Duperron Date: Wed, 27 Jan 2021 18:19:43 +0100 Subject: [PATCH 3/3] Docs --- docs/generators/spring.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/generators/spring.md b/docs/generators/spring.md index f583724fe2c5..fd8816e2f430 100644 --- a/docs/generators/spring.md +++ b/docs/generators/spring.md @@ -69,6 +69,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl |useBeanValidation|Use BeanValidation API annotations| |true| |useOptional|Use Optional container for optional parameters| |false| |useTags|use tags for creating interface and controller classnames| |false| +|versionToken|Token used for version type header or queryParam| |X-VERSION| +|versionType|Add a new criteria to filter requests handled|
**NONE**
No filter
**PATH**
Filter with path prefix
**HEADER**
Filter with custom header
**QUERY_PARAM**
Filter with custom query-param
|NONE| |virtualService|Generates the virtual service. For more details refer - https://github.com/elan-venture/virtualan/wiki| |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|