Skip to content

Commit

Permalink
Set parameters allowableValues dynamically (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbornet authored and wing328 committed May 17, 2018
1 parent 2821f18 commit 4889103
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ default ResponseEntity<Resource> downloadFile(@ApiParam(value = "",required=true
@ApiResponse(code = 200, message = "successful operation", response = ResponseCode.class) })
@RequestMapping(value = "/gen/clients/{language}",
method = RequestMethod.POST)
default ResponseEntity<ResponseCode> 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<ResponseCode> 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);
}

Expand All @@ -78,7 +78,7 @@ default ResponseEntity<ResponseCode> generateClient(@ApiParam(value = "The targe
@ApiResponse(code = 200, message = "successful operation", response = ResponseCode.class) })
@RequestMapping(value = "/gen/servers/{framework}",
method = RequestMethod.POST)
default ResponseEntity<ResponseCode> 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<ResponseCode> generateServerForLanguage(@ApiParam(value = "framework",required=true) @PathVariable("framework") String framework,@ApiParam(value = "parameters" ,required=true ) @Valid @RequestBody GeneratorInput generatorInput) {
return getDelegate().generateServerForLanguage(framework, generatorInput);
}

Expand All @@ -89,7 +89,7 @@ default ResponseEntity<ResponseCode> generateServerForLanguage(@ApiParam(value =
@RequestMapping(value = "/gen/clients/{language}",
produces = { "application/json" },
method = RequestMethod.GET)
default ResponseEntity<Map<String, CliOption>> 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<Map<String, CliOption>> getClientOptions(@ApiParam(value = "The target language for the client library",required=true) @PathVariable("language") String language) {
return getDelegate().getClientOptions(language);
}

Expand All @@ -100,7 +100,7 @@ default ResponseEntity<Map<String, CliOption>> getClientOptions(@ApiParam(value
@RequestMapping(value = "/gen/servers/{framework}",
produces = { "application/json" },
method = RequestMethod.GET)
default ResponseEntity<Map<String, CliOption>> 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<Map<String, CliOption>> getServerOptions(@ApiParam(value = "The target language for the server framework",required=true) @PathVariable("framework") String framework) {
return getDelegate().getServerOptions(framework);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> clients = new ArrayList<>();
private static List<String> servers = new ArrayList<>();

static {
List<CodegenConfig> 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;
}
}

0 comments on commit 4889103

Please # to comment.