From 4889103621cb0942a3cb3df3bc39c237fa02d587 Mon Sep 17 00:00:00 2001 From: Christophe Bornet Date: Thu, 17 May 2018 16:53:01 +0000 Subject: [PATCH] Set parameters allowableValues dynamically (#65) --- .../codegen/online/api/GenApi.java | 8 +-- .../ParameterAllowableValuesPlugin.java | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/configuration/ParameterAllowableValuesPlugin.java diff --git a/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/api/GenApi.java b/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/api/GenApi.java index a2c25535b027..ccebc4133786 100644 --- a/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/api/GenApi.java +++ b/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/api/GenApi.java @@ -68,7 +68,7 @@ default ResponseEntity downloadFile(@ApiParam(value = "",required=true @ApiResponse(code = 200, message = "successful operation", response = ResponseCode.class) }) @RequestMapping(value = "/gen/clients/{language}", method = RequestMethod.POST) - default ResponseEntity generateClient(@ApiParam(value = "The target language for the client library",required=true, allowableValues = "ada, akka-scala, android, apex, bash, clojure, cpprest, csharp, csharp-dotnet2, cwiki, dart, dynamic-html, eiffel, elixir, elm, erlang-client, flash, go, groovy, haskell-http-client, html, html2, java, javascript, javascript-closure-angular, jaxrs-cxf-client, jmeter, kotlin, lua, objc, perl, php, powershell, python, qt5cpp, r, ruby, rust, scala, scalaz, openapi, openapi-yaml, swift, swift3, swift4, tizen, typescript-angular, typescript-angularjs, typescript-aurelia, typescript-fetch, typescript-jquery, typescript-node") @PathVariable("language") String language,@ApiParam(value = "Configuration for building the client library" ,required=true ) @Valid @RequestBody GeneratorInput generatorInput) { + default ResponseEntity generateClient(@ApiParam(value = "The target language for the client library",required=true) @PathVariable("language") String language,@ApiParam(value = "Configuration for building the client library" ,required=true ) @Valid @RequestBody GeneratorInput generatorInput) { return getDelegate().generateClient(language, generatorInput); } @@ -78,7 +78,7 @@ default ResponseEntity generateClient(@ApiParam(value = "The targe @ApiResponse(code = 200, message = "successful operation", response = ResponseCode.class) }) @RequestMapping(value = "/gen/servers/{framework}", method = RequestMethod.POST) - default ResponseEntity generateServerForLanguage(@ApiParam(value = "framework",required=true, allowableValues = "ada-server, aspnetcore, erlang-server, finch, go-server, haskell, inflector, java-pkmst, java-play-framework, java-vertx, jaxrs, jaxrs-cxf, jaxrs-cxf-cdi, jaxrs-resteasy, jaxrs-resteasy-eap, jaxrs-spec, lumen, msf4j, nancyfx, nodejs-server, php-silex, php-symfony, pistache-server, python-flask, rails5, restbed, rust-server, scala-lagom-server, scalatra, sinatra, slim, spring, undertow, ze-ph") @PathVariable("framework") String framework,@ApiParam(value = "parameters" ,required=true ) @Valid @RequestBody GeneratorInput generatorInput) { + default ResponseEntity generateServerForLanguage(@ApiParam(value = "framework",required=true) @PathVariable("framework") String framework,@ApiParam(value = "parameters" ,required=true ) @Valid @RequestBody GeneratorInput generatorInput) { return getDelegate().generateServerForLanguage(framework, generatorInput); } @@ -89,7 +89,7 @@ default ResponseEntity generateServerForLanguage(@ApiParam(value = @RequestMapping(value = "/gen/clients/{language}", produces = { "application/json" }, method = RequestMethod.GET) - default ResponseEntity> getClientOptions(@ApiParam(value = "The target language for the client library",required=true, allowableValues = "ada, akka-scala, android, apex, bash, clojure, cpprest, csharp, csharp-dotnet2, cwiki, dart, dynamic-html, eiffel, elixir, elm, erlang-client, flash, go, groovy, haskell-http-client, html, html2, java, javascript, javascript-closure-angular, jaxrs-cxf-client, jmeter, kotlin, lua, objc, perl, php, powershell, python, qt5cpp, r, ruby, rust, scala, scalaz, openapi, openapi-yaml, swift, swift3, swift4, tizen, typescript-angular, typescript-angularjs, typescript-aurelia, typescript-fetch, typescript-jquery, typescript-node") @PathVariable("language") String language) { + default ResponseEntity> getClientOptions(@ApiParam(value = "The target language for the client library",required=true) @PathVariable("language") String language) { return getDelegate().getClientOptions(language); } @@ -100,7 +100,7 @@ default ResponseEntity> getClientOptions(@ApiParam(value @RequestMapping(value = "/gen/servers/{framework}", produces = { "application/json" }, method = RequestMethod.GET) - default ResponseEntity> getServerOptions(@ApiParam(value = "The target language for the server framework",required=true, allowableValues = "ada-server, aspnetcore, erlang-server, finch, go-server, haskell, inflector, java-pkmst, java-play-framework, java-vertx, jaxrs, jaxrs-cxf, jaxrs-cxf-cdi, jaxrs-resteasy, jaxrs-resteasy-eap, jaxrs-spec, lumen, msf4j, nancyfx, nodejs-server, php-silex, php-symfony, pistache-server, python-flask, rails5, restbed, rust-server, scala-lagom-server, scalatra, sinatra, slim, spring, undertow, ze-ph") @PathVariable("framework") String framework) { + default ResponseEntity> getServerOptions(@ApiParam(value = "The target language for the server framework",required=true) @PathVariable("framework") String framework) { return getDelegate().getServerOptions(framework); } diff --git a/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/configuration/ParameterAllowableValuesPlugin.java b/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/configuration/ParameterAllowableValuesPlugin.java new file mode 100644 index 000000000000..00014f5ab9b5 --- /dev/null +++ b/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/configuration/ParameterAllowableValuesPlugin.java @@ -0,0 +1,54 @@ +package org.openapitools.codegen.online.configuration; + +import org.openapitools.codegen.CodegenConfig; +import org.openapitools.codegen.CodegenConfigLoader; +import org.openapitools.codegen.CodegenType; +import org.springframework.stereotype.Component; +import springfox.documentation.service.AllowableListValues; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spi.service.ParameterBuilderPlugin; +import springfox.documentation.spi.service.contexts.ParameterContext; + +import java.util.ArrayList; +import java.util.List; + +@Component +public class ParameterAllowableValuesPlugin implements ParameterBuilderPlugin { + + private static List clients = new ArrayList<>(); + private static List servers = new ArrayList<>(); + + static { + List extensions = CodegenConfigLoader.getAll(); + for (CodegenConfig config : extensions) { + if (config.getTag().equals(CodegenType.CLIENT) + || config.getTag().equals(CodegenType.DOCUMENTATION)) { + clients.add(config.getName()); + } else if (config.getTag().equals(CodegenType.SERVER)) { + servers.add(config.getName()); + } + } + + clients.sort(String.CASE_INSENSITIVE_ORDER); + servers.sort(String.CASE_INSENSITIVE_ORDER); + } + + @Override + public void apply(ParameterContext parameterContext) { + String name = parameterContext.getOperationContext().getName(); + switch (name) { + case "getClientOptions": + case "generateClient": + parameterContext.parameterBuilder().allowableValues(new AllowableListValues(clients, "string")); + break; + case "getServerOptions": + case "generateServerForLanguage": + parameterContext.parameterBuilder().allowableValues(new AllowableListValues(servers, "string")); + } + } + + @Override + public boolean supports(DocumentationType documentationType) { + return true; + } +}