Skip to content

[Spring] Add version management in controllers #8546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/generators/spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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|<dl><dt>**NONE**</dt><dd>No filter</dd><dt>**PATH**</dt><dd>Filter with path prefix</dd><dt>**HEADER**</dt><dd>Filter with custom header</dd><dt>**QUERY_PARAM**</dt><dd>Filter with custom query-param</dd></dl>|NONE|
|virtualService|Generates the virtual service. For more details refer - https://github.com/virtualansoftware/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|

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "}";
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -895,4 +923,10 @@ public void postProcessParameter(CodegenParameter p) {
}
}

public enum VersionType {
NONE,
PATH,
QUERY_PARAM,
HEADER
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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}}
Expand Down
Empty file.
Empty file.
Empty file.
Empty file modified samples/openapi3/client/petstore/python-legacy/docs/CatAllOf.md
100755 → 100644
Empty file.
Empty file.
Empty file modified samples/openapi3/client/petstore/python-legacy/docs/Client.md
100755 → 100644
Empty file.
Empty file modified samples/openapi3/client/petstore/python-legacy/docs/DogAllOf.md
100755 → 100644
Empty file.
Empty file modified samples/openapi3/client/petstore/python-legacy/docs/File.md
100755 → 100644
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file modified samples/openapi3/client/petstore/python-legacy/docs/List.md
100755 → 100644
Empty file.
Empty file.
Empty file.
Empty file modified samples/openapi3/client/petstore/python-legacy/docs/Name.md
100755 → 100644
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file modified samples/openapi3/client/petstore/python-legacy/setup.cfg
100755 → 100644
Empty file.
Empty file modified samples/openapi3/client/petstore/python-legacy/test_python2.sh
100755 → 100644
Empty file.
Empty file.