switch Generate to use CodegenConfigurator

This commit is contained in:
russellb337
2015-09-02 09:28:40 -07:00
parent 38b1e1aad7
commit 6d7557971b
9 changed files with 828 additions and 328 deletions

View File

@@ -1,24 +1,18 @@
package io.swagger.codegen.cmd;
import config.Config;
import config.ConfigParser;
import io.airlift.airline.Command;
import io.airlift.airline.Option;
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 io.swagger.codegen.config.CodegenConfigurator;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -37,7 +31,7 @@ public class Generate implements Runnable {
public static final Logger LOG = LoggerFactory.getLogger(Generate.class);
@Option(name = {"-v", "--verbose"}, description = "verbose mode")
private boolean verbose;
private Boolean verbose;
@Option(name = {"-l", "--lang"}, title = "language", required = true,
description = "client language to generate (maybe class name in classpath, required)")
@@ -71,7 +65,7 @@ public class Generate implements Runnable {
@Option(name = {"-s", "--skip-overwrite"}, title = "skip overwrite", description = "specifies if the existing files should be " +
"overwritten during the generation.")
private boolean skipOverwrite;
private Boolean skipOverwrite;
@Option(name = {"--api-package"}, title = "api package", description = CodegenConstants.API_PACKAGE_DESC)
private String apiPackage;
@@ -110,116 +104,146 @@ public class Generate implements Runnable {
@Option(name = {"--artifact-version"}, title = "artifact version", description = CodegenConstants.ARTIFACT_VERSION_DESC)
private String artifactVersion;
@Option(name = {"--library"}, title = "library", description = CodegenConstants.LIBRARY_DESC)
private String library;
@Override
public void run() {
verbosed(verbose);
setSystemProperties();
//attempt to read from config file
CodegenConfigurator configurator = CodegenConfigurator.fromFile(configFile);
CodegenConfig config = CodegenConfigLoader.forName(lang);
//if a config file wasn't specified or we were unable to read it
if(configurator == null) {
//createa a fresh configurator
configurator = new CodegenConfigurator();
}
config.setOutputDir(new File(output).getAbsolutePath());
config.setSkipOverwrite(skipOverwrite);
//now override with any specified parameters
if (verbose != null) {
configurator.setVerbose(verbose);
}
putKeyValuePairsInMap(config.instantiationTypes(), instantiationTypes);
putKeyValuePairsInMap(config.typeMapping(), typeMappings);
putKeyValuePairsInMap(config.additionalProperties(), additionalProperties);
putKeyValuePairsInMap(config.importMapping(), importMappings);
if(skipOverwrite != null) {
configurator.setSkipOverwrite(skipOverwrite);
}
addValuesToSet(config.languageSpecificPrimitives(), languageSpecificPrimitives);
if(isNotEmpty(spec)) {
configurator.setInputSpec(spec);
}
checkAndSetAdditionalProperty(config, apiPackage, CodegenConstants.API_PACKAGE);
checkAndSetAdditionalProperty(config, modelPackage, CodegenConstants.MODEL_PACKAGE);
if(isNotEmpty(lang)) {
configurator.setLang(lang);
}
if(isNotEmpty(output)) {
configurator.setOutputDir(output);
}
if(isNotEmpty(auth)) {
configurator.setAuth(auth);
}
if(isNotEmpty(templateDir)) {
config.additionalProperties().put(CodegenConstants.TEMPLATE_DIR, new File(templateDir).getAbsolutePath());
configurator.setTemplateDir(templateDir);
}
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) {
for (CliOption langCliOption : config.cliOptions()) {
String opt = langCliOption.getOpt();
if (genConfig.hasOption(opt)) {
config.additionalProperties().put(opt, genConfig.getOption(opt));
// the "library" config option is for library template (sub-template)
if ("library".equals(opt)) {
config.setLibrary(genConfig.getOption(opt));
}
}
}
}
if(isNotEmpty(apiPackage)) {
configurator.setApiPackage(apiPackage);
}
ClientOptInput input = new ClientOptInput().config(config);
if (isNotEmpty(auth)) {
input.setAuth(auth);
if(isNotEmpty(modelPackage)) {
configurator.setModelPackage(modelPackage);
}
Swagger swagger = new SwaggerParser().read(spec, input.getAuthorizationValues(), true);
new DefaultGenerator().opts(input.opts(new ClientOpts()).swagger(swagger)).generate();
if(isNotEmpty(invokerPackage)) {
configurator.setInvokerPackage(invokerPackage);
}
if(isNotEmpty(groupId)) {
configurator.setGroupId(groupId);
}
if(isNotEmpty(artifactId)) {
configurator.setArtifactId(artifactId);
}
if(isNotEmpty(artifactVersion)) {
configurator.setArtifactVersion(artifactVersion);
}
if(isNotEmpty(library)) {
configurator.setLibrary(library);
}
setSystemProperties(configurator);
setInstantiationTypes(configurator);
setImportMappings(configurator);
setTypeMappings(configurator);
setAdditionalProperties(configurator);
setLanguageSpecificPrimitives(configurator);
final ClientOptInput clientOptInput = configurator.toClientOptInput();
new DefaultGenerator().opts(clientOptInput).generate();
}
private void addValuesToSet(Set<String> set, String csvProperty) {
private void setSystemProperties(CodegenConfigurator configurator) {
final Map<String, String> map = createMapFromKeyValuePairs(systemProperties);
for (Map.Entry<String, String> entry : map.entrySet()) {
configurator.addSystemProperty(entry.getKey(), entry.getValue());
}
}
private void setInstantiationTypes(CodegenConfigurator configurator) {
final Map<String, String> map = createMapFromKeyValuePairs(instantiationTypes);
for (Map.Entry<String, String> entry : map.entrySet()) {
configurator.addInstantiationType(entry.getKey(), entry.getValue());
}
}
private void setImportMappings(CodegenConfigurator configurator) {
final Map<String, String> map = createMapFromKeyValuePairs(importMappings);
for (Map.Entry<String, String> entry : map.entrySet()) {
configurator.addImportMapping(entry.getKey(), entry.getValue());
}
}
private void setTypeMappings(CodegenConfigurator configurator) {
final Map<String, String> map = createMapFromKeyValuePairs(typeMappings);
for (Map.Entry<String, String> entry : map.entrySet()) {
configurator.addTypeMapping(entry.getKey(), entry.getValue());
}
}
private void setAdditionalProperties(CodegenConfigurator configurator) {
final Map<String, String> map = createMapFromKeyValuePairs(additionalProperties);
for (Map.Entry<String, String> entry : map.entrySet()) {
configurator.addAdditionalProperty(entry.getKey(), entry.getValue());
}
}
private void setLanguageSpecificPrimitives(CodegenConfigurator configurator) {
final Set<String> set = createSetFromCsvList(languageSpecificPrimitives);
for (String item : set) {
configurator.addLanguageSpecificPrimitive(item);
}
}
private Set<String> createSetFromCsvList(String csvProperty) {
final List<String> values = OptionUtils.splitCommaSeparatedList(csvProperty);
for (String value : values) {
set.add(value);
}
return new HashSet<String>(values);
}
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<Pair<String, String>> systemPropertyPairs = OptionUtils.parseCommaSeparatedTuples(systemProperties);
for (Pair<String, String> pair : systemPropertyPairs) {
System.setProperty(pair.getLeft(), pair.getRight());
}
}
private void putKeyValuePairsInMap(Map map, String commaSeparatedKVPairs) {
private Map createMapFromKeyValuePairs(String commaSeparatedKVPairs) {
final List<Pair<String, String>> pairs = OptionUtils.parseCommaSeparatedTuples(commaSeparatedKVPairs);
Map result = new HashMap();
for (Pair<String, String> pair : pairs) {
map.put(pair.getLeft(), pair.getRight());
result.put(pair.getLeft(), pair.getRight());
}
}
/**
* If true parameter, adds system properties which enables debug mode in generator
*
* @param verbose - if true, enables debug mode
*/
private void verbosed(boolean verbose) {
if (!verbose) {
return;
}
LOG.info("\nVERBOSE MODE: ON. Additional debug options are injected" +
"\n - [debugSwagger] prints the swagger specification as interpreted by the codegen" +
"\n - [debugModels] prints models passed to the template engine" +
"\n - [debugOperations] prints operations passed to the template engine" +
"\n - [debugSupportingFiles] prints additional data passed to the template engine");
System.setProperty("debugSwagger", "");
System.setProperty("debugModels", "");
System.setProperty("debugOperations", "");
System.setProperty("debugSupportingFiles", "");
return result;
}
}

View File

@@ -1,84 +1,77 @@
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 io.swagger.codegen.config.CodegenConfigurator;
import mockit.Expectations;
import mockit.FullVerifications;
import mockit.Injectable;
import mockit.Mocked;
import mockit.StrictExpectations;
import mockit.Verifications;
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;
CodegenConfigurator configurator;
@Injectable
Swagger swagger;
@Mocked
DefaultGenerator defaultGenerator;
@Mocked
CodegenConfigLoader codegenConfigLoader;
@Mocked
ClientOptInput clientOptInput;
@Injectable
List<AuthorizationValue> authorizationValues;
@Mocked
DefaultGenerator generator;
@Test
public void testVerbose_ShortArg() throws Exception {
doVerboseTest("-v");
}
public void testVerbose() throws Exception {
setupAndRunGenericTest("-v");
@Test
public void testVerbose_LongArg() throws Exception {
doVerboseTest("--verbose");
new FullVerifications() {{
configurator.setVerbose(true);
times = 1;
}};
setupAndRunGenericTest("--verbose");
new FullVerifications() {{
configurator.setVerbose(true);
times = 1;
}};
}
@Test
public void testRequiredArgs_ShortArgs() throws Exception {
doRequiredArgsTest("-l", "-o", "-i");
setupAndRunTest("-i", "swagger.yaml", "-l", "java", "-o", "src/main/java", false, null);
new FullVerifications() {{
}};
}
@Test
public void testRequiredArgs_LongArgs() throws Exception {
doRequiredArgsTest("--lang", "--output", "--input-spec");
setupAndRunTest("--input-spec", "swagger.yaml", "--lang", "java", "--output", "src/main/java", false, null);
new FullVerifications() {{
}};
}
@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);
final String templateDir = "src/main/resources/customTemplates";
setupAndRunGenericTest("--template-dir", templateDir);
new FullVerifications() {{
configurator.setTemplateDir(templateDir);
times = 1;
}};
setupAndRunGenericTest("-t", templateDir);
new FullVerifications() {{
configurator.setTemplateDir(templateDir);
times = 1;
}};
}
@Test
@@ -86,267 +79,279 @@ public class GenerateTest {
final String auth = "hello:world";
new StrictExpectations() {{
clientOptInput.setAuth(auth);
setupAndRunGenericTest("--auth", auth);
new FullVerifications() {{
configurator.setAuth(auth);
times = 1;
}};
setupAndRunGenericTest("-a", auth);
new StrictExpectations() {{
clientOptInput.setAuth(auth);
new FullVerifications() {{
configurator.setAuth(auth);
times = 1;
}};
setupAndRunGenericTest("--auth", auth);
setupAndRunGenericTest();
new FullVerifications() {{
configurator.setAuth(anyString);
times = 0;
}};
}
@Test
public void testSystemProperties() throws Exception {
new StrictExpectations(System.class) {{
System.setProperty("hello", "world");
setupAndRunGenericTest("-D", "hello=world,foo=bar");
new FullVerifications() {{
configurator.addSystemProperty("hello", "world");
times = 1;
System.setProperty("foo", "bar");
configurator.addSystemProperty("foo", "bar");
times = 1;
}};
setupAndRunGenericTest("-D", "hello=world,foo=bar");
setupAndRunGenericTest("-D", "hello=world,key=,foo=bar");
new FullVerifications() {{
configurator.addSystemProperty("hello", "world");
times = 1;
configurator.addSystemProperty("foo", "bar");
times = 1;
configurator.addSystemProperty("key", anyString);
times = 0;
}};
}
@Test
public void testConfig(@Mocked final ConfigParser parser) throws Exception {
public void testConfig() throws Exception {
final String configFilePath = "config.json";
final String invokerPackage = "com.foo.bar.invoker";
final String groupId = "com.foo.bar";
Map<String, String> configMap = new HashMap<String, String>();
configMap.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
configMap.put(CodegenConstants.GROUP_ID, groupId);
final Config config = new Config(configMap);
setupAndRunTest("-i", "swagger.yaml", "-l", "java", "-o", "src/main/java", true, "config.json", "-c", "config.json");
final String[] configArgs = {"-c", "--config"};
new FullVerifications(){{}};
for (String configArg : configArgs) {
new StrictExpectations() {{
parser.read(configFilePath);
times = 1;
result = config;
setupAndRunTest("-i", "swagger.yaml", "-l", "java", "-o", "src/main/java", true, "config.json", "--config", "config.json");
}};
final CodegenConfig codegenConfig = setupAndRunGenericTest(configArg, configFilePath);
assertValueInMap(codegenConfig.additionalProperties(), CodegenConstants.INVOKER_PACKAGE, invokerPackage);
assertValueInMap(codegenConfig.additionalProperties(), CodegenConstants.GROUP_ID, groupId);
}
new FullVerifications(){{}};
}
@Test
public void testSkipOverwrite() throws Exception {
CodegenConfig codegenConfig1 = setupAndRunGenericTest();
assertFalse(codegenConfig1.isSkipOverwrite());
setupAndRunGenericTest("-s");
new FullVerifications(){{
configurator.setSkipOverwrite(true); times=1;
}};
CodegenConfig codegenConfig2 = setupAndRunGenericTest("-s");
assertTrue(codegenConfig2.isSkipOverwrite());
CodegenConfig codegenConfig3 = setupAndRunGenericTest("--skip-overwrite");
assertTrue(codegenConfig3.isSkipOverwrite());
setupAndRunGenericTest("--skip-overwrite");
new FullVerifications(){{
configurator.setSkipOverwrite(true); times=1;
}};
}
@Test
public void testApiPackage() throws Exception {
doSingleAdditionalPropertyTest("--api-package", CodegenConstants.API_PACKAGE, "io.foo.bar.api");
final String value = "io.foo.bar.api";
setupAndRunGenericTest("--api-package", value);
new FullVerifications(){{
configurator.setApiPackage(value); times=1;
}};
}
@Test
public void testModelPackage() throws Exception {
doSingleAdditionalPropertyTest("--model-package", CodegenConstants.MODEL_PACKAGE, "io.foo.bar.models");
final String value = "io.foo.bar.api";
setupAndRunGenericTest("--model-package", value);
new FullVerifications(){{
configurator.setModelPackage(value); times=1;
}};
}
@Test
public void testInstantiationTypes() throws Exception {
final CodegenConfig codegenConfig = setupAndRunGenericTest("--instantiation-types", "foo=bar,hello=world");
setupAndRunGenericTest("--instantiation-types", "hello=world,key=,foo=bar");
assertValueInMap(codegenConfig.instantiationTypes(), "foo", "bar");
assertValueInMap(codegenConfig.instantiationTypes(), "hello", "world");
new FullVerifications() {{
configurator.addInstantiationType("hello", "world");
times = 1;
configurator.addInstantiationType("foo", "bar");
times = 1;
configurator.addInstantiationType("key", anyString);
times = 0;
}};
}
@Test
public void testTypeMappings() throws Exception {
final CodegenConfig codegenConfig = setupAndRunGenericTest("--type-mappings", "foo=bar,hello=world");
setupAndRunGenericTest("--type-mappings", "hello=world,key=,foo=bar");
assertValueInMap(codegenConfig.typeMapping(), "foo", "bar");
assertValueInMap(codegenConfig.typeMapping(), "hello", "world");
new FullVerifications() {{
configurator.addTypeMapping("hello", "world");
times = 1;
configurator.addTypeMapping("foo", "bar");
times = 1;
configurator.addTypeMapping("key", anyString);
times = 0;
}};
}
@Test
public void testAdditionalProperties() throws Exception {
final CodegenConfig codegenConfig = setupAndRunGenericTest("--additional-properties", "foo=bar,hello=world");
setupAndRunGenericTest("--additional-properties", "hello=world,key=,foo=bar");
assertValueInMap(codegenConfig.additionalProperties(), "foo", "bar");
assertValueInMap(codegenConfig.additionalProperties(), "hello", "world");
new FullVerifications() {{
configurator.addAdditionalProperty("hello", "world");
times = 1;
configurator.addAdditionalProperty("foo", "bar");
times = 1;
configurator.addAdditionalProperty("key", anyString);
times = 0;
}};
}
@Test
public void testLanguageSpecificPrimitives() throws Exception {
final CodegenConfig codegenConfig = setupAndRunGenericTest("--language-specific-primitives", "foo,bar,hello,world");
setupAndRunGenericTest("--language-specific-primitives", "foo,bar,,hello,world");
final Set<String> languageSpecificPrimitives = codegenConfig.languageSpecificPrimitives();
assertTrue(languageSpecificPrimitives.contains("foo"));
assertTrue(languageSpecificPrimitives.contains("bar"));
assertTrue(languageSpecificPrimitives.contains("hello"));
assertTrue(languageSpecificPrimitives.contains("world"));
new FullVerifications() {{
configurator.addLanguageSpecificPrimitive("foo");
times = 1;
configurator.addLanguageSpecificPrimitive("bar");
times = 1;
configurator.addLanguageSpecificPrimitive("hello");
times = 1;
configurator.addLanguageSpecificPrimitive("world");
times = 1;
}};
}
@Test
public void testImportMappings() throws Exception {
final CodegenConfig codegenConfig = setupAndRunGenericTest("--import-mappings", "foo=bar,hello=world");
setupAndRunGenericTest("--import-mappings", "hello=world,key=,foo=bar");
assertValueInMap(codegenConfig.importMapping(), "foo", "bar");
assertValueInMap(codegenConfig.importMapping(), "hello", "world");
new FullVerifications() {{
configurator.addImportMapping("hello", "world");
times = 1;
configurator.addImportMapping("foo", "bar");
times = 1;
configurator.addImportMapping("key", anyString);
times = 0;
}};
}
@Test
public void testInvokerPackage() throws Exception {
doSingleAdditionalPropertyTest("--invoker-package", CodegenConstants.INVOKER_PACKAGE, "io.foo.bar.invoker");
final String value = "io.foo.bar.api";
setupAndRunGenericTest("--invoker-package", value);
new FullVerifications(){{
configurator.setInvokerPackage(value); times=1;
}};
}
@Test
public void testGroupId() throws Exception {
doSingleAdditionalPropertyTest("--group-id", CodegenConstants.GROUP_ID, "io.foo.bar");
final String value = "io.foo.bar.api";
setupAndRunGenericTest("--group-id", value);
new FullVerifications(){{
configurator.setGroupId(value); times=1;
}};
}
@Test
public void testArtifactId() throws Exception {
doSingleAdditionalPropertyTest("--artifact-id", CodegenConstants.ARTIFACT_ID, "awesome-api");
final String value = "awesome-api";
setupAndRunGenericTest("--artifact-id", value);
new FullVerifications(){{
configurator.setArtifactId(value); times=1;
}};
}
@Test
public void testArtifactVersion() throws Exception {
doSingleAdditionalPropertyTest("--artifact-version", CodegenConstants.ARTIFACT_VERSION, "1.2.3");
}
final String value = "1.2.3";
setupAndRunGenericTest("--artifact-version", value);
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;
new FullVerifications(){{
configurator.setArtifactVersion(value); 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";
@Test
public void testLibrary() throws Exception {
final String value = "library1";
setupAndRunGenericTest("--library", value);
final String[] args = {"generate", langFlag, lang, outputDirFlag, outputDir, inputSpecFlag, spec};
final CodegenConfig config = new JavaClientCodegen();
setupStandardExpectations(spec, lang, config);
SwaggerCodegen.main(args);
new FullVerifications() {{
new FullVerifications(){{
configurator.setLibrary(value); times=1;
}};
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};
private void setupAndRunTest(String specFlag, final String spec, String langFlag, final String lang,
String outputDirFlag, final String outputDir, boolean configuratorFromFile,
final String configFile, String... additionalParameters) {
final String[] commonArgs = {"generate", langFlag, lang, outputDirFlag, outputDir, specFlag, spec};
String[] argsToUse = ArrayUtils.addAll(commonArgs, additionalParameters);
final CodegenConfig config = new JavaClientCodegen();
if (configuratorFromFile) {
setupStandardExpectations(spec, lang, config);
new Expectations(){{
CodegenConfigurator.fromFile(configFile);
times = 1;
result = configurator;
}};
SwaggerCodegen.main(argsToUse);
} else {
new Expectations() {{
CodegenConfigurator.fromFile(anyString);
result = null;
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 CodegenConfigurator();
times = 1;
result = configurator;
}};
}
new Expectations() {{
CodegenConfigLoader.forName(languageName);
times = 1;
result = config;
new ClientOptInput();
configurator.toClientOptInput();
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;
result = generator;
clientOptInput.opts((ClientOpts) any);
generator.opts(clientOptInput);
times = 1;
result = clientOptInput;
result = generator;
clientOptInput.swagger(swagger);
generator.generate();
times = 1;
result = clientOptInput;
defaultGenerator.opts(clientOptInput);
times = 1;
result = defaultGenerator;
}};
defaultGenerator.generate();
SwaggerCodegen.main(argsToUse);
new Verifications() {{
configurator.setLang(lang);
times = 1;
configurator.setInputSpec(spec);
times = 1;
configurator.setOutputDir(outputDir);
}};
}
private void setupAndRunGenericTest(String... additionalParameters) {
setupAndRunTest("-i", "swagger.yaml", "-l", "java", "-o", "src/main/java", false, null, additionalParameters);
}
}

View File

@@ -346,6 +346,12 @@
<version>${reflections-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>${jmockit-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>

View File

@@ -1,6 +1,7 @@
package io.swagger.codegen;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.codegen.auth.AuthParser;
import io.swagger.models.Swagger;
import io.swagger.models.auth.AuthorizationValue;
@@ -32,47 +33,23 @@ public class ClientOptInput {
return this;
}
@Deprecated
public ClientOptInput auth(String urlEncodedAuthString) {
this.setAuth(urlEncodedAuthString);
return this;
}
@Deprecated
public String getAuth() {
if (auths != null) {
StringBuilder b = new StringBuilder();
for (AuthorizationValue v : auths) {
try {
if (b.toString().length() > 0) {
b.append(",");
}
b.append(URLEncoder.encode(v.getKeyName(), "UTF-8"))
.append(":")
.append(URLEncoder.encode(v.getValue(), "UTF-8"));
} catch (Exception e) {
// continue
e.printStackTrace();
}
}
return b.toString();
} else {
return null;
}
return AuthParser.reconstruct(auths);
}
@Deprecated
public void setAuth(String urlEncodedAuthString) {
List<AuthorizationValue> auths = new ArrayList<AuthorizationValue>();
if (isNotEmpty(urlEncodedAuthString)) {
String[] parts = urlEncodedAuthString.split(",");
for (String part : parts) {
String[] kvPair = part.split(":");
if (kvPair.length == 2) {
auths.add(new AuthorizationValue(URLDecoder.decode(kvPair[0]), URLDecoder.decode(kvPair[1]), "header"));
}
}
}
this.auths = auths;
this.auths = AuthParser.parse(urlEncodedAuthString);
}
@Deprecated
public List<AuthorizationValue> getAuthorizationValues() {
return auths;
}

View File

@@ -33,5 +33,8 @@ public class CodegenConstants {
public static final String SERIALIZABLE_MODEL = "serializableModel";
public static final String SERIALIZABLE_MODEL_DESC = "boolean - toggle \"implements Serializable\" for generated models";
public static final String LIBRARY = "library";
public static final String LIBRARY_DESC = "library template (sub-template)";
}

View File

@@ -0,0 +1,50 @@
package io.swagger.codegen.auth;
import io.swagger.models.auth.AuthorizationValue;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
public class AuthParser {
public static List<AuthorizationValue> parse(String urlEncodedAuthStr) {
List<AuthorizationValue> auths = new ArrayList<AuthorizationValue>();
if (isNotEmpty(urlEncodedAuthStr)) {
String[] parts = urlEncodedAuthStr.split(",");
for (String part : parts) {
String[] kvPair = part.split(":");
if (kvPair.length == 2) {
auths.add(new AuthorizationValue(URLDecoder.decode(kvPair[0]), URLDecoder.decode(kvPair[1]), "header"));
}
}
}
return auths;
}
public static String reconstruct(List<AuthorizationValue> authorizationValueList) {
if (authorizationValueList != null) {
StringBuilder b = new StringBuilder();
for (AuthorizationValue v : authorizationValueList) {
try {
if (b.toString().length() > 0) {
b.append(",");
}
b.append(URLEncoder.encode(v.getKeyName(), "UTF-8"))
.append(":")
.append(URLEncoder.encode(v.getValue(), "UTF-8"));
} catch (Exception e) {
// continue
e.printStackTrace();
}
}
return b.toString();
} else {
return null;
}
}
}

View File

@@ -1,20 +1,28 @@
package io.swagger.codegen.config;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
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.auth.AuthParser;
import io.swagger.models.Swagger;
import io.swagger.models.auth.AuthorizationValue;
import io.swagger.parser.SwaggerParser;
import io.swagger.util.Json;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -34,7 +42,7 @@ public class CodegenConfigurator {
private String inputSpec;
private String outputDir;
private boolean verbose = false;
private boolean skipOverwrite;
private boolean skipOverwrite = false;
private String templateDir;
private String auth;
private String apiPackage;
@@ -43,6 +51,7 @@ public class CodegenConfigurator {
private String groupId;
private String artifactId;
private String artifactVersion;
private String library;
private Map<String, String> systemProperties = new HashMap<String, String>();
private Map<String, String> instantiationTypes = new HashMap<String, String>();
private Map<String, String> typeMappings = new HashMap<String, String>();
@@ -50,6 +59,8 @@ public class CodegenConfigurator {
private Map<String, String> importMappings = new HashMap<String, String>();
private Set<String> languageSpecificPrimitives = new HashSet<String>();
private final Map<String, String> dynamicProperties = new HashMap<String, String>(); //the map that holds the JsonAnySetter/JsonAnyGetter values
public CodegenConfigurator() {
this.setOutputDir(".");
}
@@ -255,6 +266,15 @@ public class CodegenConfigurator {
return this;
}
public String getLibrary() {
return library;
}
public CodegenConfigurator setLibrary(String library) {
this.library = library;
return this;
}
public ClientOptInput toClientOptInput() {
Validate.notEmpty(lang, "language must be specified");
@@ -281,7 +301,8 @@ public class CodegenConfigurator {
checkAndSetAdditionalProperty(artifactVersion, CodegenConstants.ARTIFACT_VERSION);
checkAndSetAdditionalProperty(templateDir, toAbsolutePathStr(templateDir), CodegenConstants.TEMPLATE_DIR);
final String library = additionalProperties.remove(CodegenConstants.LIBRARY);
handleDynamicProperties(config);
if (isNotEmpty(library)) {
config.setLibrary(library);
}
@@ -289,16 +310,37 @@ public class CodegenConfigurator {
config.additionalProperties().putAll(additionalProperties);
ClientOptInput input = new ClientOptInput()
.config(config)
.auth(auth);
.config(config);
Swagger swagger = new SwaggerParser().read(inputSpec, input.getAuthorizationValues(), true);
final List<AuthorizationValue> authorizationValues = AuthParser.parse(auth);
Swagger swagger = new SwaggerParser().read(inputSpec, authorizationValues, true);
input.opts(new ClientOpts())
.swagger(swagger);
return input;
}
@JsonAnySetter
public void addDynamicProperty(String name, Object value) {
if (value instanceof String) {
dynamicProperties.put(name, (String) value);
}
}
@JsonAnyGetter
public Map<String, String> getDynamicProperties() {
return dynamicProperties;
}
private void handleDynamicProperties(CodegenConfig codegenConfig) {
for (CliOption langCliOption : codegenConfig.cliOptions()) {
String opt = langCliOption.getOpt();
if (dynamicProperties.containsKey(opt)) {
codegenConfig.additionalProperties().put(opt, dynamicProperties.get(opt));
}
}
}
private void setVerboseFlags() {
@@ -342,4 +384,17 @@ public class CodegenConfigurator {
}
}
public static CodegenConfigurator fromFile(String configFile) {
if(isNotEmpty(configFile)) {
try {
CodegenConfigurator result = Json.mapper().readValue(new File(configFile), CodegenConfigurator.class);
return result;
} catch (IOException e) {
LOG.error("Unable to deserialize config file: " + configFile, e);
}
}
return null;
}
}

View File

@@ -0,0 +1,347 @@
package io.swagger.codegen.config;
import io.swagger.codegen.ClientOptInput;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenConfigLoader;
import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.auth.AuthParser;
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 mockit.Tested;
import org.testng.annotations.Test;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
public class CodegenConfiguratorTest {
@Mocked
SwaggerParser parser;
@Mocked
AuthParser authParser;
@Injectable
Swagger swagger;
@Mocked
CodegenConfigLoader codegenConfigLoader;
@Injectable
List<AuthorizationValue> authorizationValues;
@Tested
CodegenConfigurator configurator;
@Test
public void testVerbose() throws Exception {
configurator.setVerbose(true);
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(configurator);
}
@Test
public void testTemplateDir() throws Exception {
final String templateDir = "src/test/resources";
configurator.setTemplateDir(templateDir);
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertValueInMap(clientOptInput.getConfig().additionalProperties(), CodegenConstants.TEMPLATE_DIR, toAbsolutePathDir(templateDir));
}
@Test
public void testSystemProperties() throws Exception {
configurator.addSystemProperty("hello", "world")
.addSystemProperty("foo", "bar");
new Expectations(System.class) {{
System.setProperty("hello", "world");
times = 1;
System.setProperty("foo", "bar");
times = 1;
}};
setupAndRunGenericTest(configurator);
}
@Test
public void testSkipOverwrite() throws Exception {
CodegenConfigurator configurator1 = new CodegenConfigurator();
configurator1.setSkipOverwrite(true);
ClientOptInput clientOptInput = setupAndRunGenericTest(configurator1);
assertTrue(clientOptInput.getConfig().isSkipOverwrite());
CodegenConfigurator configurator2 = new CodegenConfigurator();
configurator1.setSkipOverwrite(true);
clientOptInput = setupAndRunGenericTest(configurator2);
assertFalse(clientOptInput.getConfig().isSkipOverwrite());
}
@Test
public void testApiPackage() throws Exception {
final String apiPackage = "io.foo.bar.api";
configurator.setApiPackage(apiPackage);
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertValueInMap(clientOptInput.getConfig().additionalProperties(), CodegenConstants.API_PACKAGE, apiPackage);
}
@Test
public void testModelPackage() throws Exception {
final String modelPackage = "io.foo.bar.models";
configurator.setModelPackage(modelPackage);
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertValueInMap(clientOptInput.getConfig().additionalProperties(), CodegenConstants.MODEL_PACKAGE, modelPackage);
}
@Test
public void testInstantiationTypes() throws Exception {
configurator.addInstantiationType("foo", "bar")
.addInstantiationType("hello", "world");
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertValueInMap(clientOptInput.getConfig().instantiationTypes(), "foo", "bar");
assertValueInMap(clientOptInput.getConfig().instantiationTypes(), "hello", "world");
}
@Test
public void testTypeMappings() throws Exception {
configurator.addTypeMapping("foo", "bar")
.addTypeMapping("hello", "world");
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertValueInMap(clientOptInput.getConfig().typeMapping(), "foo", "bar");
assertValueInMap(clientOptInput.getConfig().typeMapping(), "hello", "world");
}
@Test
public void testAdditionalProperties() throws Exception {
configurator.addAdditionalProperty("foo", "bar")
.addAdditionalProperty("hello", "world");
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertValueInMap(clientOptInput.getConfig().additionalProperties(), "foo", "bar");
assertValueInMap(clientOptInput.getConfig().additionalProperties(), "hello", "world");
}
@Test
public void testLanguageSpecificPrimitives() throws Exception {
configurator.addLanguageSpecificPrimitive("foo")
.addLanguageSpecificPrimitive("bar")
.addLanguageSpecificPrimitive("hello")
.addLanguageSpecificPrimitive("world");
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertTrue(clientOptInput.getConfig().languageSpecificPrimitives().contains("foo"));
assertTrue(clientOptInput.getConfig().languageSpecificPrimitives().contains("bar"));
assertTrue(clientOptInput.getConfig().languageSpecificPrimitives().contains("hello"));
assertTrue(clientOptInput.getConfig().languageSpecificPrimitives().contains("world"));
}
@Test
public void testImportMappings() throws Exception {
configurator.addImportMapping("foo", "bar")
.addImportMapping("hello", "world");
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertValueInMap(clientOptInput.getConfig().importMapping(), "foo", "bar");
assertValueInMap(clientOptInput.getConfig().importMapping(), "hello", "world");
}
@Test
public void testInvokerPackage() throws Exception {
final String invokerPackage = "io.foo.bar.models";
configurator.setInvokerPackage(invokerPackage);
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertValueInMap(clientOptInput.getConfig().additionalProperties(), CodegenConstants.INVOKER_PACKAGE, invokerPackage);
}
@Test
public void testGroupId() throws Exception {
final String expectedValue = "io.foo.bar.models";
configurator.setGroupId(expectedValue);
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertValueInMap(clientOptInput.getConfig().additionalProperties(), CodegenConstants.GROUP_ID, expectedValue);
}
@Test
public void testArtifactId() throws Exception {
final String expectedValue = "io.foo.bar.models";
configurator.setArtifactId(expectedValue);
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertValueInMap(clientOptInput.getConfig().additionalProperties(), CodegenConstants.ARTIFACT_ID, expectedValue);
}
@Test
public void testArtifactVersion() throws Exception {
final String expectedValue = "1.2.3";
configurator.setArtifactVersion(expectedValue);
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertValueInMap(clientOptInput.getConfig().additionalProperties(), CodegenConstants.ARTIFACT_VERSION, expectedValue);
}
@Test
public void testLibrary() throws Exception {
final String expectedValue = "jersey2";
configurator.setLibrary(expectedValue);
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertEquals(clientOptInput.getConfig().getLibrary(), expectedValue);
}
@Test
public void testDynamicProperties() throws Exception {
configurator.addDynamicProperty(CodegenConstants.LOCAL_VARIABLE_PREFIX, "_");
final ClientOptInput clientOptInput = setupAndRunGenericTest(configurator);
assertValueInMap(clientOptInput.getConfig().additionalProperties(), CodegenConstants.LOCAL_VARIABLE_PREFIX, "_");
}
@Test
public void testFromFile() throws Exception {
final CodegenConfigurator configurator = CodegenConfigurator.fromFile("src/test/resources/sampleConfig.json");
assertEquals(configurator.getLang(), "java");
assertEquals(configurator.getInputSpec(), "swagger.yaml");
assertEquals(configurator.getOutputDir(), toAbsolutePathDir("src/gen/java"));
assertEquals(configurator.isVerbose(), true);
assertEquals(configurator.isSkipOverwrite(), true);
assertEquals(configurator.getTemplateDir(), toAbsolutePathDir("src/main/resources"));
assertEquals(configurator.getAuth(), "hello:world");
assertEquals(configurator.getApiPackage(), "io.something.api");
assertEquals(configurator.getModelPackage(), "io.something.models");
assertEquals(configurator.getInvokerPackage(), "io.something.invoker");
assertEquals(configurator.getGroupId(), "io.something");
assertEquals(configurator.getArtifactId(), "awesome-api");
assertEquals(configurator.getArtifactVersion(), "1.2.3");
assertEquals(configurator.getLibrary(), "jersey2");
assertEquals(configurator.getSystemProperties().size(), 1);
assertValueInMap(configurator.getSystemProperties(), "systemProp1", "value1");
assertEquals(configurator.getInstantiationTypes().size(), 1);
assertValueInMap(configurator.getInstantiationTypes(), "hello", "world");
assertEquals(configurator.getTypeMappings().size(), 1);
assertValueInMap(configurator.getTypeMappings(), "foo", "bar");
assertEquals(configurator.getAdditionalProperties().size(), 1);
assertValueInMap(configurator.getAdditionalProperties(), "addtProp1", "value2");
assertEquals(configurator.getImportMappings().size(), 1);
assertValueInMap(configurator.getImportMappings(), "type1", "import1");
assertEquals(configurator.getLanguageSpecificPrimitives().size(), 1);
assertTrue(configurator.getLanguageSpecificPrimitives().contains("rolex"));
assertEquals(configurator.getDynamicProperties().size(), 1);
assertValueInMap(configurator.getDynamicProperties(), CodegenConstants.LOCAL_VARIABLE_PREFIX, "_");
}
private ClientOptInput setupAndRunGenericTest(CodegenConfigurator configurator) {
final String spec = "swagger.yaml";
final String lang = "java";
final String outputDir = "src/test/java";
final String expectedAuth = "hello:world";
configurator.setLang(lang)
.setOutputDir(outputDir)
.setInputSpec(spec)
.setAuth(expectedAuth);
final CodegenConfig config = new JavaClientCodegen();
setupStandardExpectations(spec, lang, configurator.getAuth(), config);
ClientOptInput result = configurator.toClientOptInput();
new FullVerifications() {{
}};
final String expectedOutputDir = toAbsolutePathDir(outputDir);
assertEquals(result.getConfig().getOutputDir(), expectedOutputDir);
return result;
}
private String toAbsolutePathDir(String outputDir) {
return Paths.get(outputDir).toAbsolutePath().toAbsolutePath().toString();
}
private void setupStandardExpectations(final String spec, final String languageName, final String auth, final CodegenConfig config) {
new StrictExpectations() {{
CodegenConfigLoader.forName(languageName);
times = 1;
result = config;
AuthParser.parse(auth); times=1; result = authorizationValues;
new SwaggerParser();
times = 1;
result = parser;
parser.read(spec, authorizationValues, true);
times = 1;
result = swagger;
}};
}
private void assertValueInMap(Map map, String propertyKey, String expectedPropertyValue) {
assertTrue(map.containsKey(propertyKey));
assertEquals(map.get(propertyKey), expectedPropertyValue);
}
}

View File

@@ -0,0 +1,33 @@
{
"lang" : "java",
"inputSpec" : "swagger.yaml",
"outputDir" : "src/gen/java",
"verbose" : true,
"skipOverwrite" : true,
"templateDir" : "src/main/resources",
"auth" : "hello:world",
"apiPackage" : "io.something.api",
"modelPackage" : "io.something.models",
"invokerPackage" : "io.something.invoker",
"groupId" : "io.something",
"artifactId" : "awesome-api",
"artifactVersion" : "1.2.3",
"library" : "jersey2",
"systemProperties" : {
"systemProp1" : "value1"
},
"instantiationTypes" : {
"hello" : "world"
},
"typeMappings" : {
"foo" : "bar"
},
"additionalProperties" : {
"addtProp1" : "value2"
},
"importMappings" : {
"type1" : "import1"
},
"languageSpecificPrimitives" : [ "rolex" ],
"localVariablePrefix" : "_"
}