diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/ClientOptInput.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/ClientOptInput.java index 57918dd9b35..b3f866a6dd9 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/ClientOptInput.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/ClientOptInput.java @@ -5,13 +5,16 @@ import io.swagger.models.Swagger; import io.swagger.models.auth.AuthorizationValue; +import java.util.Collections; import java.util.List; +import java.util.Map; public class ClientOptInput { private CodegenConfig config; private ClientOpts opts; private Swagger swagger; private List auths; + private Map systemProperties; public ClientOptInput swagger(Swagger swagger) { this.setSwagger(swagger); @@ -28,6 +31,11 @@ public ClientOptInput config(CodegenConfig codegenConfig) { return this; } + public ClientOptInput systemProperties(Map properties) { + this.setSystemProperties(properties); + return this; + } + @Deprecated public ClientOptInput auth(String urlEncodedAuthString) { this.setAuth(urlEncodedAuthString); @@ -65,6 +73,18 @@ public void setOpts(ClientOpts opts) { this.opts = opts; } + public Map getSystemProperties() { + if(systemProperties == null) { + return Collections.emptyMap(); + } else { + return systemProperties; + } + } + + public void setSystemProperties(Map systemProperties) { + this.systemProperties = systemProperties; + } + @ApiModelProperty(dataType = "Object") public Swagger getSwagger() { return swagger; diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java index 6bd8931fc61..886bdb6c352 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java @@ -25,9 +25,9 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { protected ClientOptInput opts; protected Swagger swagger; protected CodegenIgnoreProcessor ignoreProcessor; - private Boolean generateApis = null; - private Boolean generateModels = null; - private Boolean generateSupportingFiles = null; + private boolean generateApis; + private boolean generateModels; + private boolean generateSupportingFiles; private Boolean generateApiTests = null; private Boolean generateApiDocumentation = null; private Boolean generateModelTests = null; @@ -86,33 +86,56 @@ private String getHost(){ return hostBuilder.toString(); } + /** + * Checks whether a system property is set. + * "System property" here means a value in the systemProperty map of {@code opts}. + * + * @param name the name of the property. + * @return {@code true} if the property is set, otherwise false. + */ + private boolean isSystemPropertySet(String name) { + return opts.getSystemProperties().get(name) != null; + } + + /** + * Returns the value of the "system property" as a boolean, with default {@code true}. + * "System property" here means a value in the systemProperty map of {@code opts}. + * + * @param name The name of the system property. + * @return {@code true} if the property is not set, or if the value is (case-insensitive) "true", otherwise {@code false}. + */ + private boolean getSystemPropertyAsBoolean(String name) { + String value = opts.getSystemProperties().get(name); + return value == null || Boolean.valueOf(value); + } + + private Set getSystemPropertyAsStringSet(String name) { + String value = opts.getSystemProperties().get(name); + if (value != null && !value.isEmpty()) { + return new HashSet(Arrays.asList(value.split(","))); + } else { + return null; + } + } + private void configureGeneratorProperties() { // allows generating only models by specifying a CSV of models to generate, or empty for all - generateApis = System.getProperty("apis") != null ? true:null; - generateModels = System.getProperty("models") != null ? true: null; - generateSupportingFiles = System.getProperty("supportingFiles") != null ? true:null; - if (generateApis == null && generateModels == null && generateSupportingFiles == null) { + generateApis = isSystemPropertySet("apis"); + generateModels = isSystemPropertySet("models"); + generateSupportingFiles = isSystemPropertySet("supportingFiles"); + + if (!generateApis && !generateModels && !generateSupportingFiles) { // no specifics are set, generate everything generateApis = generateModels = generateSupportingFiles = true; - } else { - if(generateApis == null) { - generateApis = false; - } - if(generateModels == null) { - generateModels = false; - } - if(generateSupportingFiles == null) { - generateSupportingFiles = false; - } } // model/api tests and documentation options rely on parent generate options (api or model) and no other options. // They default to true in all scenarios and can only be marked false explicitly - generateModelTests = System.getProperty("modelTests") != null ? Boolean.valueOf(System.getProperty("modelTests")): true; - generateModelDocumentation = System.getProperty("modelDocs") != null ? Boolean.valueOf(System.getProperty("modelDocs")):true; - generateApiTests = System.getProperty("apiTests") != null ? Boolean.valueOf(System.getProperty("apiTests")): true; - generateApiDocumentation = System.getProperty("apiDocs") != null ? Boolean.valueOf(System.getProperty("apiDocs")):true; + generateModelTests = getSystemPropertyAsBoolean("modelTests"); + generateModelDocumentation = getSystemPropertyAsBoolean("modelDocs"); + generateApiTests = getSystemPropertyAsBoolean("apiTests"); + generateApiDocumentation = getSystemPropertyAsBoolean("apiDocs"); // Additional properties added for tests to exclude references in project related files @@ -121,7 +144,7 @@ private void configureGeneratorProperties() { if(!generateApiTests && !generateModelTests) { config.additionalProperties().put(CodegenConstants.EXCLUDE_TESTS, true); } - if (System.getProperty("debugSwagger") != null) { + if (isSystemPropertySet("debugSwagger")) { Json.prettyPrint(swagger); } config.processOpts(); @@ -227,11 +250,7 @@ private void generateModels(List files, List allModels) { return; } - String modelNames = System.getProperty("models"); - Set modelsToGenerate = null; - if(modelNames != null && !modelNames.isEmpty()) { - modelsToGenerate = new HashSet(Arrays.asList(modelNames.split(","))); - } + Set modelsToGenerate = getSystemPropertyAsStringSet("models"); Set modelKeys = definitions.keySet(); if(modelsToGenerate != null && !modelsToGenerate.isEmpty()) { @@ -342,7 +361,7 @@ private Model getParent(Model model) { throw new RuntimeException("Could not generate model '" + modelName + "'", e); } } - if (System.getProperty("debugModels") != null) { + if (isSystemPropertySet("debugModels")) { LOGGER.info("############ Model info ############"); Json.prettyPrint(allModels); } @@ -354,11 +373,8 @@ private void generateApis(List files, List allOperations) { return; } Map> paths = processPaths(swagger.getPaths()); - Set apisToGenerate = null; - String apiNames = System.getProperty("apis"); - if(apiNames != null && !apiNames.isEmpty()) { - apisToGenerate = new HashSet(Arrays.asList(apiNames.split(","))); - } + Set apisToGenerate = getSystemPropertyAsStringSet("apis"); + if(apisToGenerate != null && !apisToGenerate.isEmpty()) { Map> updatedPaths = new TreeMap>(); for(String m : paths.keySet()) { @@ -463,7 +479,7 @@ public int compare(CodegenOperation one, CodegenOperation another) { throw new RuntimeException("Could not generate api file for '" + tag + "'", e); } } - if (System.getProperty("debugOperations") != null) { + if (isSystemPropertySet("debugOperations")) { LOGGER.info("############ Operation info ############"); Json.prettyPrint(allOperations); } @@ -474,11 +490,7 @@ private void generateSupportingFiles(List files, Map bundl if (!generateSupportingFiles) { return; } - Set supportingFilesToGenerate = null; - String supportingFiles = System.getProperty("supportingFiles"); - if(supportingFiles!= null && !supportingFiles.isEmpty()) { - supportingFilesToGenerate = new HashSet(Arrays.asList(supportingFiles.split(","))); - } + Set supportingFilesToGenerate = getSystemPropertyAsStringSet("supportingFiles"); for (SupportingFile support : config.supportingFiles()) { try { @@ -637,7 +649,7 @@ public int compare(CodegenSecurity one, CodegenSecurity another) { config.postProcessSupportingFileData(bundle); - if (System.getProperty("debugSupportingFiles") != null) { + if(isSystemPropertySet("debugSupportingFiles")) { LOGGER.info("############ Supporting file info ############"); Json.prettyPrint(bundle); } @@ -739,7 +751,7 @@ private void processOperation(String resourcePath, String httpMethod, Operation if (operation == null) { return; } - if (System.getProperty("debugOperations") != null) { + if (isSystemPropertySet("debugOperations")) { LOGGER.info("processOperation: resourcePath= " + resourcePath + "\t;" + httpMethod + " " + operation + "\n"); } List tags = operation.getTags(); @@ -751,7 +763,7 @@ private void processOperation(String resourcePath, String httpMethod, Operation build up a set of parameter "ids" defined at the operation level per the swagger 2.0 spec "A unique parameter is defined by a combination of a name and location" i'm assuming "location" == "in" - */ + */ Set operationParameters = new HashSet(); if (operation.getParameters() != null) { for (Parameter parameter : operation.getParameters()) { diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/config/CodegenConfigurator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/config/CodegenConfigurator.java index 86a7238cf04..ae6ae6a1ba8 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/config/CodegenConfigurator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/config/CodegenConfigurator.java @@ -412,14 +412,14 @@ public ClientOptInput toClientOptInput() { config.additionalProperties().putAll(additionalProperties); - ClientOptInput input = new ClientOptInput() - .config(config); - final List authorizationValues = AuthParser.parse(auth); Swagger swagger = new SwaggerParser().read(inputSpec, authorizationValues, true); - input.opts(new ClientOpts()) + ClientOptInput input = new ClientOptInput() + .config(config) + .opts(new ClientOpts()) + .systemProperties(systemProperties) .swagger(swagger); return input; diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/statichtml/StaticHtmlTagsTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/statichtml/StaticHtmlTagsTest.java index 0ba3d9e2fee..a8b2d5c2bca 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/statichtml/StaticHtmlTagsTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/statichtml/StaticHtmlTagsTest.java @@ -1,8 +1,9 @@ package io.swagger.codegen.statichtml; +import static java.util.Collections.singletonMap; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; -import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -44,7 +45,7 @@ public void setUp() throws Exception { public void tearDown() throws Exception { folder.delete(); } - + @Test public void testApiTags() throws Exception { final Swagger swagger = new SwaggerParser().read("src/test/resources/2_0/petstore.json"); @@ -52,10 +53,10 @@ public void testApiTags() throws Exception { final int maxTagsToTest = 2; // how to flip it randomly from 2 to 1, and shuffle ops? // if an op has a few tags it will be duplicated here, but it's exactly what we expect in doc final List expectedOperations = new ArrayList(); - + final String capitalCommatizedTags = pickupFewTagsAndOps(swagger, maxTagsToTest, expectedOperations); - + final Collection seenOperations = new ArrayList(); CodegenConfig codegenConfig = new StaticHtmlGenerator(){ // new StaticDocCodegen(){ public Map postProcessSupportingFileData(Map objs) { @@ -65,29 +66,25 @@ public Map postProcessSupportingFileData(Map ob assertEquals(actualOperations.size(), expectedOperations.size(), "Expectig the same size of ops for -Dapis="+capitalCommatizedTags + " in fact, actual "+actualOperations+" doesn't seem like expecting " - + expectedOperations); + + expectedOperations); return objs; } }; codegenConfig.setOutputDir(folder.getRoot().getAbsolutePath()); - - ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).swagger(swagger) - .config(codegenConfig); - - final String apisBackup = System.setProperty("apis", capitalCommatizedTags); - try { - DefaultGenerator gen = new DefaultGenerator(); - gen.opts(clientOptInput); - gen.generate(); - assertEquals(seenOperations.isEmpty(), false, - "something has been changed in code and now code bypass the mock above..."); - } finally { - if (apisBackup!=null) { - System.setProperty("apis", apisBackup); - }else{ - System.clearProperty("apis"); - } - } + + final ClientOptInput clientOptInput = + new ClientOptInput() + .opts(new ClientOpts()) + .swagger(swagger) + .config(codegenConfig) + .systemProperties(singletonMap("apis", capitalCommatizedTags)); + + + new DefaultGenerator() + .opts(clientOptInput) + .generate(); + assertFalse(seenOperations.isEmpty(), + "something has been changed in code and now code bypass the mock above..."); } protected String pickupFewTagsAndOps(final Swagger swagger, @@ -107,19 +104,19 @@ protected String pickupFewTagsAndOps(final Swagger swagger, } } } - + final String capitalCommatizedTags = StringUtils.join( Lists.transform(Lists.newArrayList(expectedTags), new Function() { - @Nullable - @Override - public String apply(final String input) { - return StringUtils.capitalize(input); - } - }), ","); + @Nullable + @Override + public String apply(final String input) { + return StringUtils.capitalize(input); + } + }), ","); return capitalCommatizedTags; } - + @SuppressWarnings({ "rawtypes", "unchecked" }) protected static Collection getOperations(Map objs) { final ArrayList rez = new ArrayList(); @@ -127,7 +124,7 @@ protected static Collection getOperations(Map objs) { for(Object apiElem : ((List)apiInfo.get("apis"))){ Map api = (Map) apiElem; rez.addAll( (Collection) // what if the same op goes on two tags?? - ((Map)api.get("operations")).get("operation")); + ((Map)api.get("operations")).get("operation")); } return rez; }