diff --git a/modules/swagger-codegen-cli/pom.xml b/modules/swagger-codegen-cli/pom.xml index 13f7f5c0152..5193e39531a 100644 --- a/modules/swagger-codegen-cli/pom.xml +++ b/modules/swagger-codegen-cli/pom.xml @@ -70,33 +70,34 @@ swagger-codegen ${project.version} - io.airlift airline 0.7 - com.googlecode.lambdaj lambdaj 2.3.3 - org.slf4j slf4j-simple ${slf4j-version} - - junit - junit - ${junit-version} + org.testng + testng + ${testng-version} + test + + + org.jmockit + jmockit + ${jmockit-version} test - \ No newline at end of file diff --git a/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/ConfigHelp.java b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/ConfigHelp.java index 78e4b9bd6ba..f7a128613b9 100644 --- a/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/ConfigHelp.java +++ b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/ConfigHelp.java @@ -4,10 +4,7 @@ import io.airlift.airline.Command; import io.airlift.airline.Option; import io.swagger.codegen.CliOption; import io.swagger.codegen.CodegenConfig; - -import java.util.ServiceLoader; - -import static java.util.ServiceLoader.load; +import io.swagger.codegen.CodegenConfigLoader; @Command(name = "config-help", description = "Config help for chosen lang") public class ConfigHelp implements Runnable { @@ -16,32 +13,10 @@ public class ConfigHelp implements Runnable { description = "language to get config help for") private String lang; - /** - * Tries to load config class with SPI first, then with class name directly from classpath - * - * @param name name of config, or full qualified class name in classpath - * @return config class - */ - private static CodegenConfig forName(String name) { - ServiceLoader loader = load(CodegenConfig.class); - for (CodegenConfig config : loader) { - if (config.getName().equals(name)) { - return config; - } - } - - // else try to load directly - try { - return (CodegenConfig) Class.forName(name).newInstance(); - } catch (Exception e) { - throw new RuntimeException("Can't load config class with name ".concat(name), e); - } - } - @Override public void run() { System.out.println(); - CodegenConfig config = forName(lang); + CodegenConfig config = CodegenConfigLoader.forName(lang); System.out.println("CONFIG OPTIONS"); for (CliOption langCliOption : config.cliOptions()) { System.out.println("\t" + langCliOption.getOpt()); diff --git a/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Generate.java b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Generate.java index 74b445c57ff..d6790a6aa8f 100644 --- a/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Generate.java +++ b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Generate.java @@ -8,16 +8,21 @@ import io.swagger.codegen.CliOption; import io.swagger.codegen.ClientOptInput; import io.swagger.codegen.ClientOpts; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConfigLoader; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.DefaultGenerator; +import io.swagger.codegen.cmd.utils.OptionUtils; import io.swagger.models.Swagger; import io.swagger.parser.SwaggerParser; +import org.apache.commons.lang3.tuple.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.util.ServiceLoader; +import java.util.List; +import java.util.Map; +import java.util.Set; -import static java.util.ServiceLoader.load; import static org.apache.commons.lang3.StringUtils.isNotEmpty; /** @@ -31,8 +36,6 @@ public class Generate implements Runnable { public static final Logger LOG = LoggerFactory.getLogger(Generate.class); - public static final String TEMPLATE_DIR_PARAM = "templateDir"; - @Option(name = {"-v", "--verbose"}, description = "verbose mode") private boolean verbose; @@ -70,29 +73,42 @@ public class Generate implements Runnable { "overwritten during the generation.") private boolean skipOverwrite; - /** - * Tries to load config class with SPI first, then with class name directly from classpath - * - * @param name name of config, or full qualified class name in classpath - * @return config class - */ - private static CodegenConfig forName(String name) { - ServiceLoader loader = load(CodegenConfig.class); - String available = ""; - for (CodegenConfig config : loader) { - if (config.getName().equals(name)) { - return config; - } - available = available + config.getName() + "\n"; - } + @Option(name = {"--api-package"}, title = "api package", description = CodegenConstants.API_PACKAGE_DESC) + private String apiPackage; - // else try to load directly - try { - return (CodegenConfig) Class.forName(name).newInstance(); - } catch (Exception e) { - throw new RuntimeException("Can't load config class with name ".concat(name) + "Available: "+available, e); - } - } + @Option(name = {"--model-package"}, title = "model package", description = CodegenConstants.MODEL_PACKAGE_DESC) + private String modelPackage; + + @Option(name = {"--instantiation-types"}, title = "instantiation types", description = "sets instantiation type mappings in the format of type=instantiatedType,type=instantiatedType." + + "For example (in Java): array=ArrayList,map=HashMap. In other words array types will get instantiated as ArrayList in generated code.") + private String instantiationTypes; + + @Option(name = {"--type-mappings"}, title = "type mappings", description = "sets mappings between swagger spec types and generated code types " + + "in the format of swaggerType=generatedType,swaggerType=generatedType. For example: array=List,map=Map,string=String") + private String typeMappings; + + @Option(name = {"--additional-properties"}, title = "additional properties", description = "sets additional properties that can be referenced by the mustache templates in the format of name=value,name=value") + private String additionalProperties; + + @Option(name = {"--language-specific-primitives"}, title = "language specific primitives", + description = "specifies additional language specific primitive types in the format of type1,type2,type3,type3. For example: String,boolean,Boolean,Double") + private String languageSpecificPrimitives; + + @Option(name = {"--import-mappings"}, title = "import mappings", + description = "specifies mappings between a given class and the import that should be used for that class in the format of type=import,type=import") + private String importMappings; + + @Option(name = {"--invoker-package"}, title = "invoker package", description = CodegenConstants.INVOKER_PACKAGE_DESC) + private String invokerPackage; + + @Option(name = {"--group-id"}, title = "group id", description = CodegenConstants.GROUP_ID_DESC) + private String groupId; + + @Option(name = {"--artifact-id"}, title = "artifact id", description = CodegenConstants.ARTIFACT_ID_DESC) + private String artifactId; + + @Option(name = {"--artifact-version"}, title = "artifact version", description = CodegenConstants.ARTIFACT_VERSION_DESC) + private String artifactVersion; @Override public void run() { @@ -100,19 +116,30 @@ public class Generate implements Runnable { setSystemProperties(); - ClientOptInput input = new ClientOptInput(); + CodegenConfig config = CodegenConfigLoader.forName(lang); - if (isNotEmpty(auth)) { - input.setAuth(auth); - } - - CodegenConfig config = forName(lang); config.setOutputDir(new File(output).getAbsolutePath()); + config.setSkipOverwrite(skipOverwrite); - if (null != templateDir) { - config.additionalProperties().put(TEMPLATE_DIR_PARAM, new File(templateDir).getAbsolutePath()); + putKeyValuePairsInMap(config.instantiationTypes(), instantiationTypes); + putKeyValuePairsInMap(config.typeMapping(), typeMappings); + putKeyValuePairsInMap(config.additionalProperties(), additionalProperties); + putKeyValuePairsInMap(config.importMapping(), importMappings); + + addValuesToSet(config.languageSpecificPrimitives(), languageSpecificPrimitives); + + checkAndSetAdditionalProperty(config, apiPackage, CodegenConstants.API_PACKAGE); + checkAndSetAdditionalProperty(config, modelPackage, CodegenConstants.MODEL_PACKAGE); + + if(isNotEmpty(templateDir)) { + config.additionalProperties().put(CodegenConstants.TEMPLATE_DIR, new File(templateDir).getAbsolutePath()); } + checkAndSetAdditionalProperty(config, invokerPackage, CodegenConstants.INVOKER_PACKAGE); + checkAndSetAdditionalProperty(config, groupId, CodegenConstants.GROUP_ID); + checkAndSetAdditionalProperty(config, artifactId, CodegenConstants.ARTIFACT_ID); + checkAndSetAdditionalProperty(config, artifactVersion, CodegenConstants.ARTIFACT_VERSION); + if (null != configFile) { Config genConfig = ConfigParser.read(configFile); if (null != genConfig) { @@ -129,24 +156,52 @@ public class Generate implements Runnable { } } - config.setSkipOverwrite(skipOverwrite); - input.setConfig(config); + ClientOptInput input = new ClientOptInput().config(config); + + if (isNotEmpty(auth)) { + input.setAuth(auth); + } Swagger swagger = new SwaggerParser().read(spec, input.getAuthorizationValues(), true); new DefaultGenerator().opts(input.opts(new ClientOpts()).swagger(swagger)).generate(); } - private void setSystemProperties() { - if (systemProperties != null && systemProperties.length() > 0) { - for (String property : systemProperties.split(",")) { - int ix = property.indexOf('='); - if (ix > 0 && ix < property.length() - 1) { - System.setProperty(property.substring(0, ix), property.substring(ix + 1)); - } - } + private void addValuesToSet(Set set, String csvProperty) { + final List values = OptionUtils.splitCommaSeparatedList(csvProperty); + + for (String value : values) { + set.add(value); } } + private void checkAndSetAdditionalProperty(CodegenConfig config, String property, String propertyKey) { + checkAndSetAdditionalProperty(config, property, property, propertyKey); + } + + private void checkAndSetAdditionalProperty(CodegenConfig config, String property, String valueToSet, String propertyKey) { + if(isNotEmpty(property)) { + config.additionalProperties().put(propertyKey, valueToSet); + } + } + + private void setSystemProperties() { + + final List> systemPropertyPairs = OptionUtils.parseCommaSeparatedTuples(systemProperties); + + for (Pair pair : systemPropertyPairs) { + System.setProperty(pair.getLeft(), pair.getRight()); + } + } + + private void putKeyValuePairsInMap(Map map, String commaSeparatedKVPairs) { + final List> pairs = OptionUtils.parseCommaSeparatedTuples(commaSeparatedKVPairs); + + for (Pair pair : pairs) { + map.put(pair.getLeft(), pair.getRight()); + } + } + + /** * If true parameter, adds system properties which enables debug mode in generator * diff --git a/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/utils/OptionUtils.java b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/utils/OptionUtils.java new file mode 100644 index 00000000000..34d56727412 --- /dev/null +++ b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/utils/OptionUtils.java @@ -0,0 +1,43 @@ +package io.swagger.codegen.cmd.utils; + +import org.apache.commons.lang3.tuple.Pair; + +import java.util.ArrayList; +import java.util.List; + +import static org.apache.commons.lang3.StringUtils.isNotEmpty; + +public class OptionUtils { + + public static List> parseCommaSeparatedTuples(String input) { + + List> results = new ArrayList>(); + + final List tuples = splitCommaSeparatedList(input); + + for (String tuple : tuples) { + int ix = tuple.indexOf('='); + if (ix > 0 && ix < tuple.length() - 1) { + final Pair pair = Pair.of(tuple.substring(0, ix), tuple.substring(ix + 1)); + results.add(pair); + } + } + + return results; + } + + public static List splitCommaSeparatedList(String input) { + + List results = new ArrayList(); + + if(input != null && !input.isEmpty()) { + for (String value : input.split(",")) { + if(isNotEmpty(value)) + results.add(value); + } + } + + return results; + } + +} diff --git a/modules/swagger-codegen-cli/src/test/java/io/swagger/codegen/cmd/GenerateTest.java b/modules/swagger-codegen-cli/src/test/java/io/swagger/codegen/cmd/GenerateTest.java new file mode 100644 index 00000000000..94a397df700 --- /dev/null +++ b/modules/swagger-codegen-cli/src/test/java/io/swagger/codegen/cmd/GenerateTest.java @@ -0,0 +1,352 @@ +package io.swagger.codegen.cmd; + +import config.Config; +import config.ConfigParser; +import io.swagger.codegen.ClientOptInput; +import io.swagger.codegen.ClientOpts; +import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; +import io.swagger.codegen.DefaultGenerator; +import io.swagger.codegen.SwaggerCodegen; +import io.swagger.codegen.CodegenConfigLoader; +import io.swagger.codegen.languages.JavaClientCodegen; +import io.swagger.models.Swagger; +import io.swagger.models.auth.AuthorizationValue; +import io.swagger.parser.SwaggerParser; +import mockit.Expectations; +import mockit.FullVerifications; +import mockit.Injectable; +import mockit.Mocked; +import mockit.StrictExpectations; +import org.apache.commons.lang3.ArrayUtils; +import org.testng.annotations.Test; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +public class GenerateTest { + + @Mocked + SwaggerParser parser; + + @Injectable + Swagger swagger; + + @Mocked + DefaultGenerator defaultGenerator; + + @Mocked + CodegenConfigLoader codegenConfigLoader; + + @Mocked + ClientOptInput clientOptInput; + + @Injectable + List authorizationValues; + + @Test + public void testVerbose_ShortArg() throws Exception { + doVerboseTest("-v"); + } + + @Test + public void testVerbose_LongArg() throws Exception { + doVerboseTest("--verbose"); + } + + @Test + public void testRequiredArgs_ShortArgs() throws Exception { + doRequiredArgsTest("-l", "-o", "-i"); + } + + @Test + public void testRequiredArgs_LongArgs() throws Exception { + doRequiredArgsTest("--lang", "--output", "--input-spec"); + } + + @Test + public void testTemplateDir() throws Exception { + final String absolutePath = new File("src").getAbsolutePath(); + + doSingleAdditionalPropertyTest("--template-dir", CodegenConstants.TEMPLATE_DIR, "src", absolutePath); + doSingleAdditionalPropertyTest("--template-dir", CodegenConstants.TEMPLATE_DIR, absolutePath, absolutePath); + doSingleAdditionalPropertyTest("-t", CodegenConstants.TEMPLATE_DIR, "src", absolutePath); + doSingleAdditionalPropertyTest("-t", CodegenConstants.TEMPLATE_DIR, absolutePath, absolutePath); + } + + @Test + public void testAuth() throws Exception { + + final String auth = "hello:world"; + + new StrictExpectations() {{ + clientOptInput.setAuth(auth); + times = 1; + }}; + + setupAndRunGenericTest("-a", auth); + + new StrictExpectations() {{ + clientOptInput.setAuth(auth); + times = 1; + }}; + + setupAndRunGenericTest("--auth", auth); + } + + @Test + public void testSystemProperties() throws Exception { + + new StrictExpectations(System.class) {{ + System.setProperty("hello", "world"); + times = 1; + System.setProperty("foo", "bar"); + times = 1; + }}; + + setupAndRunGenericTest("-D", "hello=world,foo=bar"); + } + + @Test + public void testConfig(@Mocked final ConfigParser parser) throws Exception { + + final String configFilePath = "config.json"; + final String invokerPackage = "com.foo.bar.invoker"; + final String groupId = "com.foo.bar"; + Map configMap = new HashMap(); + configMap.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); + configMap.put(CodegenConstants.GROUP_ID, groupId); + final Config config = new Config(configMap); + + final String[] configArgs = {"-c", "--config"}; + + for (String configArg : configArgs) { + new StrictExpectations() {{ + parser.read(configFilePath); + times = 1; + result = config; + + }}; + + final CodegenConfig codegenConfig = setupAndRunGenericTest(configArg, configFilePath); + + assertValueInMap(codegenConfig.additionalProperties(), CodegenConstants.INVOKER_PACKAGE, invokerPackage); + assertValueInMap(codegenConfig.additionalProperties(), CodegenConstants.GROUP_ID, groupId); + } + } + + @Test + public void testSkipOverwrite() throws Exception { + + CodegenConfig codegenConfig1 = setupAndRunGenericTest(); + assertFalse(codegenConfig1.isSkipOverwrite()); + + CodegenConfig codegenConfig2 = setupAndRunGenericTest("-s"); + assertTrue(codegenConfig2.isSkipOverwrite()); + + CodegenConfig codegenConfig3 = setupAndRunGenericTest("--skip-overwrite"); + assertTrue(codegenConfig3.isSkipOverwrite()); + } + + @Test + public void testApiPackage() throws Exception { + doSingleAdditionalPropertyTest("--api-package", CodegenConstants.API_PACKAGE, "io.foo.bar.api"); + } + + @Test + public void testModelPackage() throws Exception { + doSingleAdditionalPropertyTest("--model-package", CodegenConstants.MODEL_PACKAGE, "io.foo.bar.models"); + } + + @Test + public void testInstantiationTypes() throws Exception { + + final CodegenConfig codegenConfig = setupAndRunGenericTest("--instantiation-types", "foo=bar,hello=world"); + + assertValueInMap(codegenConfig.instantiationTypes(), "foo", "bar"); + assertValueInMap(codegenConfig.instantiationTypes(), "hello", "world"); + } + + @Test + public void testTypeMappings() throws Exception { + final CodegenConfig codegenConfig = setupAndRunGenericTest("--type-mappings", "foo=bar,hello=world"); + + assertValueInMap(codegenConfig.typeMapping(), "foo", "bar"); + assertValueInMap(codegenConfig.typeMapping(), "hello", "world"); + } + + @Test + public void testAdditionalProperties() throws Exception { + final CodegenConfig codegenConfig = setupAndRunGenericTest("--additional-properties", "foo=bar,hello=world"); + + assertValueInMap(codegenConfig.additionalProperties(), "foo", "bar"); + assertValueInMap(codegenConfig.additionalProperties(), "hello", "world"); + } + + @Test + public void testLanguageSpecificPrimitives() throws Exception { + final CodegenConfig codegenConfig = setupAndRunGenericTest("--language-specific-primitives", "foo,bar,hello,world"); + + final Set languageSpecificPrimitives = codegenConfig.languageSpecificPrimitives(); + + assertTrue(languageSpecificPrimitives.contains("foo")); + assertTrue(languageSpecificPrimitives.contains("bar")); + assertTrue(languageSpecificPrimitives.contains("hello")); + assertTrue(languageSpecificPrimitives.contains("world")); + } + + @Test + public void testImportMappings() throws Exception { + final CodegenConfig codegenConfig = setupAndRunGenericTest("--import-mappings", "foo=bar,hello=world"); + + assertValueInMap(codegenConfig.importMapping(), "foo", "bar"); + assertValueInMap(codegenConfig.importMapping(), "hello", "world"); + } + + @Test + public void testInvokerPackage() throws Exception { + doSingleAdditionalPropertyTest("--invoker-package", CodegenConstants.INVOKER_PACKAGE, "io.foo.bar.invoker"); + } + + @Test + public void testGroupId() throws Exception { + doSingleAdditionalPropertyTest("--group-id", CodegenConstants.GROUP_ID, "io.foo.bar"); + } + + @Test + public void testArtifactId() throws Exception { + doSingleAdditionalPropertyTest("--artifact-id", CodegenConstants.ARTIFACT_ID, "awesome-api"); + } + + @Test + public void testArtifactVersion() throws Exception { + doSingleAdditionalPropertyTest("--artifact-version", CodegenConstants.ARTIFACT_VERSION, "1.2.3"); + } + + private void doVerboseTest(String verboseFlag) { + new StrictExpectations(System.class) {{ + System.setProperty("debugSwagger", ""); + times = 1; + System.setProperty("debugModels", ""); + times = 1; + System.setProperty("debugOperations", ""); + times = 1; + System.setProperty("debugSupportingFiles", ""); + times = 1; + }}; + + setupAndRunGenericTest(verboseFlag); + } + + private void doRequiredArgsTest(String langFlag, String outputDirFlag, String inputSpecFlag) { + final String spec = "swagger.yaml"; + final String lang = "java"; + final String outputDir = "src/main/java"; + + final String[] args = {"generate", langFlag, lang, outputDirFlag, outputDir, inputSpecFlag, spec}; + + final CodegenConfig config = new JavaClientCodegen(); + + setupStandardExpectations(spec, lang, config); + + SwaggerCodegen.main(args); + + new FullVerifications() {{ + }}; + + assertEquals(config.getOutputDir(), new File(outputDir).getAbsolutePath()); + } + + private void doSingleAdditionalPropertyTest(String cliArg, String additionalPropertyKey, String expectedValue) { + doSingleAdditionalPropertyTest(cliArg, additionalPropertyKey, expectedValue, expectedValue); + } + + private void doSingleAdditionalPropertyTest(String cliArg, String additionalPropertyKey, String cliValue, String additionalPropertyValue) { + + final CodegenConfig config = setupAndRunGenericTest(cliArg, cliValue); + + assertValueInMap(config.additionalProperties(), additionalPropertyKey, additionalPropertyValue); + } + + private CodegenConfig setupAndRunGenericTest(String... additionalParameters) { + + final String spec = "swagger.yaml"; + final String lang = "java"; + + final String[] commonArgs = {"generate", "-l", lang, "-o", "path/to/some/directory", "-i", spec}; + + String[] argsToUse = ArrayUtils.addAll(commonArgs, additionalParameters); + + final CodegenConfig config = new JavaClientCodegen(); + + setupStandardExpectations(spec, lang, config); + + SwaggerCodegen.main(argsToUse); + + new FullVerifications() {{ + }}; + + return config; + } + + private void assertValueInMap(Map map, String propertyKey, String expectedPropertyValue) { + assertTrue(map.containsKey(propertyKey)); + assertEquals(map.get(propertyKey), expectedPropertyValue); + } + + private void setupStandardExpectations(final String spec, final String languageName, final CodegenConfig config) { + + new Expectations() {{ + CodegenConfigLoader.forName(languageName); + times = 1; + result = config; + + new ClientOptInput(); + times = 1; + result = clientOptInput; + + clientOptInput.config(config); + times = 1; + result = clientOptInput; + + new SwaggerParser(); + times = 1; + result = parser; + + clientOptInput.getAuthorizationValues(); + times = 1; + result = authorizationValues; + + parser.read(spec, authorizationValues, true); + times = 1; + result = swagger; + + new DefaultGenerator(); + times = 1; + result = defaultGenerator; + + clientOptInput.opts((ClientOpts) any); + times = 1; + result = clientOptInput; + + clientOptInput.swagger(swagger); + times = 1; + result = clientOptInput; + + defaultGenerator.opts(clientOptInput); + times = 1; + result = defaultGenerator; + + defaultGenerator.generate(); + times = 1; + }}; + } + +} diff --git a/modules/swagger-codegen-cli/src/test/java/io/swagger/codegen/cmd/utils/OptionUtilsTest.java b/modules/swagger-codegen-cli/src/test/java/io/swagger/codegen/cmd/utils/OptionUtilsTest.java new file mode 100644 index 00000000000..a7dc4d7f1cd --- /dev/null +++ b/modules/swagger-codegen-cli/src/test/java/io/swagger/codegen/cmd/utils/OptionUtilsTest.java @@ -0,0 +1,52 @@ +package io.swagger.codegen.cmd.utils; + +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; + +public class OptionUtilsTest { + + @Test + public void splitCommaSeparatedList() throws Exception { + doCommaSeparatedListTest("a,b,c", Arrays.asList("a", "b", "c")); + doCommaSeparatedListTest("a,,c", Arrays.asList("a", "c")); + doCommaSeparatedListTest("", new ArrayList()); + doCommaSeparatedListTest(null, new ArrayList()); + } + + @Test + public void testParseCommaSeparatedTuples() throws Exception { + doTupleListTest("a=1,b=2,c=3", Arrays.asList(Pair.of("a", "1"), Pair.of("b", "2"), Pair.of("c", "3"))); + doTupleListTest("a=1,,c=3", Arrays.asList(Pair.of("a", "1"), Pair.of("c", "3"))); + doTupleListTest("a=1,xyz,c=3", Arrays.asList(Pair.of("a", "1"), Pair.of("c", "3"))); + doTupleListTest("a=1,=,c=3", Arrays.asList(Pair.of("a", "1"), Pair.of("c", "3"))); + doTupleListTest("", new ArrayList>()); + doTupleListTest(null, new ArrayList>()); + } + + private void doTupleListTest(String input, List> expectedResults) { + final List> result = OptionUtils.parseCommaSeparatedTuples(input); + assertNotNull(result); + assertEquals(result.size(), expectedResults.size()); + for (int i = 0; i < expectedResults.size(); i++) { + final Pair actualPair = result.get(i); + final Pair expected = expectedResults.get(i); + assertEquals(actualPair, expected); + } + } + + private void doCommaSeparatedListTest(String csvStr, List expectedResults) { + final List result = OptionUtils.splitCommaSeparatedList(csvStr); + assertNotNull(result); + assertEquals(result.size(), expectedResults.size()); + for (int i = 0; i < expectedResults.size(); i++) { + assertEquals(result.get(i), expectedResults.get(i)); + } + } +} diff --git a/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java b/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java index 41737ca0968..7500fcfeb39 100644 --- a/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java +++ b/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java @@ -20,6 +20,7 @@ import io.swagger.codegen.CliOption; import io.swagger.codegen.ClientOptInput; import io.swagger.codegen.ClientOpts; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConfigLoader; import io.swagger.codegen.DefaultGenerator; import io.swagger.models.Swagger; import io.swagger.parser.SwaggerParser; @@ -121,7 +122,7 @@ public class CodeGenMojo extends AbstractMojo { public void execute() throws MojoExecutionException { Swagger swagger = new SwaggerParser().read(inputSpec); - CodegenConfig config = forName(language); + CodegenConfig config = CodegenConfigLoader.forName(language); config.setOutputDir(output.getAbsolutePath()); if (null != templateDirectory) { @@ -167,20 +168,4 @@ public class CodeGenMojo extends AbstractMojo { project.addCompileSourceRoot(output.toString()); } } - - private CodegenConfig forName(String name) { - ServiceLoader loader = ServiceLoader.load(CodegenConfig.class); - for (CodegenConfig config : loader) { - if (config.getName().equals(name)) { - return config; - } - } - - // else try to load directly - try { - return (CodegenConfig) Class.forName(name).newInstance(); - } catch (Exception e) { - throw new RuntimeException("Can't load config class with name ".concat(name), e); - } - } } diff --git a/modules/swagger-codegen/pom.xml b/modules/swagger-codegen/pom.xml index a392e98bda4..b6caa0a684a 100644 --- a/modules/swagger-codegen/pom.xml +++ b/modules/swagger-codegen/pom.xml @@ -334,6 +334,18 @@ ${scala-version} test + + org.testng + testng + ${testng-version} + test + + + org.reflections + reflections + ${reflections-version} + test + 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 da026c7f040..56c4c66835a 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 @@ -10,7 +10,7 @@ import java.util.ArrayList; import java.util.List; public class ClientOptInput { - protected CodegenConfig config; + private CodegenConfig config; private ClientOpts opts; private Swagger swagger; private List auths; @@ -25,6 +25,11 @@ public class ClientOptInput { return this; } + public ClientOptInput config(CodegenConfig codegenConfig) { + this.setConfig(codegenConfig); + return this; + } + public String getAuth() { if (auths != null) { StringBuilder b = new StringBuilder(); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/Codegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/Codegen.java index c99e04e5494..d4c0707f1a7 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/Codegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/Codegen.java @@ -85,7 +85,7 @@ public class Codegen extends DefaultGenerator { swagger = new SwaggerParser().read(cmd.getOptionValue("i"), clientOptInput.getAuthorizationValues(), true); } if (cmd.hasOption("t")) { - clientOpts.getProperties().put("templateDir", String.valueOf(cmd.getOptionValue("t"))); + clientOpts.getProperties().put(CodegenConstants.TEMPLATE_DIR, String.valueOf(cmd.getOptionValue("t"))); } } catch (Exception e) { usage(options); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java index 9cbf32a154c..1b0e349daca 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java @@ -83,6 +83,8 @@ public interface CodegenConfig { Map modelTemplateFiles(); + Set languageSpecificPrimitives(); + void processSwagger(Swagger swagger); String toApiFilename(String name); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfigLoader.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfigLoader.java new file mode 100644 index 00000000000..45a57e7bb3a --- /dev/null +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfigLoader.java @@ -0,0 +1,34 @@ +package io.swagger.codegen; + +import java.util.ServiceLoader; + +import static java.util.ServiceLoader.load; + +public class CodegenConfigLoader { + /** + * Tries to load config class with SPI first, then with class name directly from classpath + * + * @param name name of config, or full qualified class name in classpath + * @return config class + */ + public static CodegenConfig forName(String name) { + ServiceLoader loader = load(CodegenConfig.class); + + StringBuilder availableConfigs = new StringBuilder(); + + for (CodegenConfig config : loader) { + if (config.getName().equals(name)) { + return config; + } + + availableConfigs.append(config.getName()).append("\n"); + } + + // else try to load directly + try { + return (CodegenConfig) Class.forName(name).newInstance(); + } catch (Exception e) { + throw new RuntimeException("Can't load config class with name ".concat(name) + " Available: " + availableConfigs.toString(), e); + } + } +} diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConstants.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConstants.java new file mode 100644 index 00000000000..c4bdad9c6ea --- /dev/null +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConstants.java @@ -0,0 +1,36 @@ +package io.swagger.codegen; + +/** + * A class for storing constants that are used throughout the project. + */ +public class CodegenConstants { + public static final String API_PACKAGE = "apiPackage"; + public static final String API_PACKAGE_DESC = "package for generated api classes"; + + public static final String MODEL_PACKAGE = "modelPackage"; + public static final String MODEL_PACKAGE_DESC = "package for generated models"; + + public static final String TEMPLATE_DIR = "templateDir"; + + + public static final String INVOKER_PACKAGE = "invokerPackage"; + public static final String INVOKER_PACKAGE_DESC = "root package for generated code"; + + public static final String GROUP_ID = "groupId"; + public static final String GROUP_ID_DESC = "groupId in generated pom.xml"; + + public static final String ARTIFACT_ID = "artifactId"; + public static final String ARTIFACT_ID_DESC = "artifactId in generated pom.xml"; + + public static final String ARTIFACT_VERSION = "artifactVersion"; + public static final String ARTIFACT_VERSION_DESC = "artifact version in generated pom.xml"; + + public static final String SOURCE_FOLDER = "sourceFolder"; + public static final String SOURCE_FOLDER_DESC = "source folder for generated code"; + + public static final String LOCAL_VARIABLE_PREFIX = "localVariablePrefix"; + public static final String LOCAL_VARIABLE_PREFIX_DESC = "prefix for generated code members and local variables"; + + public static final String SERIALIZABLE_MODEL = "serializableModel"; + public static final String SERIALIZABLE_MODEL_DESC = "boolean - toggle \"implements Serializable\" for generated models"; +} diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index aeca659e42a..2188af33b7f 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -91,16 +91,16 @@ public class DefaultCodegen { } public void processOpts() { - if (additionalProperties.containsKey("templateDir")) { - this.setTemplateDir((String) additionalProperties.get("templateDir")); + if (additionalProperties.containsKey(CodegenConstants.TEMPLATE_DIR)) { + this.setTemplateDir((String) additionalProperties.get(CodegenConstants.TEMPLATE_DIR)); } - if (additionalProperties.containsKey("modelPackage")) { - this.setModelPackage((String) additionalProperties.get("modelPackage")); + if (additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) { + this.setModelPackage((String) additionalProperties.get(CodegenConstants.MODEL_PACKAGE)); } - if (additionalProperties.containsKey("apiPackage")) { - this.setApiPackage((String) additionalProperties.get("apiPackage")); + if (additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) { + this.setApiPackage((String) additionalProperties.get(CodegenConstants.API_PACKAGE)); } } @@ -336,8 +336,8 @@ public class DefaultCodegen { importMapping.put("LocalDate", "org.joda.time.*"); importMapping.put("LocalTime", "org.joda.time.*"); - cliOptions.add(new CliOption("modelPackage", "package for generated models")); - cliOptions.add(new CliOption("apiPackage", "package for generated api classes")); + cliOptions.add(new CliOption(CodegenConstants.MODEL_PACKAGE, CodegenConstants.MODEL_PACKAGE_DESC)); + cliOptions.add(new CliOption(CodegenConstants.API_PACKAGE, CodegenConstants.API_PACKAGE_DESC)); } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AkkaScalaClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AkkaScalaClientCodegen.java index f9490e3d3dd..c40eb653543 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AkkaScalaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AkkaScalaClientCodegen.java @@ -4,6 +4,7 @@ import com.google.common.base.CaseFormat; import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Template; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenOperation; import io.swagger.codegen.CodegenProperty; import io.swagger.codegen.CodegenResponse; @@ -80,10 +81,10 @@ public class AkkaScalaClientCodegen extends DefaultCodegen implements CodegenCon "trait", "try", "true", "type", "val", "var", "while", "with", "yield") ); - additionalProperties.put("invokerPackage", invokerPackage); - additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); additionalProperties.put("configKey", configKey); additionalProperties.put("configKeyPath", configKeyPath); additionalProperties.put("defaultTimeout", defaultTimeoutInMs); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidClientCodegen.java index f570cc42e82..9c55053e59c 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidClientCodegen.java @@ -2,6 +2,7 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CliOption; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; import io.swagger.codegen.SupportingFile; @@ -58,11 +59,11 @@ public class AndroidClientCodegen extends DefaultCodegen implements CodegenConfi instantiationTypes.put("array", "ArrayList"); instantiationTypes.put("map", "HashMap"); - cliOptions.add(new CliOption("invokerPackage", "root package to use for the generated code")); - cliOptions.add(new CliOption("groupId", "groupId for use in the generated build.gradle and pom.xml")); - cliOptions.add(new CliOption("artifactId", "artifactId for use in the generated build.gradle and pom.xml")); - cliOptions.add(new CliOption("artifactVersion", "artifact version for use in the generated build.gradle and pom.xml")); - cliOptions.add(new CliOption("sourceFolder", "source folder for generated code")); + cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, CodegenConstants.INVOKER_PACKAGE_DESC)); + cliOptions.add(new CliOption(CodegenConstants.GROUP_ID, "groupId for use in the generated build.gradle and pom.xml")); + cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_ID, "artifactId for use in the generated build.gradle and pom.xml")); + cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_VERSION, "artifact version for use in the generated build.gradle and pom.xml")); + cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC)); cliOptions.add(new CliOption("useAndroidMavenGradlePlugin", "A flag to toggle android-maven gradle plugin. Default is true.")); } @@ -187,36 +188,36 @@ public class AndroidClientCodegen extends DefaultCodegen implements CodegenConfi public void processOpts() { super.processOpts(); - if (additionalProperties.containsKey("invokerPackage")) { - this.setInvokerPackage((String) additionalProperties.get("invokerPackage")); + if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) { + this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE)); } else { //not set, use default to be passed to template - additionalProperties.put("invokerPackage", invokerPackage); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); } - if (additionalProperties.containsKey("groupId")) { - this.setGroupId((String) additionalProperties.get("groupId")); + if (additionalProperties.containsKey(CodegenConstants.GROUP_ID)) { + this.setGroupId((String) additionalProperties.get(CodegenConstants.GROUP_ID)); } else { //not set, use to be passed to template - additionalProperties.put("groupId", groupId); + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); } - if (additionalProperties.containsKey("artifactId")) { - this.setArtifactId((String) additionalProperties.get("artifactId")); + if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_ID)) { + this.setArtifactId((String) additionalProperties.get(CodegenConstants.ARTIFACT_ID)); } else { //not set, use to be passed to template - additionalProperties.put("artifactId", artifactId); + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); } - if (additionalProperties.containsKey("artifactVersion")) { - this.setArtifactVersion((String) additionalProperties.get("artifactVersion")); + if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) { + this.setArtifactVersion((String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION)); } else { //not set, use to be passed to template - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); } - if (additionalProperties.containsKey("sourceFolder")) { - this.setSourceFolder((String) additionalProperties.get("sourceFolder")); + if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) { + this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER)); } if (additionalProperties.containsKey("useAndroidMavenGradlePlugin")) { diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AsyncScalaClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AsyncScalaClientCodegen.java index 53faf2f9e1f..38e6328a8e5 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AsyncScalaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AsyncScalaClientCodegen.java @@ -1,6 +1,7 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; import io.swagger.codegen.SupportingFile; @@ -52,10 +53,10 @@ public class AsyncScalaClientCodegen extends DefaultCodegen implements CodegenCo "trait", "try", "true", "type", "val", "var", "while", "with", "yield") ); - additionalProperties.put("invokerPackage", invokerPackage); - additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); additionalProperties.put("asyncHttpClient", asyncHttpClient); additionalProperties.put("authScheme", authScheme); additionalProperties.put("authPreemptive", authPreemptive); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlashClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlashClientCodegen.java index ca0979fb694..7c484c38a0b 100755 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlashClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/FlashClientCodegen.java @@ -2,34 +2,27 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CliOption; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; import io.swagger.codegen.SupportingFile; import io.swagger.models.properties.ArrayProperty; -import io.swagger.models.properties.MapProperty; -import io.swagger.models.properties.Property; -import io.swagger.models.properties.AbstractNumericProperty; -import io.swagger.models.properties.ArrayProperty; import io.swagger.models.properties.BooleanProperty; import io.swagger.models.properties.DateProperty; import io.swagger.models.properties.DateTimeProperty; -import io.swagger.models.properties.DecimalProperty; import io.swagger.models.properties.DoubleProperty; import io.swagger.models.properties.FloatProperty; import io.swagger.models.properties.IntegerProperty; import io.swagger.models.properties.LongProperty; import io.swagger.models.properties.MapProperty; import io.swagger.models.properties.Property; -import io.swagger.models.properties.PropertyBuilder; -import io.swagger.models.properties.RefProperty; import io.swagger.models.properties.StringProperty; +import org.apache.commons.lang.StringUtils; import java.io.File; import java.util.Arrays; -import java.util.HashSet; import java.util.HashMap; - -import org.apache.commons.lang.StringUtils; +import java.util.HashSet; public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig { protected String packageName = "io.swagger"; @@ -82,8 +75,8 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig cliOptions.clear(); cliOptions.add(new CliOption("packageName", "flash package name (convention: package.name), default: io.swagger")); cliOptions.add(new CliOption("packageVersion", "flash package version, default: 1.0.0")); - cliOptions.add(new CliOption("invokerPackage", "root package for generated code")); - cliOptions.add(new CliOption("sourceFolder", "source folder for generated code. e.g. src/main/flex")); + cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, CodegenConstants.INVOKER_PACKAGE_DESC)); + cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, "source folder for generated code. e.g. src/main/flex")); } @@ -91,15 +84,15 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig public void processOpts() { super.processOpts(); - if (additionalProperties.containsKey("invokerPackage")) { - this.setInvokerPackage((String) additionalProperties.get("invokerPackage")); + if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) { + this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE)); } else { //not set, use default to be passed to template - additionalProperties.put("invokerPackage", invokerPackage); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); } - if (additionalProperties.containsKey("sourceFolder")) { - this.setSourceFolder((String) additionalProperties.get("sourceFolder")); + if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) { + this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER)); } if (additionalProperties.containsKey("packageName")) { diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java index fe9b9ed2e57..00b5c99cf4f 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java @@ -3,19 +3,16 @@ package io.swagger.codegen.languages; import com.google.common.base.Strings; import io.swagger.codegen.CliOption; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenModel; import io.swagger.codegen.CodegenProperty; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; import io.swagger.codegen.SupportingFile; -import io.swagger.models.ComposedModel; import io.swagger.models.Model; -import io.swagger.models.ModelImpl; -import io.swagger.models.RefModel; import io.swagger.models.properties.ArrayProperty; import io.swagger.models.properties.MapProperty; import io.swagger.models.properties.Property; -import io.swagger.models.properties.StringProperty; import java.io.File; import java.util.ArrayList; @@ -76,14 +73,13 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { instantiationTypes.put("array", "ArrayList"); instantiationTypes.put("map", "HashMap"); - cliOptions.add(new CliOption("invokerPackage", "root package for generated code")); - cliOptions.add(new CliOption("groupId", "groupId in generated pom.xml")); - cliOptions.add(new CliOption("artifactId", "artifactId in generated pom.xml")); - cliOptions.add(new CliOption("artifactVersion", "artifact version in generated pom.xml")); - cliOptions.add(new CliOption("sourceFolder", "source folder for generated code")); - cliOptions.add(new CliOption("localVariablePrefix", "prefix for generated code members and local variables")); - - cliOptions.add(new CliOption("serializableModel", "boolean - toggle \"implements Serializable\" for generated models")); + cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, CodegenConstants.INVOKER_PACKAGE_DESC)); + cliOptions.add(new CliOption(CodegenConstants.GROUP_ID, CodegenConstants.GROUP_ID_DESC)); + cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_ID, CodegenConstants.ARTIFACT_ID_DESC)); + cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_VERSION, CodegenConstants.ARTIFACT_VERSION_DESC)); + cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC)); + cliOptions.add(new CliOption(CodegenConstants.LOCAL_VARIABLE_PREFIX, CodegenConstants.LOCAL_VARIABLE_PREFIX_DESC)); + cliOptions.add(new CliOption(CodegenConstants.SERIALIZABLE_MODEL, CodegenConstants.SERIALIZABLE_MODEL_DESC)); supportedLibraries.put("", "HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2"); supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.6"); @@ -109,49 +105,49 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { public void processOpts() { super.processOpts(); - if (additionalProperties.containsKey("invokerPackage")) { - this.setInvokerPackage((String) additionalProperties.get("invokerPackage")); + if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) { + this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE)); } else { //not set, use default to be passed to template - additionalProperties.put("invokerPackage", invokerPackage); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); } - if (additionalProperties.containsKey("groupId")) { - this.setGroupId((String) additionalProperties.get("groupId")); + if (additionalProperties.containsKey(CodegenConstants.GROUP_ID)) { + this.setGroupId((String) additionalProperties.get(CodegenConstants.GROUP_ID)); } else { //not set, use to be passed to template - additionalProperties.put("groupId", groupId); + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); } - if (additionalProperties.containsKey("artifactId")) { - this.setArtifactId((String) additionalProperties.get("artifactId")); + if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_ID)) { + this.setArtifactId((String) additionalProperties.get(CodegenConstants.ARTIFACT_ID)); } else { //not set, use to be passed to template - additionalProperties.put("artifactId", artifactId); + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); } - if (additionalProperties.containsKey("artifactVersion")) { - this.setArtifactVersion((String) additionalProperties.get("artifactVersion")); + if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) { + this.setArtifactVersion((String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION)); } else { //not set, use to be passed to template - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); } - if (additionalProperties.containsKey("sourceFolder")) { - this.setSourceFolder((String) additionalProperties.get("sourceFolder")); + if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) { + this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER)); } - if (additionalProperties.containsKey("localVariablePrefix")) { - this.setLocalVariablePrefix((String) additionalProperties.get("localVariablePrefix")); + if (additionalProperties.containsKey(CodegenConstants.LOCAL_VARIABLE_PREFIX)) { + this.setLocalVariablePrefix((String) additionalProperties.get(CodegenConstants.LOCAL_VARIABLE_PREFIX)); } - if (additionalProperties.containsKey("serializableModel")) { - this.setSerializableModel(Boolean.valueOf((String)additionalProperties.get("serializableModel").toString())); + if (additionalProperties.containsKey(CodegenConstants.SERIALIZABLE_MODEL)) { + this.setSerializableModel(Boolean.valueOf((String)additionalProperties.get(CodegenConstants.SERIALIZABLE_MODEL).toString())); } // need to put back serializableModel (boolean) into additionalProperties as value in additionalProperties is string - additionalProperties.put("serializableModel", serializableModel); + additionalProperties.put(CodegenConstants.SERIALIZABLE_MODEL, serializableModel); this.sanitizeConfig(); @@ -177,18 +173,18 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { // the whole additionalProperties object is injected into the main object passed to the mustache layer this.setApiPackage(sanitizePackageName(apiPackage)); - if (additionalProperties.containsKey("apiPackage")) { - this.additionalProperties.put("apiPackage", apiPackage); + if (additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) { + this.additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage); } this.setModelPackage(sanitizePackageName(modelPackage)); - if (additionalProperties.containsKey("modelPackage")) { - this.additionalProperties.put("modelPackage", modelPackage); + if (additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) { + this.additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage); } this.setInvokerPackage(sanitizePackageName(invokerPackage)); - if (additionalProperties.containsKey("invokerPackage")) { - this.additionalProperties.put("invokerPackage", invokerPackage); + if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) { + this.additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); } } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaInflectorServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaInflectorServerCodegen.java index 168e214aef1..56dbdd0e48b 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaInflectorServerCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaInflectorServerCodegen.java @@ -1,6 +1,7 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenOperation; import io.swagger.codegen.CodegenType; import io.swagger.codegen.SupportingFile; @@ -38,10 +39,10 @@ public class JavaInflectorServerCodegen extends JavaClientCodegen implements Cod apiPackage = System.getProperty("swagger.codegen.inflector.apipackage", "io.swagger.handler"); modelPackage = System.getProperty("swagger.codegen.inflector.modelpackage", "io.swagger.model"); - additionalProperties.put("invokerPackage", invokerPackage); - additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); additionalProperties.put("title", title); languageSpecificPrimitives = new HashSet( diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JaxRSServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JaxRSServerCodegen.java index 01261799556..3966cb9bea7 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JaxRSServerCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JaxRSServerCodegen.java @@ -1,14 +1,12 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CodegenConfig; -import io.swagger.codegen.CodegenResponse; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenOperation; +import io.swagger.codegen.CodegenResponse; import io.swagger.codegen.CodegenType; import io.swagger.codegen.SupportingFile; import io.swagger.models.Operation; -import io.swagger.models.properties.ArrayProperty; -import io.swagger.models.properties.MapProperty; -import io.swagger.models.properties.Property; import java.io.File; import java.util.ArrayList; @@ -39,10 +37,10 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf apiPackage = System.getProperty("swagger.codegen.jaxrs.apipackage", "io.swagger.api"); modelPackage = System.getProperty("swagger.codegen.jaxrs.modelpackage", "io.swagger.model"); - additionalProperties.put("invokerPackage", invokerPackage); - additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); additionalProperties.put("title", title); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java index 5f6c48120cb..fbb798f6802 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java @@ -2,6 +2,7 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CliOption; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; import io.swagger.codegen.SupportingFile; @@ -84,14 +85,14 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { typeMapping.put("list", "array"); typeMapping.put("object", "object"); typeMapping.put("DateTime", "\\DateTime"); - + cliOptions.add(new CliOption("variableNamingConvention", "naming convention of variable name, e.g. camelCase. Default: snake_case")); - cliOptions.add(new CliOption("invokerPackage", "The main namespace to use for all classes. e.g. Yay\\Pets")); + cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, "The main namespace to use for all classes. e.g. Yay\\Pets")); cliOptions.add(new CliOption("packagePath", "The main package name for classes. e.g. GeneratedPetstore")); cliOptions.add(new CliOption("srcBasePath", "The directory under packagePath to serve as source root.")); cliOptions.add(new CliOption("composerVendorName", "The vendor name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name. e.g. yaypets")); cliOptions.add(new CliOption("composerProjectName", "The project name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name. e.g. petstore-client")); - cliOptions.add(new CliOption("artifactVersion", "The version to use in the composer package version field. e.g. 1.2.3")); + cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_VERSION, "The version to use in the composer package version field. e.g. 1.2.3")); } public String getPackagePath() { @@ -155,22 +156,22 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { additionalProperties.put("srcBasePath", srcBasePath); } - if (additionalProperties.containsKey("invokerPackage")) { - this.setInvokerPackage((String) additionalProperties.get("invokerPackage")); + if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) { + this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE)); } else { - additionalProperties.put("invokerPackage", invokerPackage); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); } - if (additionalProperties.containsKey("modelPackage")) { - this.setModelPackage((String) additionalProperties.get("modelPackage")); + if (additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) { + this.setModelPackage((String) additionalProperties.get(CodegenConstants.MODEL_PACKAGE)); } else { - additionalProperties.put("modelPackage", modelPackage); + additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage); } - if (additionalProperties.containsKey("apiPackage")) { - this.setApiPackage((String) additionalProperties.get("apiPackage")); + if (additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) { + this.setApiPackage((String) additionalProperties.get(CodegenConstants.API_PACKAGE)); } else { - additionalProperties.put("apiPackage", apiPackage); + additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage); } if (additionalProperties.containsKey("composerProjectName")) { @@ -185,10 +186,10 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { additionalProperties.put("composerVendorName", composerVendorName); } - if (additionalProperties.containsKey("artifactVersion")) { - this.setArtifactVersion((String) additionalProperties.get("artifactVersion")); + if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) { + this.setArtifactVersion((String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION)); } else { - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); } additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\")); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RetrofitClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RetrofitClientCodegen.java index 66d3f19f948..37c119edfb6 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RetrofitClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RetrofitClientCodegen.java @@ -1,6 +1,7 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenOperation; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; @@ -44,10 +45,10 @@ public class RetrofitClientCodegen extends DefaultCodegen implements CodegenConf "native", "super", "while") ); - additionalProperties.put("invokerPackage", invokerPackage); - additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); supportingFiles.add(new SupportingFile("service.mustache", diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalaClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalaClientCodegen.java index 5d8ae4b89b6..003be5ab950 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalaClientCodegen.java @@ -1,6 +1,7 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; import io.swagger.codegen.SupportingFile; @@ -54,10 +55,10 @@ public class ScalaClientCodegen extends DefaultCodegen implements CodegenConfig "trait", "try", "true", "type", "val", "var", "while", "with", "yield") ); - additionalProperties.put("invokerPackage", invokerPackage); - additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); additionalProperties.put("asyncHttpClient", asyncHttpClient); additionalProperties.put("authScheme", authScheme); additionalProperties.put("authPreemptive", authPreemptive); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalatraServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalatraServerCodegen.java index d6b0f78253a..9bab6a6cad0 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalatraServerCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ScalatraServerCodegen.java @@ -1,6 +1,7 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenOperation; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; @@ -73,10 +74,10 @@ public class ScalatraServerCodegen extends DefaultCodegen implements CodegenConf additionalProperties.put("infoEmail", "apiteam@swagger.io"); additionalProperties.put("licenseInfo", "All rights reserved"); additionalProperties.put("licenseUrl", "http://apache.org/licenses/LICENSE-2.0.html"); - additionalProperties.put("invokerPackage", invokerPackage); - additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("build.sbt", "", "build.sbt")); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SilexServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SilexServerCodegen.java index 8bd4d627d56..e25aae15976 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SilexServerCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SilexServerCodegen.java @@ -1,6 +1,7 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; import io.swagger.codegen.SupportingFile; @@ -41,10 +42,10 @@ public class SilexServerCodegen extends DefaultCodegen implements CodegenConfig "__halt_compiler", "abstract", "and", "array", "as", "break", "callable", "case", "catch", "class", "clone", "const", "continue", "declare", "default", "die", "do", "echo", "else", "elseif", "empty", "enddeclare", "endfor", "endforeach", "endif", "endswitch", "endwhile", "eval", "exit", "extends", "final", "for", "foreach", "function", "global", "goto", "if", "implements", "include", "include_once", "instanceof", "insteadof", "interface", "isset", "list", "namespace", "new", "or", "print", "private", "protected", "public", "require", "require_once", "return", "static", "switch", "throw", "trait", "try", "unset", "use", "var", "while", "xor") ); - additionalProperties.put("invokerPackage", invokerPackage); - additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); // ref: http://php.net/manual/en/language.types.intro.php languageSpecificPrimitives = new HashSet( diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringMVCServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringMVCServerCodegen.java index edc424f125c..597fd1f1936 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringMVCServerCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringMVCServerCodegen.java @@ -34,12 +34,12 @@ public class SpringMVCServerCodegen extends JavaClientCodegen implements Codegen configPackage = "io.swagger.configuration"; - additionalProperties.put("invokerPackage", invokerPackage); - additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); additionalProperties.put("title", title); - additionalProperties.put("apiPackage", apiPackage); + additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage); additionalProperties.put("configPackage", configPackage); languageSpecificPrimitives = new HashSet( diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/StaticDocCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/StaticDocCodegen.java index bfee70f1732..c0ccf2a0a6f 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/StaticDocCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/StaticDocCodegen.java @@ -1,6 +1,7 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; import io.swagger.codegen.SupportingFile; @@ -21,10 +22,10 @@ public class StaticDocCodegen extends DefaultCodegen implements CodegenConfig { apiTemplateFiles.put("operation.mustache", ".html"); templateDir = "swagger-static"; - additionalProperties.put("invokerPackage", invokerPackage); - additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); supportingFiles.add(new SupportingFile("package.mustache", "", "package.json")); supportingFiles.add(new SupportingFile("main.mustache", "", "main.js")); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/StaticHtmlGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/StaticHtmlGenerator.java index 8a3244b0097..29cb5d55eaa 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/StaticHtmlGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/StaticHtmlGenerator.java @@ -1,6 +1,7 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenOperation; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; @@ -37,10 +38,10 @@ public class StaticHtmlGenerator extends DefaultCodegen implements CodegenConfig additionalProperties.put("infoEmail", "hello@helloreverb.com"); additionalProperties.put("licenseInfo", "All rights reserved"); additionalProperties.put("licenseUrl", "http://apache.org/licenses/LICENSE-2.0.html"); - additionalProperties.put("invokerPackage", invokerPackage); - additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); supportingFiles.add(new SupportingFile("index.mustache", "", "index.html")); reservedWords = new HashSet(); diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenConfigLoaderTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenConfigLoaderTest.java new file mode 100644 index 00000000000..b91e506a4f9 --- /dev/null +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenConfigLoaderTest.java @@ -0,0 +1,57 @@ +package io.swagger.codegen; + +import org.reflections.Reflections; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import static org.testng.Assert.assertEquals; + +public class CodegenConfigLoaderTest { + + @DataProvider(name = "codegenConfig") + public Object[][] createCodegenConfigDataSet() throws Exception { + + Reflections reflections = new Reflections("io.swagger.codegen.languages"); + final Set> subTypesOf = reflections.getSubTypesOf(DefaultCodegen.class); + + List codegenConfigList = new ArrayList(); + + for (Class aClass : subTypesOf) { + if (!Modifier.isAbstract(aClass.getModifiers())) { + final DefaultCodegen defaultCodegen = aClass.newInstance(); + codegenConfigList.add((CodegenConfig) defaultCodegen); + } + } + + Object[][] result = new Object[codegenConfigList.size()][1]; + + for (int i = 0; i < codegenConfigList.size(); i++) { + result[i]= new Object[]{codegenConfigList.get(i)}; + } + + return result; + } + + @Test(dataProvider = "codegenConfig") + public void testLoadByName(CodegenConfig codegenConfig) throws Exception { + final CodegenConfig loadedConfig = CodegenConfigLoader.forName(codegenConfig.getName()); + + assertEquals(loadedConfig.getClass(), codegenConfig.getClass()); + assertEquals(loadedConfig.getName(), codegenConfig.getName()); + } + + @Test(dataProvider = "codegenConfig") + public void testLoadByFullQualifiedName(CodegenConfig codegenConfig) throws Exception { + final CodegenConfig loadedConfig = CodegenConfigLoader.forName(codegenConfig.getClass().getName()); + + assertEquals(loadedConfig.getClass(), codegenConfig.getClass()); + assertEquals(loadedConfig.getName(), codegenConfig.getName()); + + + } +} diff --git a/modules/swagger-codegen-cli/src/test/java/io/swagger/codegen/GenerateTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java similarity index 51% rename from modules/swagger-codegen-cli/src/test/java/io/swagger/codegen/GenerateTest.java rename to modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java index bd2a342e3e2..5da6a01763c 100644 --- a/modules/swagger-codegen-cli/src/test/java/io/swagger/codegen/GenerateTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java @@ -1,15 +1,13 @@ package io.swagger.codegen; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - +import io.swagger.codegen.languages.JavaClientCodegen; +import io.swagger.models.Swagger; +import io.swagger.parser.SwaggerParser; import org.apache.commons.io.FileUtils; -import org.junit.Rule; -import org.junit.Test; import org.junit.rules.TemporaryFolder; - -import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.fail; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; import java.io.BufferedWriter; import java.io.File; @@ -20,34 +18,53 @@ import java.io.Writer; import java.nio.charset.StandardCharsets; import java.util.Arrays; -public class GenerateTest { +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.fail; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +/** + * Tests for DefaultGenerator logic + */ +public class DefaultGeneratorTest { private static final String TEST_SKIP_OVERWRITE = "testSkipOverwrite"; private static final String POM_FILE = "pom.xml"; private static final String MODEL_ORDER_FILE = "/src/main/java/io/swagger/client/model/Order.java"; - @Rule public TemporaryFolder folder = new TemporaryFolder(); + @BeforeMethod + public void setUp() throws Exception { + folder.create(); + } + + @AfterMethod + public void tearDown() throws Exception { + folder.delete(); + } + @Test - public void testSkipOverwrite() throws IOException { + public void testSkipOverwrite() throws Exception { final File output = folder.getRoot(); - String[] args = {"generate", "-i", "src/test/resources/petstore.json", "-l", "java", "-o", output.getAbsolutePath()}; - String[] argsWithSparam = Arrays.copyOf(args, args.length + 1); - argsWithSparam[args.length] = "-s"; + final Swagger swagger = new SwaggerParser().read("src/test/resources/petstore.json"); + CodegenConfig codegenConfig = new JavaClientCodegen(); + codegenConfig.setOutputDir(output.getAbsolutePath()); - //generate content first time without -s flag, so all generated files should be recorded - SwaggerCodegen.main(args); + ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).swagger(swagger).config(codegenConfig); + + //generate content first time without skipOverwrite flag, so all generated files should be recorded + new DefaultGenerator().opts(clientOptInput).generate(); final File order = new File(output, MODEL_ORDER_FILE); assertTrue(order.exists()); //change content of one file changeContent(order); - //generate content second time without -s flag, so changed file should be rewritten - SwaggerCodegen.main(args); - //order = new File(output, MODEL_ORDER_FILE); + //generate content second time without skipOverwrite flag, so changed file should be rewritten + new DefaultGenerator().opts(clientOptInput).generate(); + assertTrue(!TEST_SKIP_OVERWRITE.equals(FileUtils.readFileToString(order, StandardCharsets.UTF_8))); //change content again @@ -58,9 +75,10 @@ public class GenerateTest { fail(); } - //generate content third time with -s flag, so changed file should not be rewritten + //generate content third time with skipOverwrite flag, so changed file should not be rewritten //and deleted file should be recorded - SwaggerCodegen.main(argsWithSparam); + codegenConfig.setSkipOverwrite(true); + new DefaultGenerator().opts(clientOptInput).generate(); assertEquals(FileUtils.readFileToString(order, StandardCharsets.UTF_8), TEST_SKIP_OVERWRITE); assertTrue(pom.exists()); } @@ -70,4 +88,5 @@ public class GenerateTest { out.write(TEST_SKIP_OVERWRITE); out.close(); } + } diff --git a/modules/swagger-codegen-cli/src/test/resources/petstore.json b/modules/swagger-codegen/src/test/resources/petstore.json similarity index 100% rename from modules/swagger-codegen-cli/src/test/resources/petstore.json rename to modules/swagger-codegen/src/test/resources/petstore.json diff --git a/pom.xml b/pom.xml index 4baa768bd76..2ff111c5745 100644 --- a/pom.xml +++ b/pom.xml @@ -68,6 +68,27 @@ target ${project.artifactId}-${project.version} + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire-version} + + none:none + + + + test-testng + test + + test + + + none:none + org.testng:testng + + + + maven-dependency-plugin @@ -457,6 +478,18 @@ ${junit-version} test + + org.testng + testng + ${testng-version} + test + + + org.jmockit + jmockit + ${jmockit-version} + test + @@ -482,5 +515,9 @@ 1.6.3 3.2.1 1.9 + 6.9.6 + 2.18.1 + 1.18 + 0.9.10