From f8cafaaee7d6a9ac69de3c2fcc6c7056ae8c8c53 Mon Sep 17 00:00:00 2001 From: hrachya Date: Tue, 26 May 2015 15:49:32 -0700 Subject: [PATCH 01/21] new CliOption class for wrapping org.apache.commons.cli.Option --- .../wordnik/swagger/codegen/CliOption.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CliOption.java diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CliOption.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CliOption.java new file mode 100644 index 00000000000..837759b9d2f --- /dev/null +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CliOption.java @@ -0,0 +1,44 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.wordnik.swagger.codegen; + +import org.apache.commons.cli.Option; + +public class CliOption extends Option { + + private Boolean langSpecific = false; + + public CliOption(String opt, String description) throws IllegalArgumentException { + super(opt, description); + } + + public CliOption(String opt, boolean hasArg, String description) throws IllegalArgumentException { + super(opt, hasArg, description); + } + + public CliOption(String opt, String longOpt, boolean hasArg, String description) throws IllegalArgumentException { + super(opt, longOpt, hasArg, description); + } + + public CliOption(String opt, String description, Boolean langSpecific) throws IllegalArgumentException { + this(opt, description); + this.langSpecific = langSpecific; + } + + public CliOption(String opt, boolean hasArg, String description, Boolean langSpecific) throws IllegalArgumentException { + this(opt, hasArg, description); + this.langSpecific = langSpecific; + } + + public CliOption(String opt, String longOpt, boolean hasArg, String description, Boolean langSpecific) throws IllegalArgumentException { + this(opt, longOpt, hasArg, description); + this.langSpecific = langSpecific; + } + + public Boolean isLangSpecific() { + return langSpecific; + } +} From 9f1504062af24a7a589f2e5112a5ed9356e88881 Mon Sep 17 00:00:00 2001 From: hrachya Date: Tue, 26 May 2015 15:52:25 -0700 Subject: [PATCH 02/21] add List cliOptions() method to CodegenConfig interface with default implementation in DefaultCodegen --- .../main/java/com/wordnik/swagger/codegen/CodegenConfig.java | 1 + .../java/com/wordnik/swagger/codegen/DefaultCodegen.java | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CodegenConfig.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CodegenConfig.java index 167610fcd70..b112923b6d7 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CodegenConfig.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CodegenConfig.java @@ -27,6 +27,7 @@ public interface CodegenConfig { String getTypeDeclaration(Property p); String getTypeDeclaration(String name); void processOpts(); + List cliOptions(); String generateExamplePath(String path, Operation operation); Set reservedWords(); diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java index 2513bedfcb9..4bb7940b148 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java @@ -77,6 +77,11 @@ public class DefaultCodegen { protected String templateDir; protected Map additionalProperties = new HashMap(); protected List supportingFiles = new ArrayList(); + protected List cliOptions = new ArrayList(); + + public List cliOptions() { + return cliOptions; + } public void processOpts(){ if(additionalProperties.containsKey("templateDir")) { From 33436d62c3a50d3d5c0f56b938fdf6e2e2f36c7a Mon Sep 17 00:00:00 2001 From: hrachya Date: Tue, 26 May 2015 15:57:01 -0700 Subject: [PATCH 03/21] Get all cliOptions for available codegen configs and add to command line options Add all the specified ones to clientOpts properties --- .../com/wordnik/swagger/codegen/Codegen.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/Codegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/Codegen.java index efda171ff82..1adcc792a8e 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/Codegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/Codegen.java @@ -51,6 +51,30 @@ public class Codegen extends DefaultGenerator { options.addOption("d", "debug-info", false, "prints additional info for debugging"); options.addOption("a", "auth", true, "adds authorization headers when fetching the swagger definitions remotely. Pass in a URL-encoded string of name:header with a comma separating multiple values"); + //adding additional options from language specific configs + Option existingOption; + for (Map.Entry configEntry : configs.entrySet()) { + for (CliOption langCliOption : configEntry.getValue().cliOptions()) { + //lang specific option + if (langCliOption.isLangSpecific()) { + //option exists, update description + if (options.hasOption(langCliOption.getOpt())) { + existingOption = options.getOption(langCliOption.getOpt()); + existingOption.setDescription(existingOption.getDescription() + "\n" + configEntry.getKey() + ": " + langCliOption.getDescription()); + } else { + //new option, prepend 'lang: ' to the description and add + langCliOption.setDescription(configEntry.getKey() + ": " + langCliOption.getDescription()); + options.addOption((Option) langCliOption); + } + } else { + //not lang specific, add if not already there + if (!options.hasOption(langCliOption.getOpt())) { + options.addOption((Option) langCliOption); + } + } + } + } + ClientOptInput clientOptInput = new ClientOptInput(); ClientOpts clientOpts = new ClientOpts(); Swagger swagger = null; @@ -92,6 +116,13 @@ 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"))); + + //add all passed cliOptions to clientOpts.properties + for (CliOption langCliOption : clientOptInput.getConfig().cliOptions()) { + if (cmd.hasOption(langCliOption.getOpt())) { + clientOpts.getProperties().put(langCliOption.getOpt(), String.valueOf(cmd.getOptionValue(langCliOption.getOpt()))); + } + } } catch (Exception e) { usage(options); From d7dec5a43d5047c565247f99bd4cde486b9bdf61 Mon Sep 17 00:00:00 2001 From: hrachya Date: Tue, 26 May 2015 16:02:27 -0700 Subject: [PATCH 04/21] Use new functionality to add 't' as a command line option to set 'templateDir' --- modules/swagger-codegen/nbactions.xml | 46 +++++++++++++++++++ .../com/wordnik/swagger/codegen/Codegen.java | 3 -- .../swagger/codegen/DefaultCodegen.java | 6 ++- 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 modules/swagger-codegen/nbactions.xml diff --git a/modules/swagger-codegen/nbactions.xml b/modules/swagger-codegen/nbactions.xml new file mode 100644 index 00000000000..92802c5f44d --- /dev/null +++ b/modules/swagger-codegen/nbactions.xml @@ -0,0 +1,46 @@ + + + + run + + jar + + + process-classes + org.codehaus.mojo:exec-maven-plugin:1.2.1:exec + + + -classpath %classpath ${packageClassName} generate -i http://hyeghsealnxd1.bigfish.lan:83/gamestatus.json -l java -o /home/hrachya/Desktop/codegen/java -t blah + java + + + + debug + + jar + + + process-classes + org.codehaus.mojo:exec-maven-plugin:1.2.1:exec + + + -Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath ${packageClassName} generate -i http://hyeghsealnxd1.bigfish.lan:83/gamestatus.json -l java -o /home/hrachya/Desktop/codegen/java -t blah + java + true + + + + profile + + jar + + + process-classes + org.codehaus.mojo:exec-maven-plugin:1.2.1:exec + + + -classpath %classpath ${packageClassName} generate -i http://hyeghsealnxd1.bigfish.lan:83/gamestatus.json -l java -o /home/hrachya/Desktop/codegen/java -t blah + java + + + diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/Codegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/Codegen.java index 1adcc792a8e..76c95ae102f 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/Codegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/Codegen.java @@ -47,7 +47,6 @@ public class Codegen extends DefaultGenerator { options.addOption("l", "lang", true, "client language to generate.\nAvailable languages include:\n\t[" + configString + "]"); options.addOption("o", "output", true, "where to write the generated files"); options.addOption("i", "input-spec", true, "location of the swagger spec, as URL or file"); - options.addOption("t", "template-dir", true, "folder containing the template files"); options.addOption("d", "debug-info", false, "prints additional info for debugging"); options.addOption("a", "auth", true, "adds authorization headers when fetching the swagger definitions remotely. Pass in a URL-encoded string of name:header with a comma separating multiple values"); @@ -114,8 +113,6 @@ public class Codegen extends DefaultGenerator { } if (cmd.hasOption("i")) swagger = new SwaggerParser().read(cmd.getOptionValue("i"), clientOptInput.getAuthorizationValues(), true); - if (cmd.hasOption("t")) - clientOpts.getProperties().put("templateDir", String.valueOf(cmd.getOptionValue("t"))); //add all passed cliOptions to clientOpts.properties for (CliOption langCliOption : clientOptInput.getConfig().cliOptions()) { diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java index 4bb7940b148..97c5798f223 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java @@ -84,8 +84,8 @@ public class DefaultCodegen { } public void processOpts(){ - if(additionalProperties.containsKey("templateDir")) { - this.setTemplateDir((String)additionalProperties.get("templateDir")); + if(additionalProperties.containsKey("t")) { + this.setTemplateDir((String)additionalProperties.get("t")); } } @@ -286,6 +286,8 @@ public class DefaultCodegen { importMapping.put("LocalDateTime", "org.joda.time.*"); importMapping.put("LocalDate", "org.joda.time.*"); importMapping.put("LocalTime", "org.joda.time.*"); + + cliOptions.add(new CliOption("t", "template-dir", true, "folder containing the template files")); } From e08a5a9d83aeb979181ef1469ddc13ae90d6b134 Mon Sep 17 00:00:00 2001 From: hrachya Date: Tue, 26 May 2015 17:01:07 -0700 Subject: [PATCH 05/21] Add 'modelPackage' and 'apiPackage' as command line options Add setters for modelPackage and apiPackage --- .../swagger/codegen/DefaultCodegen.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java index 97c5798f223..6571990efc7 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java @@ -87,6 +87,14 @@ public class DefaultCodegen { if(additionalProperties.containsKey("t")) { this.setTemplateDir((String)additionalProperties.get("t")); } + + if(additionalProperties.containsKey("modelPackage")) { + this.setModelPackage((String)additionalProperties.get("modelPackage")); + } + + if(additionalProperties.containsKey("apiPackage")) { + this.setApiPackage((String)additionalProperties.get("apiPackage")); + } } // override with any special post-processing @@ -183,6 +191,14 @@ public class DefaultCodegen { this.templateDir = templateDir; } + public void setModelPackage(String modelPackage) { + this.modelPackage = modelPackage; + } + + public void setApiPackage(String apiPackage) { + this.apiPackage = apiPackage; + } + public String toApiFilename(String name) { return toApiName(name); } @@ -288,6 +304,8 @@ public class DefaultCodegen { importMapping.put("LocalTime", "org.joda.time.*"); cliOptions.add(new CliOption("t", "template-dir", true, "folder containing the template files")); + cliOptions.add(new CliOption("modelPackage", "model-package", true, "Package for generated models")); + cliOptions.add(new CliOption("apiPackage", "api-package", true, "Package for generated api classes")); } From 1e09f5149ee773fd970fce9da6901f0762f880fe Mon Sep 17 00:00:00 2001 From: hrachya Date: Tue, 26 May 2015 17:05:53 -0700 Subject: [PATCH 06/21] Add 'invokerPackage', 'groupId', 'arifactId', 'artifactVersion', 'sourceFolder' command line options for java (and all the derived ones jaxrs, spring-mvc ...) Add processOpts override to process java specific options Move supporting file initialization from constructor to processOpts, since it uses some of the options that are not yet set in the constructor Add setters --- .../codegen/languages/JavaClientCodegen.java | 88 ++++++++++++++++--- 1 file changed, 75 insertions(+), 13 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java index 7c0ac7422ff..622e1322a1d 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java @@ -45,19 +45,6 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { "native", "super", "while") ); - additionalProperties.put("invokerPackage", invokerPackage); - additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion); - - supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); - supportingFiles.add(new SupportingFile("apiInvoker.mustache", - (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiInvoker.java")); - supportingFiles.add(new SupportingFile("JsonUtil.mustache", - (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "JsonUtil.java")); - supportingFiles.add(new SupportingFile("apiException.mustache", - (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiException.java")); - languageSpecificPrimitives = new HashSet( Arrays.asList( "String", @@ -71,8 +58,65 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { ); instantiationTypes.put("array", "ArrayList"); instantiationTypes.put("map", "HashMap"); + + cliOptions.add(new CliOption("invokerPackage", "invoker-package", true, "Root package for generated code", true)); + cliOptions.add(new CliOption("groupId", "group-id", true, "groupId in pom.xml", true)); + cliOptions.add(new CliOption("artifactId", "artifact-id", true, "groupId in pom.xml", true)); + cliOptions.add(new CliOption("artifactVersion", "artifact-version", true, "artifact version in pom.xml", true)); + cliOptions.add(new CliOption("sourceFolder", "source-folder", true, "Source folder for generated code", true)); } + @Override + public void processOpts() { + super.processOpts(); + + if(additionalProperties.containsKey("invokerPackage")) { + this.setInvokerPackage((String)additionalProperties.get("invokerPackage")); + } + else{ + //not set, use default to be passed to template + additionalProperties.put("invokerPackage", invokerPackage); + } + + if(additionalProperties.containsKey("groupId")) { + this.setGroupId((String)additionalProperties.get("groupId")); + } + else{ + //not set, use to be passed to template + additionalProperties.put("groupId", groupId); + } + + if(additionalProperties.containsKey("artifactId")) { + this.setArtifactId((String)additionalProperties.get("artifactId")); + } + else{ + //not set, use to be passed to template + additionalProperties.put("artifactId", artifactId); + } + + if(additionalProperties.containsKey("artifactVersion")) { + this.setArtifactVersion((String)additionalProperties.get("artifactVersion")); + } + else{ + //not set, use to be passed to template + additionalProperties.put("artifactVersion", artifactVersion); + } + + if(additionalProperties.containsKey("sourceFolder")) { + this.setSourceFolder((String)additionalProperties.get("sourceFolder")); + } + + supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); + supportingFiles.add(new SupportingFile("apiInvoker.mustache", + (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiInvoker.java")); + supportingFiles.add(new SupportingFile("JsonUtil.mustache", + (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "JsonUtil.java")); + supportingFiles.add(new SupportingFile("apiException.mustache", + (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiException.java")); + } + + + @Override public String escapeReservedWord(String name) { return "_" + name; @@ -169,5 +213,23 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { return camelize(operationId, true); } + public void setInvokerPackage(String invokerPackage) { + this.invokerPackage = invokerPackage; + } + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + public void setArtifactId(String artifactId) { + this.artifactId = artifactId; + } + + public void setArtifactVersion(String artifactVersion) { + this.artifactVersion = artifactVersion; + } + + public void setSourceFolder(String sourceFolder) { + this.sourceFolder = sourceFolder; + } } From 74f5c769f9b5a31ed6980d26ab6940101d6cba81 Mon Sep 17 00:00:00 2001 From: hrachya Date: Tue, 26 May 2015 17:10:19 -0700 Subject: [PATCH 07/21] removing extra file, was committed by mistake --- modules/swagger-codegen/nbactions.xml | 46 --------------------------- 1 file changed, 46 deletions(-) delete mode 100644 modules/swagger-codegen/nbactions.xml diff --git a/modules/swagger-codegen/nbactions.xml b/modules/swagger-codegen/nbactions.xml deleted file mode 100644 index 92802c5f44d..00000000000 --- a/modules/swagger-codegen/nbactions.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - run - - jar - - - process-classes - org.codehaus.mojo:exec-maven-plugin:1.2.1:exec - - - -classpath %classpath ${packageClassName} generate -i http://hyeghsealnxd1.bigfish.lan:83/gamestatus.json -l java -o /home/hrachya/Desktop/codegen/java -t blah - java - - - - debug - - jar - - - process-classes - org.codehaus.mojo:exec-maven-plugin:1.2.1:exec - - - -Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath ${packageClassName} generate -i http://hyeghsealnxd1.bigfish.lan:83/gamestatus.json -l java -o /home/hrachya/Desktop/codegen/java -t blah - java - true - - - - profile - - jar - - - process-classes - org.codehaus.mojo:exec-maven-plugin:1.2.1:exec - - - -classpath %classpath ${packageClassName} generate -i http://hyeghsealnxd1.bigfish.lan:83/gamestatus.json -l java -o /home/hrachya/Desktop/codegen/java -t blah - java - - - From a9e767cbd8ed98c1b448bacabe84c533e0061c54 Mon Sep 17 00:00:00 2001 From: hrachya Date: Wed, 27 May 2015 17:19:34 -0700 Subject: [PATCH 08/21] Rolling back changes to deprecated class --- .../com/wordnik/swagger/codegen/Codegen.java | 36 +++---------------- 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/Codegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/Codegen.java index 76c95ae102f..c28031a9f27 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/Codegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/Codegen.java @@ -47,33 +47,10 @@ public class Codegen extends DefaultGenerator { options.addOption("l", "lang", true, "client language to generate.\nAvailable languages include:\n\t[" + configString + "]"); options.addOption("o", "output", true, "where to write the generated files"); options.addOption("i", "input-spec", true, "location of the swagger spec, as URL or file"); + options.addOption("t", "template-dir", true, "folder containing the template files"); options.addOption("d", "debug-info", false, "prints additional info for debugging"); options.addOption("a", "auth", true, "adds authorization headers when fetching the swagger definitions remotely. Pass in a URL-encoded string of name:header with a comma separating multiple values"); - //adding additional options from language specific configs - Option existingOption; - for (Map.Entry configEntry : configs.entrySet()) { - for (CliOption langCliOption : configEntry.getValue().cliOptions()) { - //lang specific option - if (langCliOption.isLangSpecific()) { - //option exists, update description - if (options.hasOption(langCliOption.getOpt())) { - existingOption = options.getOption(langCliOption.getOpt()); - existingOption.setDescription(existingOption.getDescription() + "\n" + configEntry.getKey() + ": " + langCliOption.getDescription()); - } else { - //new option, prepend 'lang: ' to the description and add - langCliOption.setDescription(configEntry.getKey() + ": " + langCliOption.getDescription()); - options.addOption((Option) langCliOption); - } - } else { - //not lang specific, add if not already there - if (!options.hasOption(langCliOption.getOpt())) { - options.addOption((Option) langCliOption); - } - } - } - } - ClientOptInput clientOptInput = new ClientOptInput(); ClientOpts clientOpts = new ClientOpts(); Swagger swagger = null; @@ -113,13 +90,8 @@ public class Codegen extends DefaultGenerator { } if (cmd.hasOption("i")) swagger = new SwaggerParser().read(cmd.getOptionValue("i"), clientOptInput.getAuthorizationValues(), true); - - //add all passed cliOptions to clientOpts.properties - for (CliOption langCliOption : clientOptInput.getConfig().cliOptions()) { - if (cmd.hasOption(langCliOption.getOpt())) { - clientOpts.getProperties().put(langCliOption.getOpt(), String.valueOf(cmd.getOptionValue(langCliOption.getOpt()))); - } - } + if (cmd.hasOption("t")) + clientOpts.getProperties().put("templateDir", String.valueOf(cmd.getOptionValue("t"))); } catch (Exception e) { usage(options); @@ -168,4 +140,4 @@ public class Codegen extends DefaultGenerator { } } } -} +} \ No newline at end of file From 2cca1a8c2c8ec33f2b34a43bed172e55b0bc0c7b Mon Sep 17 00:00:00 2001 From: hrachya Date: Wed, 27 May 2015 17:22:44 -0700 Subject: [PATCH 09/21] Changing templateDir back to explicit option for cli --- .../main/java/com/wordnik/swagger/codegen/DefaultCodegen.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java index 6571990efc7..f7ec12b2c94 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java @@ -84,7 +84,7 @@ public class DefaultCodegen { } public void processOpts(){ - if(additionalProperties.containsKey("t")) { + if(additionalProperties.containsKey("templateDir")) { this.setTemplateDir((String)additionalProperties.get("t")); } @@ -303,7 +303,6 @@ public class DefaultCodegen { importMapping.put("LocalDate", "org.joda.time.*"); importMapping.put("LocalTime", "org.joda.time.*"); - cliOptions.add(new CliOption("t", "template-dir", true, "folder containing the template files")); cliOptions.add(new CliOption("modelPackage", "model-package", true, "Package for generated models")); cliOptions.add(new CliOption("apiPackage", "api-package", true, "Package for generated api classes")); } From c3055c7cc456b523e88dceb38bf2d639cfa4ae25 Mon Sep 17 00:00:00 2001 From: hrachya Date: Wed, 27 May 2015 17:30:42 -0700 Subject: [PATCH 10/21] Adding Config and ConfigParser classes --- .../src/main/java/config/Config.java | 38 +++++++++++++++ .../src/main/java/config/ConfigParser.java | 46 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 modules/swagger-codegen/src/main/java/config/Config.java create mode 100644 modules/swagger-codegen/src/main/java/config/ConfigParser.java diff --git a/modules/swagger-codegen/src/main/java/config/Config.java b/modules/swagger-codegen/src/main/java/config/Config.java new file mode 100644 index 00000000000..745824bd816 --- /dev/null +++ b/modules/swagger-codegen/src/main/java/config/Config.java @@ -0,0 +1,38 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package config; + +import com.google.common.collect.ImmutableMap; +import java.util.HashMap; +import java.util.Map; + +public class Config { + private Map options; + + public Config() { + this.options = new HashMap(); + } + + public Config(Map properties) { + this.options = properties; + } + + public Map getOptions() { + return ImmutableMap.copyOf(options); + } + + public boolean hasOption(String opt){ + return options.containsKey(opt); + } + + public String getOption(String opt){ + return options.get(opt); + } + + public void setOption(String opt, String value){ + options.put(opt, value); + } +} diff --git a/modules/swagger-codegen/src/main/java/config/ConfigParser.java b/modules/swagger-codegen/src/main/java/config/ConfigParser.java new file mode 100644 index 00000000000..df2b37c2923 --- /dev/null +++ b/modules/swagger-codegen/src/main/java/config/ConfigParser.java @@ -0,0 +1,46 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package config; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.File; +import java.util.Iterator; +import java.util.Map; + +public class ConfigParser { + + public static Config read(String location) { + + System.out.println("reading cofig from " + location); + + ObjectMapper mapper = new ObjectMapper(); + + Config config = new Config(); + + try { + JsonNode rootNode = mapper.readTree(new File(location)); + Iterator> optionNodes = rootNode.fields(); + + while (optionNodes.hasNext()) { + Map.Entry optionNode = (Map.Entry) optionNodes.next(); + + if(optionNode.getValue().isValueNode()){ + config.setOption(optionNode.getKey(), optionNode.getValue().asText()); + } + else{ + System.out.println("omitting non-value node " + optionNode.getKey()); + } + } + } + catch (Exception e) { + System.out.println(e.getMessage()); + return null; + } + + return config; + } +} From c9c58cbe4c6115af273dd290f13d867acc5ff30f Mon Sep 17 00:00:00 2001 From: hrachya Date: Wed, 27 May 2015 17:37:07 -0700 Subject: [PATCH 11/21] new -c/--config option for swagger-cli to specify json config file path setting options passed in config file if those were allowed by lang specific CodegenConfig --- .../wordnik/swagger/codegen/cmd/Generate.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/Generate.java b/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/Generate.java index 47449d84286..9bd2b5e0c24 100644 --- a/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/Generate.java +++ b/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/Generate.java @@ -1,10 +1,13 @@ package com.wordnik.swagger.codegen.cmd; +import com.wordnik.swagger.codegen.CliOption; import com.wordnik.swagger.codegen.ClientOptInput; import com.wordnik.swagger.codegen.ClientOpts; import com.wordnik.swagger.codegen.CodegenConfig; import com.wordnik.swagger.codegen.DefaultGenerator; import com.wordnik.swagger.models.Swagger; +import config.Config; +import config.ConfigParser; import io.airlift.airline.Command; import io.airlift.airline.Option; import io.swagger.parser.SwaggerParser; @@ -57,6 +60,9 @@ public class Generate implements Runnable { @Option( name= {"-D"}, title = "system properties", description = "sets specified system properties in " + "the format of name=value,name=value") private String systemProperties; + + @Option( name= {"-c", "--config"}, title = "configuration file", description = "path to json configuration file") + private String configFile; @Override public void run() { @@ -76,6 +82,17 @@ public class Generate implements Runnable { if (null != templateDir) { config.additionalProperties().put(TEMPLATE_DIR_PARAM, new File(templateDir).getAbsolutePath()); } + + if(null != configFile){ + Config genConfig = ConfigParser.read(configFile); + if (null != genConfig) { + for (CliOption langCliOption : config.cliOptions()) { + if (genConfig.hasOption(langCliOption.getOpt())) { + config.additionalProperties().put(langCliOption.getOpt(), genConfig.getOption(langCliOption.getOpt())); + } + } + } + } input.setConfig(config); From 26a2290078726172bf3007071fb9288e3baaf582 Mon Sep 17 00:00:00 2001 From: hrachya Date: Thu, 28 May 2015 13:01:54 -0700 Subject: [PATCH 12/21] adding classPrefix, sourceFolder, projectName cli options to objc --- .../codegen/languages/ObjcClientCodegen.java | 51 +++++++++++++++---- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java index d1e227ce744..021ca0b3b21 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java @@ -1,6 +1,5 @@ package com.wordnik.swagger.codegen.languages; -import com.wordnik.swagger.util.Json; import com.wordnik.swagger.codegen.*; import com.wordnik.swagger.models.properties.*; @@ -10,7 +9,8 @@ import java.io.File; public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { protected Set foundationClasses = new HashSet(); protected String sourceFolder = "client"; - protected static String PREFIX = "SWG"; + protected String classPrefix = "SWG"; + protected String projectName = "swaggerClient"; public CodegenType getTag() { return CodegenType.CLIENT; @@ -34,12 +34,6 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { templateDir = "objc"; modelPackage = ""; - String appName = System.getProperty("appName"); - if(appName == null) { - appName = "swaggerClient"; - } - additionalProperties.put("projectName", appName); - defaultIncludes = new HashSet( Arrays.asList( "bool", @@ -111,7 +105,31 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { instantiationTypes.put("array", "NSMutableArray"); instantiationTypes.put("map", "NSMutableDictionary"); + + cliOptions.add(new CliOption("classPrefix", "class-prefix", true, "prefix for generated classes", true)); + cliOptions.add(new CliOption("sourceFolder", "source-folder", true, "source folder for generated code", true)); + cliOptions.add(new CliOption("projectName", "project-name", true, "name of the Xcode project in generated Podfile", true)); + } + @Override + public void processOpts() { + super.processOpts(); + + if(additionalProperties.containsKey("sourceFolder")) { + this.setSourceFolder((String)additionalProperties.get("sourceFolder")); + } + + if(additionalProperties.containsKey("classPrefix")) { + this.setClassPrefix((String)additionalProperties.get("classPrefix")); + } + + if(additionalProperties.containsKey("projectName")) { + this.setProjectName((String)additionalProperties.get("projectName")); + } + else{ + additionalProperties.put("projectName", projectName); + } + supportingFiles.add(new SupportingFile("SWGObject.h", sourceFolder, "SWGObject.h")); supportingFiles.add(new SupportingFile("SWGObject.m", sourceFolder, "SWGObject.m")); supportingFiles.add(new SupportingFile("SWGQueryParamCollection.h", sourceFolder, "SWGQueryParamCollection.h")); @@ -220,7 +238,7 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { } // custom classes else { - return PREFIX + camelize(type); + return classPrefix + camelize(type); } } @@ -266,11 +284,11 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { @Override public String toApiName(String name) { - return PREFIX + camelize(name) + "Api"; + return classPrefix + camelize(name) + "Api"; } public String toApiFilename(String name) { - return PREFIX + camelize(name) + "Api"; + return classPrefix + camelize(name) + "Api"; } @Override @@ -313,4 +331,15 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { return camelize(operationId, true); } + public void setSourceFolder(String sourceFolder) { + this.sourceFolder = sourceFolder; + } + + public void setClassPrefix(String classPrefix) { + this.classPrefix = classPrefix; + } + + public void setProjectName(String projectName) { + this.projectName = projectName; + } } From c5a7800ec61fa52feec9e5793b0cb358bc85a19a Mon Sep 17 00:00:00 2001 From: hrachya Date: Thu, 28 May 2015 13:02:30 -0700 Subject: [PATCH 13/21] fixing typo --- modules/swagger-codegen/src/main/java/config/ConfigParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/java/config/ConfigParser.java b/modules/swagger-codegen/src/main/java/config/ConfigParser.java index df2b37c2923..019061faa33 100644 --- a/modules/swagger-codegen/src/main/java/config/ConfigParser.java +++ b/modules/swagger-codegen/src/main/java/config/ConfigParser.java @@ -15,7 +15,7 @@ public class ConfigParser { public static Config read(String location) { - System.out.println("reading cofig from " + location); + System.out.println("reading config from " + location); ObjectMapper mapper = new ObjectMapper(); From 08647b337fcecc4343520f339490752d84dff9c4 Mon Sep 17 00:00:00 2001 From: hrachya Date: Thu, 28 May 2015 13:08:52 -0700 Subject: [PATCH 14/21] fixing property name --- .../main/java/com/wordnik/swagger/codegen/DefaultCodegen.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java index f7ec12b2c94..7a689164449 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java @@ -85,7 +85,7 @@ public class DefaultCodegen { public void processOpts(){ if(additionalProperties.containsKey("templateDir")) { - this.setTemplateDir((String)additionalProperties.get("t")); + this.setTemplateDir((String)additionalProperties.get("templateDir")); } if(additionalProperties.containsKey("modelPackage")) { From 1f360961e0bff365b3c0ad3f19a8e9f9c8829c5e Mon Sep 17 00:00:00 2001 From: hrachya Date: Thu, 28 May 2015 14:55:36 -0700 Subject: [PATCH 15/21] Simplifying CliOption class --- .../wordnik/swagger/codegen/CliOption.java | 40 ++++++------------- .../swagger/codegen/DefaultCodegen.java | 4 +- .../codegen/languages/JavaClientCodegen.java | 10 ++--- .../codegen/languages/ObjcClientCodegen.java | 6 +-- 4 files changed, 22 insertions(+), 38 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CliOption.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CliOption.java index 837759b9d2f..4294d097116 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CliOption.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CliOption.java @@ -5,40 +5,24 @@ */ package com.wordnik.swagger.codegen; -import org.apache.commons.cli.Option; +public class CliOption { + private final String opt; + private String description; -public class CliOption extends Option { - - private Boolean langSpecific = false; - - public CliOption(String opt, String description) throws IllegalArgumentException { - super(opt, description); + public CliOption(String opt, String description) { + this.opt = opt; + this.description = description; } - public CliOption(String opt, boolean hasArg, String description) throws IllegalArgumentException { - super(opt, hasArg, description); + public String getOpt() { + return opt; } - public CliOption(String opt, String longOpt, boolean hasArg, String description) throws IllegalArgumentException { - super(opt, longOpt, hasArg, description); + public String getDescription() { + return description; } - public CliOption(String opt, String description, Boolean langSpecific) throws IllegalArgumentException { - this(opt, description); - this.langSpecific = langSpecific; - } - - public CliOption(String opt, boolean hasArg, String description, Boolean langSpecific) throws IllegalArgumentException { - this(opt, hasArg, description); - this.langSpecific = langSpecific; - } - - public CliOption(String opt, String longOpt, boolean hasArg, String description, Boolean langSpecific) throws IllegalArgumentException { - this(opt, longOpt, hasArg, description); - this.langSpecific = langSpecific; - } - - public Boolean isLangSpecific() { - return langSpecific; + public void setDescription(String description) { + this.description = description; } } diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java index 7a689164449..d7788ef8865 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java @@ -303,8 +303,8 @@ public class DefaultCodegen { importMapping.put("LocalDate", "org.joda.time.*"); importMapping.put("LocalTime", "org.joda.time.*"); - cliOptions.add(new CliOption("modelPackage", "model-package", true, "Package for generated models")); - cliOptions.add(new CliOption("apiPackage", "api-package", true, "Package for generated api classes")); + cliOptions.add(new CliOption("modelPackage", "package for generated models")); + cliOptions.add(new CliOption("apiPackage", "package for generated api classes")); } diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java index 622e1322a1d..736b533ebc6 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java @@ -59,11 +59,11 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { instantiationTypes.put("array", "ArrayList"); instantiationTypes.put("map", "HashMap"); - cliOptions.add(new CliOption("invokerPackage", "invoker-package", true, "Root package for generated code", true)); - cliOptions.add(new CliOption("groupId", "group-id", true, "groupId in pom.xml", true)); - cliOptions.add(new CliOption("artifactId", "artifact-id", true, "groupId in pom.xml", true)); - cliOptions.add(new CliOption("artifactVersion", "artifact-version", true, "artifact version in pom.xml", true)); - cliOptions.add(new CliOption("sourceFolder", "source-folder", true, "Source folder for generated code", true)); + 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")); } @Override diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java index 021ca0b3b21..75d33511a9e 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java @@ -106,9 +106,9 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { instantiationTypes.put("array", "NSMutableArray"); instantiationTypes.put("map", "NSMutableDictionary"); - cliOptions.add(new CliOption("classPrefix", "class-prefix", true, "prefix for generated classes", true)); - cliOptions.add(new CliOption("sourceFolder", "source-folder", true, "source folder for generated code", true)); - cliOptions.add(new CliOption("projectName", "project-name", true, "name of the Xcode project in generated Podfile", true)); + cliOptions.add(new CliOption("classPrefix", "prefix for generated classes")); + cliOptions.add(new CliOption("sourceFolder", "source folder for generated code")); + cliOptions.add(new CliOption("projectName", "name of the Xcode project in generated Podfile")); } @Override From ba7025d54bd8f7dad69d808b125c50e679631463 Mon Sep 17 00:00:00 2001 From: hrachya Date: Thu, 28 May 2015 15:02:32 -0700 Subject: [PATCH 16/21] Adding config-help command --- .../swagger/codegen/SwaggerCodegen.java | 4 +- .../swagger/codegen/cmd/ConfigHelp.java | 54 +++++++++++++++++++ .../wordnik/swagger/codegen/cmd/Generate.java | 4 +- 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/ConfigHelp.java diff --git a/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/SwaggerCodegen.java b/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/SwaggerCodegen.java index c8c718fb67b..f2c794776ce 100644 --- a/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/SwaggerCodegen.java +++ b/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/SwaggerCodegen.java @@ -1,5 +1,6 @@ package com.wordnik.swagger.codegen; +import com.wordnik.swagger.codegen.cmd.ConfigHelp; import com.wordnik.swagger.codegen.cmd.Generate; import com.wordnik.swagger.codegen.cmd.Langs; import com.wordnik.swagger.codegen.cmd.Meta; @@ -27,7 +28,8 @@ public class SwaggerCodegen { Generate.class, Meta.class, Langs.class, - Help.class + Help.class, + ConfigHelp.class ); builder.build().parse(args).run(); diff --git a/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/ConfigHelp.java b/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/ConfigHelp.java new file mode 100644 index 00000000000..6934bcf702e --- /dev/null +++ b/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/ConfigHelp.java @@ -0,0 +1,54 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.wordnik.swagger.codegen.cmd; + +import com.wordnik.swagger.codegen.CliOption; +import com.wordnik.swagger.codegen.CodegenConfig; +import io.airlift.airline.Command; +import io.airlift.airline.Option; +import java.util.ServiceLoader; +import static java.util.ServiceLoader.load; + +@Command(name = "config-help", description = "Config help for chosen lang") +public class ConfigHelp implements Runnable { + + @Option(name = {"-l", "--lang"}, title = "language", required = true, + description = "language to get config help for") + private String lang; + + @Override + public void run() { + System.out.println(); + CodegenConfig config = forName(lang); + System.out.println("CONFIG OPTIONS"); + for (CliOption langCliOption : config.cliOptions()) { + System.out.println("\t" + langCliOption.getOpt()); + System.out.println("\t " + langCliOption.getDescription()); + System.out.println(); + } + } + + /** + * 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); + } + } +} diff --git a/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/Generate.java b/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/Generate.java index 9bd2b5e0c24..3485eb8dd24 100644 --- a/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/Generate.java +++ b/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/Generate.java @@ -61,7 +61,9 @@ public class Generate implements Runnable { "the format of name=value,name=value") private String systemProperties; - @Option( name= {"-c", "--config"}, title = "configuration file", description = "path to json configuration file") + @Option( name= {"-c", "--config"}, title = "configuration file", description = "Path to json configuration file. " + + "File content should be in a json format {\"optionKey\":\"optionValue\", \"optionKey1\":\"optionValue1\"...} " + + "Supported options can be different for each language. Run config-help -l {lang} command for language specific config options.") private String configFile; @Override From 34315f733822ac4a6cab16e703a90e323b990cd3 Mon Sep 17 00:00:00 2001 From: Ron Date: Fri, 29 May 2015 08:52:29 -0400 Subject: [PATCH 17/21] Create LICENSE --- LICENSE | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000000..542991f9550 --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +Copyright 2015 SmartBear Software + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at [apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. From 7d7ed15fdcb4d79bbf9c197ed6b8b43807ac1a5c Mon Sep 17 00:00:00 2001 From: Ron Date: Fri, 29 May 2015 08:52:48 -0400 Subject: [PATCH 18/21] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cb649e07da5..dc3214c9831 100644 --- a/README.md +++ b/README.md @@ -273,7 +273,7 @@ Note! The templates are included in the library generated. If you want to modi License ------- -Copyright 2015 Reverb Technologies, Inc. +Copyright 2015 SmartBear Software Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. From ec08d31031ba540d2beea3767992d9550fe122f8 Mon Sep 17 00:00:00 2001 From: Oleg Godovykh Date: Wed, 29 Apr 2015 17:18:20 -0400 Subject: [PATCH 19/21] add template for Retrofit and Gson --- bin/all-petstore.sh | 1 + bin/retrofit-petstore.sh | 31 +++ .../swagger/codegen/CodegenOperation.java | 2 +- .../languages/RetrofitClientCodegen.java | 190 ++++++++++++++++++ .../com.wordnik.swagger.codegen.CodegenConfig | 1 + .../src/main/resources/retrofit/api.mustache | 29 +++ .../resources/retrofit/bodyParams.mustache | 1 + .../resources/retrofit/formParams.mustache | 1 + .../resources/retrofit/headerParams.mustache | 1 + .../main/resources/retrofit/model.mustache | 50 +++++ .../resources/retrofit/pathParams.mustache | 1 + .../src/main/resources/retrofit/pom.mustache | 143 +++++++++++++ .../resources/retrofit/queryParams.mustache | 1 + .../main/resources/retrofit/service.mustache | 23 +++ pom.xml | 12 ++ .../src/main/java/AndroidManifest.xml | 3 + samples/client/petstore/retrofit/pom.xml | 143 +++++++++++++ .../io/swagger/client/ServiceGenerator.java | 23 +++ .../java/io/swagger/client/api/PetApi.java | 117 +++++++++++ .../java/io/swagger/client/api/StoreApi.java | 60 ++++++ .../java/io/swagger/client/api/UserApi.java | 110 ++++++++++ .../io/swagger/client/model/Category.java | 49 +++++ .../java/io/swagger/client/model/Order.java | 110 ++++++++++ .../java/io/swagger/client/model/Pet.java | 112 +++++++++++ .../java/io/swagger/client/model/Tag.java | 49 +++++ .../java/io/swagger/client/model/User.java | 134 ++++++++++++ .../io/swagger/petstore/test/PetApiTest.java | 158 +++++++++++++++ .../swagger/petstore/test/StoreApiTest.java | 70 +++++++ .../io/swagger/petstore/test/UserApiTest.java | 84 ++++++++ 29 files changed, 1708 insertions(+), 1 deletion(-) create mode 100755 bin/retrofit-petstore.sh create mode 100644 modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/RetrofitClientCodegen.java create mode 100644 modules/swagger-codegen/src/main/resources/retrofit/api.mustache create mode 100644 modules/swagger-codegen/src/main/resources/retrofit/bodyParams.mustache create mode 100644 modules/swagger-codegen/src/main/resources/retrofit/formParams.mustache create mode 100644 modules/swagger-codegen/src/main/resources/retrofit/headerParams.mustache create mode 100644 modules/swagger-codegen/src/main/resources/retrofit/model.mustache create mode 100644 modules/swagger-codegen/src/main/resources/retrofit/pathParams.mustache create mode 100644 modules/swagger-codegen/src/main/resources/retrofit/pom.mustache create mode 100644 modules/swagger-codegen/src/main/resources/retrofit/queryParams.mustache create mode 100644 modules/swagger-codegen/src/main/resources/retrofit/service.mustache create mode 100644 samples/client/petstore/android-java/src/main/java/AndroidManifest.xml create mode 100644 samples/client/petstore/retrofit/pom.xml create mode 100644 samples/client/petstore/retrofit/src/main/java/io/swagger/client/ServiceGenerator.java create mode 100644 samples/client/petstore/retrofit/src/main/java/io/swagger/client/api/PetApi.java create mode 100644 samples/client/petstore/retrofit/src/main/java/io/swagger/client/api/StoreApi.java create mode 100644 samples/client/petstore/retrofit/src/main/java/io/swagger/client/api/UserApi.java create mode 100644 samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Category.java create mode 100644 samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Order.java create mode 100644 samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Pet.java create mode 100644 samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Tag.java create mode 100644 samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/User.java create mode 100644 samples/client/petstore/retrofit/src/test/java/io/swagger/petstore/test/PetApiTest.java create mode 100644 samples/client/petstore/retrofit/src/test/java/io/swagger/petstore/test/StoreApiTest.java create mode 100644 samples/client/petstore/retrofit/src/test/java/io/swagger/petstore/test/UserApiTest.java diff --git a/bin/all-petstore.sh b/bin/all-petstore.sh index 0f97790e75a..72797973a32 100755 --- a/bin/all-petstore.sh +++ b/bin/all-petstore.sh @@ -27,6 +27,7 @@ cd $APP_DIR ./bin/qt5-petstore.sh ./bin/php-petstore.sh ./bin/python-petstore.sh +./bin/retrofit-petstore.sh ./bin/ruby-petstore.sh ./bin/objc-petstore.sh ./bin/scala-petstore.sh diff --git a/bin/retrofit-petstore.sh b/bin/retrofit-petstore.sh new file mode 100755 index 00000000000..10b82e61963 --- /dev/null +++ b/bin/retrofit-petstore.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +SCRIPT="$0" + +while [ -h "$SCRIPT" ] ; do + ls=`ls -ld "$SCRIPT"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + SCRIPT="$link" + else + SCRIPT=`dirname "$SCRIPT"`/"$link" + fi +done + +if [ ! -d "${APP_DIR}" ]; then + APP_DIR=`dirname "$SCRIPT"`/.. + APP_DIR=`cd "${APP_DIR}"; pwd` +fi + +executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar" + +if [ ! -f "$executable" ] +then + mvn clean package +fi + +# if you've executed sbt assembly previously it will use that instead. +export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" +ags="$@ generate -t modules/swagger-codegen/src/main/resources/retrofit -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l retrofit -o samples/client/petstore/retrofit" + +java $JAVA_OPTS -jar $executable $ags diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CodegenOperation.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CodegenOperation.java index 7f8d390f371..76055110b92 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CodegenOperation.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CodegenOperation.java @@ -7,7 +7,7 @@ import java.util.*; public class CodegenOperation { public Boolean hasConsumes, hasProduces, hasParams, returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMapContainer, isListContainer, - hasMore = Boolean.TRUE; + hasMore = Boolean.TRUE, isMultipart; public String path, operationId, returnType, httpMethod, returnBaseType, returnContainer, summary, notes, baseName, defaultResponse; diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/RetrofitClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/RetrofitClientCodegen.java new file mode 100644 index 00000000000..6f80f8a5c5e --- /dev/null +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/RetrofitClientCodegen.java @@ -0,0 +1,190 @@ +package com.wordnik.swagger.codegen.languages; + +import com.wordnik.swagger.codegen.*; +import com.wordnik.swagger.models.Operation; +import com.wordnik.swagger.models.properties.*; + +import java.util.*; +import java.io.File; + +public class RetrofitClientCodegen extends DefaultCodegen implements CodegenConfig { + protected String invokerPackage = "io.swagger.client"; + protected String groupId = "io.swagger"; + protected String artifactId = "swagger-java-client"; + protected String artifactVersion = "1.0.0"; + protected String sourceFolder = "src/main/java"; + + public CodegenType getTag() { + return CodegenType.CLIENT; + } + + public String getName() { + return "retrofit"; + } + + public String getHelp() { + return "Generates a Retrofit client library."; + } + + public RetrofitClientCodegen() { + super(); + outputFolder = "generated-code/java"; + modelTemplateFiles.put("model.mustache", ".java"); + apiTemplateFiles.put("api.mustache", ".java"); + templateDir = "retrofit"; + apiPackage = "io.swagger.client.api"; + modelPackage = "io.swagger.client.model"; + + reservedWords = new HashSet ( + Arrays.asList( + "abstract", "continue", "for", "new", "switch", "assert", + "default", "if", "package", "synchronized", "boolean", "do", "goto", "private", + "this", "break", "double", "implements", "protected", "throw", "byte", "else", + "import", "public", "throws", "case", "enum", "instanceof", "return", "transient", + "catch", "extends", "int", "short", "try", "char", "final", "interface", "static", + "void", "class", "finally", "long", "strictfp", "volatile", "const", "float", + "native", "super", "while") + ); + + additionalProperties.put("invokerPackage", invokerPackage); + additionalProperties.put("groupId", groupId); + additionalProperties.put("artifactId", artifactId); + additionalProperties.put("artifactVersion", artifactVersion); + + supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); + supportingFiles.add(new SupportingFile("service.mustache", + (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ServiceGenerator.java")); + + languageSpecificPrimitives = new HashSet( + Arrays.asList( + "String", + "boolean", + "Boolean", + "Double", + "Integer", + "Long", + "Float", + "Object") + ); + instantiationTypes.put("array", "ArrayList"); + instantiationTypes.put("map", "HashMap"); + } + + @Override + public String escapeReservedWord(String name) { + return "_" + name; + } + + @Override + public String apiFileFolder() { + return outputFolder + "/" + sourceFolder + "/" + apiPackage().replace('.', File.separatorChar); + } + + public String modelFileFolder() { + return outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', File.separatorChar); + } + + @Override + public String toVarName(String name) { + // replace - with _ e.g. created-at => created_at + name = name.replaceAll("-", "_"); + + // if it's all uppper case, do nothing + if (name.matches("^[A-Z_]*$")) + return name; + + // camelize (lower first character) the variable name + // pet_id => petId + name = camelize(name, true); + + // for reserved word or word starting with number, append _ + if(reservedWords.contains(name) || name.matches("^\\d.*")) + name = escapeReservedWord(name); + + return name; + } + + @Override + public String toParamName(String name) { + // should be the same as variable name + return toVarName(name); + } + + @Override + public String toModelName(String name) { + // model name cannot use reserved keyword, e.g. return + if(reservedWords.contains(name)) + throw new RuntimeException(name + " (reserved word) cannot be used as a model name"); + + // camelize the model name + // phone_number => PhoneNumber + return camelize(name); + } + + @Override + public String toModelFilename(String name) { + // should be the same as the model name + return toModelName(name); + } + + + @Override + public String getTypeDeclaration(Property p) { + if(p instanceof ArrayProperty) { + ArrayProperty ap = (ArrayProperty) p; + Property inner = ap.getItems(); + return getSwaggerType(p) + "<" + getTypeDeclaration(inner) + ">"; + } + else if (p instanceof MapProperty) { + MapProperty mp = (MapProperty) p; + Property inner = mp.getAdditionalProperties(); + + return getSwaggerType(p) + ""; + } + return super.getTypeDeclaration(p); + } + + @Override + public String getSwaggerType(Property p) { + String swaggerType = super.getSwaggerType(p); + String type = null; + if(typeMapping.containsKey(swaggerType)) { + type = typeMapping.get(swaggerType); + if(languageSpecificPrimitives.contains(type)) + return toModelName(type); + } + else + type = swaggerType; + return toModelName(type); + } + + @Override + public String toOperationId(String operationId) { + // method name cannot use reserved keyword, e.g. return + if(reservedWords.contains(operationId)) + throw new RuntimeException(operationId + " (reserved word) cannot be used as method name"); + + return camelize(operationId, true); + } + + public Map postProcessOperations(Map objs) { + Map operations = (Map)objs.get("operations"); + if(operations != null) { + List ops = (List) operations.get("operation"); + for(CodegenOperation operation : ops) { + if (operation.hasConsumes == Boolean.TRUE) { + Map firstType = operation.consumes.get(0); + if (firstType != null) { + if ("multipart/form-data".equals(firstType.get("mediaType"))) { + operation.isMultipart = Boolean.TRUE; + } + } + } + if(operation.returnType == null) { + operation.returnType = "Void"; + } + } + } + return objs; + } +} diff --git a/modules/swagger-codegen/src/main/resources/META-INF/services/com.wordnik.swagger.codegen.CodegenConfig b/modules/swagger-codegen/src/main/resources/META-INF/services/com.wordnik.swagger.codegen.CodegenConfig index 73dbeae62ef..4dec8a51df5 100644 --- a/modules/swagger-codegen/src/main/resources/META-INF/services/com.wordnik.swagger.codegen.CodegenConfig +++ b/modules/swagger-codegen/src/main/resources/META-INF/services/com.wordnik.swagger.codegen.CodegenConfig @@ -10,6 +10,7 @@ com.wordnik.swagger.codegen.languages.PhpClientCodegen com.wordnik.swagger.codegen.languages.PythonClientCodegen com.wordnik.swagger.codegen.languages.Python3ClientCodegen com.wordnik.swagger.codegen.languages.Qt5CPPGenerator +com.wordnik.swagger.codegen.languages.RetrofitClientCodegen com.wordnik.swagger.codegen.languages.RubyClientCodegen com.wordnik.swagger.codegen.languages.ScalaClientCodegen com.wordnik.swagger.codegen.languages.ScalatraServerCodegen diff --git a/modules/swagger-codegen/src/main/resources/retrofit/api.mustache b/modules/swagger-codegen/src/main/resources/retrofit/api.mustache new file mode 100644 index 00000000000..a916f363ff5 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/retrofit/api.mustache @@ -0,0 +1,29 @@ +package {{package}}; + +import {{modelPackage}}.*; + +import retrofit.http.*; +import retrofit.mime.*; +import java.util.*; + +{{#imports}}import {{import}}; +{{/imports}} + +{{#operations}} +public interface {{classname}} { + {{#operation}} + /** + * {{summary}} + * {{notes}} +{{#allParams}} * @param {{paramName}} {{description}} +{{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} + */ + {{#formParams}}{{#-first}} + {{#isMultipart}}@Multipart{{/isMultipart}}{{^isMultipart}}@FormUrlEncoded{{/isMultipart}}{{/-first}}{{/formParams}} + @{{httpMethod}}("{{path}}") + {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}} {{nickname}}({{^allParams}});{{/allParams}} + {{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{^hasMore}} + );{{/hasMore}}{{/allParams}} + {{/operation}} +} +{{/operations}} diff --git a/modules/swagger-codegen/src/main/resources/retrofit/bodyParams.mustache b/modules/swagger-codegen/src/main/resources/retrofit/bodyParams.mustache new file mode 100644 index 00000000000..f8788583db5 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/retrofit/bodyParams.mustache @@ -0,0 +1 @@ +{{#isBodyParam}}@Body {{{dataType}}} {{paramName}}{{/isBodyParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/retrofit/formParams.mustache b/modules/swagger-codegen/src/main/resources/retrofit/formParams.mustache new file mode 100644 index 00000000000..9853bc0bf11 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/retrofit/formParams.mustache @@ -0,0 +1 @@ +{{#isFormParam}}{{#notFile}}{{#isMultipart}}@Part{{/isMultipart}}{{^isMultipart}}@Field{{/isMultipart}}("{{paramName}}") {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}{{#isMultipart}}@Part{{/isMultipart}}{{^isMultipart}}@Field{{/isMultipart}}("{{paramName}}") TypedFile {{paramName}}{{/isFile}}{{/isFormParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/retrofit/headerParams.mustache b/modules/swagger-codegen/src/main/resources/retrofit/headerParams.mustache new file mode 100644 index 00000000000..29206e1546b --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/retrofit/headerParams.mustache @@ -0,0 +1 @@ +{{#isHeaderParam}}@Header("{{baseName}}") {{{dataType}}} {{paramName}}{{/isHeaderParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/retrofit/model.mustache b/modules/swagger-codegen/src/main/resources/retrofit/model.mustache new file mode 100644 index 00000000000..f4446d5a1ef --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/retrofit/model.mustache @@ -0,0 +1,50 @@ +package {{package}}; + +{{#imports}}import {{import}}; +{{/imports}} + +import com.wordnik.swagger.annotations.*; +import com.google.gson.annotations.SerializedName; +{{#models}} + +{{#model}}{{#description}} +/** + * {{description}} + **/{{/description}} +@ApiModel(description = "{{{description}}}") +public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} { + {{#vars}}{{#isEnum}} + public enum {{datatypeWithEnum}} { + {{#allowableValues}}{{#values}} {{.}}, {{/values}}{{/allowableValues}} + };{{/isEnum}} + + /**{{#description}} + * {{{description}}}{{/description}}{{#minimum}} + * minimum: {{minimum}}{{/minimum}}{{#maximum}} + * maximum: {{maximum}}{{/maximum}} + **/ + @ApiModelProperty({{#required}}required = {{required}}, {{/required}}value = "{{{description}}}") + @SerializedName("{{baseName}}"){{#isEnum}} + private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};{{/isEnum}}{{^isEnum}} + private {{{datatype}}} {{name}} = {{{defaultValue}}};{{/isEnum}}{{/vars}} + + {{#vars}} + public {{{datatypeWithEnum}}} {{getter}}() { + return {{name}}; + } + public void {{setter}}({{{datatypeWithEnum}}} {{name}}) { + this.{{name}} = {{name}}; + } + {{/vars}} + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class {{classname}} {\n"); + {{#parent}}sb.append(" " + super.toString()).append("\n");{{/parent}} + {{#vars}}sb.append(" {{name}}: ").append({{name}}).append("\n"); + {{/vars}}sb.append("}\n"); + return sb.toString(); + } +} +{{/model}} +{{/models}} diff --git a/modules/swagger-codegen/src/main/resources/retrofit/pathParams.mustache b/modules/swagger-codegen/src/main/resources/retrofit/pathParams.mustache new file mode 100644 index 00000000000..8a8bdc74c88 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/retrofit/pathParams.mustache @@ -0,0 +1 @@ +{{#isPathParam}}@Path("{{baseName}}") {{{dataType}}} {{paramName}}{{/isPathParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/retrofit/pom.mustache b/modules/swagger-codegen/src/main/resources/retrofit/pom.mustache new file mode 100644 index 00000000000..fd631871054 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/retrofit/pom.mustache @@ -0,0 +1,143 @@ + + 4.0.0 + {{groupId}} + {{artifactId}} + jar + {{artifactId}} + {{artifactVersion}} + + scm:git:git@github.com:wordnik/swagger-mustache.git + scm:git:git@github.com:wordnik/swagger-codegen.git + https://github.com/wordnik/swagger-codegen + + + 2.2.0 + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12 + + + + loggerPath + conf/log4j.properties + + + -Xms512m -Xmx1500m + methods + pertest + + + + maven-dependency-plugin + + + package + + copy-dependencies + + + ${project.build.directory}/lib + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.2 + + + + jar + test-jar + + + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + add_sources + generate-sources + + add-source + + + + src/main/java + + + + + add_test_sources + generate-test-sources + + add-test-source + + + + src/test/java + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + + + + com.wordnik + swagger-annotations + ${swagger-annotations-version} + + + com.google.code.gson + gson + ${gson-version} + compile + + + com.squareup.retrofit + retrofit + ${retrofit-version} + compile + + + + + junit + junit + ${junit-version} + test + + + + 1.5.3-M1 + 2.3.1 + 1.9.0 + 1.0.0 + 4.12 + + diff --git a/modules/swagger-codegen/src/main/resources/retrofit/queryParams.mustache b/modules/swagger-codegen/src/main/resources/retrofit/queryParams.mustache new file mode 100644 index 00000000000..e0a58533e5f --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/retrofit/queryParams.mustache @@ -0,0 +1 @@ +{{#isQueryParam}}@Query("{{baseName}}") {{{dataType}}} {{paramName}}{{/isQueryParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/retrofit/service.mustache b/modules/swagger-codegen/src/main/resources/retrofit/service.mustache new file mode 100644 index 00000000000..218a9304071 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/retrofit/service.mustache @@ -0,0 +1,23 @@ +package {{invokerPackage}}; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import retrofit.RestAdapter; +import retrofit.converter.GsonConverter; + +public class ServiceGenerator { + // No need to instantiate this class. + private ServiceGenerator() { } + + public static S createService(Class serviceClass) { + Gson gson = new GsonBuilder() + .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ") + .create(); + RestAdapter adapter = new RestAdapter.Builder() + .setEndpoint("{{basePath}}") + .setConverter(new GsonConverter(gson)) + .build(); + + return adapter.create(serviceClass); + } +} diff --git a/pom.xml b/pom.xml index 6fbb36464f8..c8d223cfb2c 100644 --- a/pom.xml +++ b/pom.xml @@ -350,6 +350,18 @@ samples/client/petstore/ruby + + retrofit-client + + + env + java + + + + samples/client/petstore/retrofit + + spring-mvc diff --git a/samples/client/petstore/android-java/src/main/java/AndroidManifest.xml b/samples/client/petstore/android-java/src/main/java/AndroidManifest.xml new file mode 100644 index 00000000000..3042699fd86 --- /dev/null +++ b/samples/client/petstore/android-java/src/main/java/AndroidManifest.xml @@ -0,0 +1,3 @@ + + + diff --git a/samples/client/petstore/retrofit/pom.xml b/samples/client/petstore/retrofit/pom.xml new file mode 100644 index 00000000000..4a95855cac9 --- /dev/null +++ b/samples/client/petstore/retrofit/pom.xml @@ -0,0 +1,143 @@ + + 4.0.0 + io.swagger + swagger-java-client + jar + swagger-java-client + 1.0.0 + + scm:git:git@github.com:wordnik/swagger-mustache.git + scm:git:git@github.com:wordnik/swagger-codegen.git + https://github.com/wordnik/swagger-codegen + + + 2.2.0 + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12 + + + + loggerPath + conf/log4j.properties + + + -Xms512m -Xmx1500m + methods + pertest + + + + maven-dependency-plugin + + + package + + copy-dependencies + + + ${project.build.directory}/lib + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.2 + + + + jar + test-jar + + + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + add_sources + generate-sources + + add-source + + + + src/main/java + + + + + add_test_sources + generate-test-sources + + add-test-source + + + + src/test/java + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + + + + com.wordnik + swagger-annotations + ${swagger-annotations-version} + + + com.google.code.gson + gson + ${gson-version} + compile + + + com.squareup.retrofit + retrofit + ${retrofit-version} + compile + + + + + junit + junit + ${junit-version} + test + + + + 1.5.3-M1 + 2.3.1 + 1.9.0 + 1.0.0 + 4.12 + + diff --git a/samples/client/petstore/retrofit/src/main/java/io/swagger/client/ServiceGenerator.java b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/ServiceGenerator.java new file mode 100644 index 00000000000..e443c984fd0 --- /dev/null +++ b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/ServiceGenerator.java @@ -0,0 +1,23 @@ +package io.swagger.client; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import retrofit.RestAdapter; +import retrofit.converter.GsonConverter; + +public class ServiceGenerator { + // No need to instantiate this class. + private ServiceGenerator() { } + + public static S createService(Class serviceClass) { + Gson gson = new GsonBuilder() + .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ") + .create(); + RestAdapter adapter = new RestAdapter.Builder() + .setEndpoint("http://petstore.swagger.io/v2") + .setConverter(new GsonConverter(gson)) + .build(); + + return adapter.create(serviceClass); + } +} diff --git a/samples/client/petstore/retrofit/src/main/java/io/swagger/client/api/PetApi.java b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/api/PetApi.java new file mode 100644 index 00000000000..3d1b0893b61 --- /dev/null +++ b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/api/PetApi.java @@ -0,0 +1,117 @@ +package io.swagger.client.api; + +import io.swagger.client.model.*; + +import retrofit.http.*; +import retrofit.mime.*; +import java.util.*; + +import io.swagger.client.model.Pet; +import java.io.File; + +public interface PetApi { + + /** + * Update an existing pet + * + * @param body Pet object that needs to be added to the store + * @return Void + */ + + @PUT("/pet") + Void updatePet( + @Body Pet body + ); + + /** + * Add a new pet to the store + * + * @param body Pet object that needs to be added to the store + * @return Void + */ + + @POST("/pet") + Void addPet( + @Body Pet body + ); + + /** + * Finds Pets by status + * Multiple status values can be provided with comma seperated strings + * @param status Status values that need to be considered for filter + * @return List + */ + + @GET("/pet/findByStatus") + List findPetsByStatus( + @Query("status") List status + ); + + /** + * Finds Pets by tags + * Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing. + * @param tags Tags to filter by + * @return List + */ + + @GET("/pet/findByTags") + List findPetsByTags( + @Query("tags") List tags + ); + + /** + * Find pet by ID + * Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions + * @param petId ID of pet that needs to be fetched + * @return Pet + */ + + @GET("/pet/{petId}") + Pet getPetById( + @Path("petId") Long petId + ); + + /** + * Updates a pet in the store with form data + * + * @param petId ID of pet that needs to be updated + * @param name Updated name of the pet + * @param status Updated status of the pet + * @return Void + */ + + @FormUrlEncoded + @POST("/pet/{petId}") + Void updatePetWithForm( + @Path("petId") String petId,@Field("name") String name,@Field("status") String status + ); + + /** + * Deletes a pet + * + * @param apiKey + * @param petId Pet id to delete + * @return Void + */ + + @DELETE("/pet/{petId}") + Void deletePet( + @Header("api_key") String apiKey,@Path("petId") Long petId + ); + + /** + * uploads an image + * + * @param petId ID of pet to update + * @param additionalMetadata Additional data to pass to server + * @param file file to upload + * @return Void + */ + + @Multipart + @POST("/pet/{petId}/uploadImage") + Void uploadFile( + @Path("petId") Long petId,@Part("additionalMetadata") String additionalMetadata,@Part("file") TypedFile file + ); + +} diff --git a/samples/client/petstore/retrofit/src/main/java/io/swagger/client/api/StoreApi.java b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/api/StoreApi.java new file mode 100644 index 00000000000..fcd47b4b735 --- /dev/null +++ b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/api/StoreApi.java @@ -0,0 +1,60 @@ +package io.swagger.client.api; + +import io.swagger.client.model.*; + +import retrofit.http.*; +import retrofit.mime.*; +import java.util.*; + +import java.util.Map; +import io.swagger.client.model.Order; + +public interface StoreApi { + + /** + * Returns pet inventories by status + * Returns a map of status codes to quantities + * @return Map + */ + + @GET("/store/inventory") + Map getInventory(); + + + /** + * Place an order for a pet + * + * @param body order placed for purchasing the pet + * @return Order + */ + + @POST("/store/order") + Order placeOrder( + @Body Order body + ); + + /** + * Find purchase order by ID + * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions + * @param orderId ID of pet that needs to be fetched + * @return Order + */ + + @GET("/store/order/{orderId}") + Order getOrderById( + @Path("orderId") String orderId + ); + + /** + * Delete purchase order by ID + * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors + * @param orderId ID of the order that needs to be deleted + * @return Void + */ + + @DELETE("/store/order/{orderId}") + Void deleteOrder( + @Path("orderId") String orderId + ); + +} diff --git a/samples/client/petstore/retrofit/src/main/java/io/swagger/client/api/UserApi.java b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/api/UserApi.java new file mode 100644 index 00000000000..4aa39c52e2c --- /dev/null +++ b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/api/UserApi.java @@ -0,0 +1,110 @@ +package io.swagger.client.api; + +import io.swagger.client.model.*; + +import retrofit.http.*; +import retrofit.mime.*; +import java.util.*; + +import io.swagger.client.model.User; +import java.util.*; + +public interface UserApi { + + /** + * Create user + * This can only be done by the logged in user. + * @param body Created user object + * @return Void + */ + + @POST("/user") + Void createUser( + @Body User body + ); + + /** + * Creates list of users with given input array + * + * @param body List of user object + * @return Void + */ + + @POST("/user/createWithArray") + Void createUsersWithArrayInput( + @Body List body + ); + + /** + * Creates list of users with given input array + * + * @param body List of user object + * @return Void + */ + + @POST("/user/createWithList") + Void createUsersWithListInput( + @Body List body + ); + + /** + * Logs user into the system + * + * @param username The user name for login + * @param password The password for login in clear text + * @return String + */ + + @GET("/user/login") + String loginUser( + @Query("username") String username,@Query("password") String password + ); + + /** + * Logs out current logged in user session + * + * @return Void + */ + + @GET("/user/logout") + Void logoutUser(); + + + /** + * Get user by user name + * + * @param username The name that needs to be fetched. Use user1 for testing. + * @return User + */ + + @GET("/user/{username}") + User getUserByName( + @Path("username") String username + ); + + /** + * Updated user + * This can only be done by the logged in user. + * @param username name that need to be deleted + * @param body Updated user object + * @return Void + */ + + @PUT("/user/{username}") + Void updateUser( + @Path("username") String username,@Body User body + ); + + /** + * Delete user + * This can only be done by the logged in user. + * @param username The name that needs to be deleted + * @return Void + */ + + @DELETE("/user/{username}") + Void deleteUser( + @Path("username") String username + ); + +} diff --git a/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Category.java b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Category.java new file mode 100644 index 00000000000..8ba10d629dc --- /dev/null +++ b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Category.java @@ -0,0 +1,49 @@ +package io.swagger.client.model; + + +import com.wordnik.swagger.annotations.*; +import com.google.gson.annotations.SerializedName; + + +@ApiModel(description = "") +public class Category { + + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("id") + private Long id = null; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("name") + private String name = null; + + + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class Category {\n"); + + sb.append(" id: ").append(id).append("\n"); + sb.append(" name: ").append(name).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} diff --git a/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Order.java b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Order.java new file mode 100644 index 00000000000..d7d0b2d9282 --- /dev/null +++ b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Order.java @@ -0,0 +1,110 @@ +package io.swagger.client.model; + +import java.util.Date; + +import com.wordnik.swagger.annotations.*; +import com.google.gson.annotations.SerializedName; + + +@ApiModel(description = "") +public class Order { + + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("id") + private Long id = null; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("petId") + private Long petId = null; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("quantity") + private Integer quantity = null; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("shipDate") + private Date shipDate = null; + public enum StatusEnum { + placed, approved, delivered, + }; + + /** + * Order Status + **/ + @ApiModelProperty(value = "Order Status") + @SerializedName("status") + private StatusEnum status = null; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("complete") + private Boolean complete = null; + + + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + + public Long getPetId() { + return petId; + } + public void setPetId(Long petId) { + this.petId = petId; + } + + public Integer getQuantity() { + return quantity; + } + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } + + public Date getShipDate() { + return shipDate; + } + public void setShipDate(Date shipDate) { + this.shipDate = shipDate; + } + + public StatusEnum getStatus() { + return status; + } + public void setStatus(StatusEnum status) { + this.status = status; + } + + public Boolean getComplete() { + return complete; + } + public void setComplete(Boolean complete) { + this.complete = complete; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class Order {\n"); + + sb.append(" id: ").append(id).append("\n"); + sb.append(" petId: ").append(petId).append("\n"); + sb.append(" quantity: ").append(quantity).append("\n"); + sb.append(" shipDate: ").append(shipDate).append("\n"); + sb.append(" status: ").append(status).append("\n"); + sb.append(" complete: ").append(complete).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} diff --git a/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Pet.java b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Pet.java new file mode 100644 index 00000000000..66a02367c90 --- /dev/null +++ b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Pet.java @@ -0,0 +1,112 @@ +package io.swagger.client.model; + +import io.swagger.client.model.Category; +import java.util.*; +import io.swagger.client.model.Tag; + +import com.wordnik.swagger.annotations.*; +import com.google.gson.annotations.SerializedName; + + +@ApiModel(description = "") +public class Pet { + + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("id") + private Long id = null; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("category") + private Category category = null; + + /** + **/ + @ApiModelProperty(required = true, value = "") + @SerializedName("name") + private String name = null; + + /** + **/ + @ApiModelProperty(required = true, value = "") + @SerializedName("photoUrls") + private List photoUrls = new ArrayList() ; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("tags") + private List tags = new ArrayList() ; + public enum StatusEnum { + available, pending, sold, + }; + + /** + * pet status in the store + **/ + @ApiModelProperty(value = "pet status in the store") + @SerializedName("status") + private StatusEnum status = null; + + + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + + public Category getCategory() { + return category; + } + public void setCategory(Category category) { + this.category = category; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + public List getPhotoUrls() { + return photoUrls; + } + public void setPhotoUrls(List photoUrls) { + this.photoUrls = photoUrls; + } + + public List getTags() { + return tags; + } + public void setTags(List tags) { + this.tags = tags; + } + + public StatusEnum getStatus() { + return status; + } + public void setStatus(StatusEnum status) { + this.status = status; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class Pet {\n"); + + sb.append(" id: ").append(id).append("\n"); + sb.append(" category: ").append(category).append("\n"); + sb.append(" name: ").append(name).append("\n"); + sb.append(" photoUrls: ").append(photoUrls).append("\n"); + sb.append(" tags: ").append(tags).append("\n"); + sb.append(" status: ").append(status).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} diff --git a/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Tag.java b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Tag.java new file mode 100644 index 00000000000..e6ddd4c92cd --- /dev/null +++ b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/Tag.java @@ -0,0 +1,49 @@ +package io.swagger.client.model; + + +import com.wordnik.swagger.annotations.*; +import com.google.gson.annotations.SerializedName; + + +@ApiModel(description = "") +public class Tag { + + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("id") + private Long id = null; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("name") + private String name = null; + + + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class Tag {\n"); + + sb.append(" id: ").append(id).append("\n"); + sb.append(" name: ").append(name).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} diff --git a/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/User.java b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/User.java new file mode 100644 index 00000000000..f42f77b9773 --- /dev/null +++ b/samples/client/petstore/retrofit/src/main/java/io/swagger/client/model/User.java @@ -0,0 +1,134 @@ +package io.swagger.client.model; + + +import com.wordnik.swagger.annotations.*; +import com.google.gson.annotations.SerializedName; + + +@ApiModel(description = "") +public class User { + + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("id") + private Long id = null; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("username") + private String username = null; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("firstName") + private String firstName = null; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("lastName") + private String lastName = null; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("email") + private String email = null; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("password") + private String password = null; + + /** + **/ + @ApiModelProperty(value = "") + @SerializedName("phone") + private String phone = null; + + /** + * User Status + **/ + @ApiModelProperty(value = "User Status") + @SerializedName("userStatus") + private Integer userStatus = null; + + + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + + public String getUsername() { + return username; + } + public void setUsername(String username) { + this.username = username; + } + + public String getFirstName() { + return firstName; + } + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getEmail() { + return email; + } + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } + + public String getPhone() { + return phone; + } + public void setPhone(String phone) { + this.phone = phone; + } + + public Integer getUserStatus() { + return userStatus; + } + public void setUserStatus(Integer userStatus) { + this.userStatus = userStatus; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class User {\n"); + + sb.append(" id: ").append(id).append("\n"); + sb.append(" username: ").append(username).append("\n"); + sb.append(" firstName: ").append(firstName).append("\n"); + sb.append(" lastName: ").append(lastName).append("\n"); + sb.append(" email: ").append(email).append("\n"); + sb.append(" password: ").append(password).append("\n"); + sb.append(" phone: ").append(phone).append("\n"); + sb.append(" userStatus: ").append(userStatus).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} diff --git a/samples/client/petstore/retrofit/src/test/java/io/swagger/petstore/test/PetApiTest.java b/samples/client/petstore/retrofit/src/test/java/io/swagger/petstore/test/PetApiTest.java new file mode 100644 index 00000000000..c588a04adcc --- /dev/null +++ b/samples/client/petstore/retrofit/src/test/java/io/swagger/petstore/test/PetApiTest.java @@ -0,0 +1,158 @@ +package io.swagger.petstore.test; + +import io.swagger.client.ServiceGenerator; +import io.swagger.client.api.*; +import io.swagger.client.model.*; + +import retrofit.RetrofitError; +import retrofit.mime.TypedFile; + +import java.util.*; +import java.io.*; + +import static org.junit.Assert.*; +import org.junit.*; + +public class PetApiTest { + PetApi api = null; + + @Before + public void setup() { + api = ServiceGenerator.createService(PetApi.class); + } + + @Test + public void testCreateAndGetPet() throws Exception { + Pet pet = createRandomPet(); + api.addPet(pet); + + Pet fetched = api.getPetById(pet.getId()); + assertNotNull(fetched); + assertEquals(pet.getId(), fetched.getId()); + assertNotNull(fetched.getCategory()); + assertEquals(fetched.getCategory().getName(), pet.getCategory().getName()); + } + + @Test + public void testUpdatePet() throws Exception { + Pet pet = createRandomPet(); + pet.setName("programmer"); + + api.updatePet(pet); + + Pet fetched = api.getPetById(pet.getId()); + assertNotNull(fetched); + assertEquals(pet.getId(), fetched.getId()); + assertNotNull(fetched.getCategory()); + assertEquals(fetched.getCategory().getName(), pet.getCategory().getName()); + } + + @Test + public void testFindPetsByStatus() throws Exception { + Pet pet = createRandomPet(); + pet.setName("programmer"); + pet.setStatus(Pet.StatusEnum.available); + + api.updatePet(pet); + + List pets = api.findPetsByStatus(Arrays.asList(new String[]{"available"})); + assertNotNull(pets); + + boolean found = false; + for(Pet fetched : pets) { + if(fetched.getId().equals(pet.getId())) { + found = true; + break; + } + } + + assertTrue(found); + } + + @Test + public void testFindPetsByTags() throws Exception { + Pet pet = createRandomPet(); + pet.setName("monster"); + pet.setStatus(Pet.StatusEnum.available); + + List tags = new ArrayList(); + Tag tag1 = new Tag(); + tag1.setName("friendly"); + tags.add(tag1); + pet.setTags(tags); + + api.updatePet(pet); + + List pets = api.findPetsByTags(Arrays.asList(new String[]{"friendly"})); + assertNotNull(pets); + + boolean found = false; + for(Pet fetched : pets) { + if(fetched.getId().equals(pet.getId())) { + found = true; + break; + } + } + assertTrue(found); + } + + @Test + public void testUpdatePetWithForm() throws Exception { + Pet pet = createRandomPet(); + pet.setName("frank"); + api.addPet(pet); + + Pet fetched = api.getPetById(pet.getId()); + + api.updatePetWithForm(String.valueOf(fetched.getId()), "furt", null); + Pet updated = api.getPetById(fetched.getId()); + + assertEquals(updated.getName(), fetched.getName()); + } + + @Test + public void testDeletePet() throws Exception { + Pet pet = createRandomPet(); + api.addPet(pet); + + Pet fetched = api.getPetById(pet.getId()); + api.deletePet(null, fetched.getId()); + + try { + fetched = api.getPetById(fetched.getId()); + fail("expected an error"); + } + catch (RetrofitError e) { + assertEquals(404, e.getResponse().getStatus()); + } + } + + @Test + public void testUploadFile() throws Exception { + Pet pet = createRandomPet(); + api.addPet(pet); + + File file = new File("hello.txt"); + BufferedWriter writer = new BufferedWriter(new FileWriter(file)); + writer.write("Hello world!"); + writer.close(); + + api.uploadFile(pet.getId(), "a test file", new TypedFile("text/plain", file)); + } + + private Pet createRandomPet() { + Pet pet = new Pet(); + pet.setId(System.currentTimeMillis()); + pet.setName("gorilla"); + + Category category = new Category(); + category.setName("really-happy"); + + pet.setCategory(category); + pet.setStatus(Pet.StatusEnum.available); + List photos = Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}); + pet.setPhotoUrls(photos); + + return pet; + } +} \ No newline at end of file diff --git a/samples/client/petstore/retrofit/src/test/java/io/swagger/petstore/test/StoreApiTest.java b/samples/client/petstore/retrofit/src/test/java/io/swagger/petstore/test/StoreApiTest.java new file mode 100644 index 00000000000..733b597fefc --- /dev/null +++ b/samples/client/petstore/retrofit/src/test/java/io/swagger/petstore/test/StoreApiTest.java @@ -0,0 +1,70 @@ +package io.swagger.petstore.test; + +import io.swagger.client.ServiceGenerator; +import io.swagger.client.api.*; +import io.swagger.client.model.*; + +import retrofit.RetrofitError; + +import java.util.*; +import java.io.*; + +import static org.junit.Assert.*; +import org.junit.*; + +public class StoreApiTest { + StoreApi api = null; + + @Before + public void setup() { + api = ServiceGenerator.createService(StoreApi.class); + } + + @Test + public void testGetInventory() throws Exception { + Map inventory = api.getInventory(); + assertTrue(inventory.keySet().size() > 0); + } + + @Test + public void testPlaceOrder() throws Exception { + Order order = createOrder(); + api.placeOrder(order); + + Order fetched = api.getOrderById(String.valueOf(order.getId())); + assertEquals(order.getId(), fetched.getId()); + assertEquals(order.getPetId(), fetched.getPetId()); + assertEquals(order.getQuantity(), fetched.getQuantity()); + } + + @Test + public void testDeleteOrder() throws Exception { + Order order = createOrder(); + api.placeOrder(order); + + Order fetched = api.getOrderById(String.valueOf(order.getId())); + assertEquals(fetched.getId(), order.getId()); + + api.deleteOrder(String.valueOf(order.getId())); + + try { + api.getOrderById(String.valueOf(order.getId())); + // fail("expected an error"); + } + catch (RetrofitError e) { + // ok + } + } + + private Order createOrder() { + Order order = new Order(); + order.setId(new Long(System.currentTimeMillis())); + order.setPetId(new Long(200)); + order.setQuantity(new Integer(13)); + order.setShipDate(new java.util.Date()); + order.setStatus(Order.StatusEnum.placed); + order.setComplete(true); + + return order; + } +} \ No newline at end of file diff --git a/samples/client/petstore/retrofit/src/test/java/io/swagger/petstore/test/UserApiTest.java b/samples/client/petstore/retrofit/src/test/java/io/swagger/petstore/test/UserApiTest.java new file mode 100644 index 00000000000..c7748bc492d --- /dev/null +++ b/samples/client/petstore/retrofit/src/test/java/io/swagger/petstore/test/UserApiTest.java @@ -0,0 +1,84 @@ +package io.swagger.petstore.test; + +import io.swagger.client.ServiceGenerator; +import io.swagger.client.api.*; +import io.swagger.client.model.*; + +import java.util.*; +import java.io.*; + +import static org.junit.Assert.*; +import org.junit.*; + +public class UserApiTest { + UserApi api = null; + + @Before + public void setup() { + api = ServiceGenerator.createService(UserApi.class); + } + + @Test + public void testCreateUser() throws Exception { + User user = createUser(); + + api.createUser(user); + + User fetched = api.getUserByName(user.getUsername()); + assertEquals(user.getId(), fetched.getId()); + } + + @Test + public void testCreateUsersWithArray() throws Exception { + User user1 = createUser(); + user1.setUsername("abc123"); + User user2 = createUser(); + user2.setUsername("123abc"); + + api.createUsersWithArrayInput(Arrays.asList(new User[]{user1, user2})); + + User fetched = api.getUserByName(user1.getUsername()); + assertEquals(user1.getId(), fetched.getId()); + } + + @Test + public void testCreateUsersWithList() throws Exception { + User user1 = createUser(); + user1.setUsername("abc123"); + User user2 = createUser(); + user2.setUsername("123abc"); + + api.createUsersWithListInput(Arrays.asList(new User[]{user1, user2})); + + User fetched = api.getUserByName(user1.getUsername()); + assertEquals(user1.getId(), fetched.getId()); + } + + @Test + public void testLoginUser() throws Exception { + User user = createUser(); + api.createUser(user); + + String token = api.loginUser(user.getUsername(), user.getPassword()); + assertTrue(token.startsWith("logged in user session:")); + } + + @Test + public void logoutUser() throws Exception { + api.logoutUser(); + } + + private User createUser() { + User user = new User(); + user.setId(System.currentTimeMillis()); + user.setUsername("fred"); + user.setFirstName("Fred"); + user.setLastName("Meyer"); + user.setEmail("fred@fredmeyer.com"); + user.setPassword("xxXXxx"); + user.setPhone("408-867-5309"); + user.setUserStatus(123); + + return user; + } +} \ No newline at end of file From 0996901e72ef510c591fafacdbbeb38d1b4a3bd9 Mon Sep 17 00:00:00 2001 From: Richie Azaria Date: Fri, 29 May 2015 13:01:42 -0700 Subject: [PATCH 20/21] add documentation -c option and config-help --- README.md | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cb649e07da5..da7d0752f0d 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,8 @@ NAME SYNOPSIS swagger generate [(-a | --auth )] + [(-c | --config )] + [-D ] (-i | --input-spec ) (-l | --lang ) [(-o | --output )] @@ -72,6 +74,16 @@ OPTIONS remotely. Pass in a URL-encoded string of name:header with a comma separating multiple values + -c , --config + Path to json configuration file. File content should be in a json + format {"optionKey":"optionValue", "optionKey1":"optionValue1"...} + Supported options can be different for each language. Run + config-help -l {lang} command for language specific config options. + + -D + sets specified system properties in the format of + name=value,name=value + -i , --input-spec location of the swagger spec, as URL or file (required) @@ -166,8 +178,60 @@ SwaggerYamlGenerator.java TizenClientCodegen.java ``` -Each of these files creates reasonable defaults so you can get running quickly. But if you want to configure package names, prefixes, model folders, etc., you may want to extend these. +Each of these files creates reasonable defaults so you can get running quickly. But if you want to configure package names, prefixes, model folders, etc. you can use a json config file to pass the values. +``` +java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \ + -i http://petstore.swagger.io/v2/swagger.json \ + -l java \ + -o samples/client/petstore/java \ + -c path/to/config.json +``` +Supported config options can be different per language. Running `config-help -l {lang}` will show available options. + +``` +java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jarr config-help -l java +``` + +Output + +``` +CONFIG OPTIONS + modelPackage + package for generated models + + apiPackage + package for generated api classes + + invokerPackage + root package for generated code + + groupId + groupId in generated pom.xml + + artifactId + artifactId in generated pom.xml + + artifactVersion + artifact version in generated pom.xml + + sourceFolder + source folder for generated code +``` + +Your config file for java can look like + +``` +{ + "groupId":"com.my.company", + "artifactId":"MyClent", + "artifactVersion":"1.2.0" +} +``` + +For all the unspecified options default values will be used. + +Another way to override default options is to extend config class for specific language. To change, for example, the prefix for the Objective-C generated files, simply subclass the ObjcClientCodegen.java: ``` From 3e0913a813bc0e9d6d55c32ad72e41da4986a916 Mon Sep 17 00:00:00 2001 From: hrachya Date: Mon, 1 Jun 2015 10:43:23 -0700 Subject: [PATCH 21/21] removing generic header comments --- .../java/com/wordnik/swagger/codegen/cmd/ConfigHelp.java | 5 ----- .../src/main/java/com/wordnik/swagger/codegen/CliOption.java | 5 ----- modules/swagger-codegen/src/main/java/config/Config.java | 5 ----- .../swagger-codegen/src/main/java/config/ConfigParser.java | 5 ----- 4 files changed, 20 deletions(-) diff --git a/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/ConfigHelp.java b/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/ConfigHelp.java index 6934bcf702e..fbdca12e6e8 100644 --- a/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/ConfigHelp.java +++ b/modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/ConfigHelp.java @@ -1,8 +1,3 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package com.wordnik.swagger.codegen.cmd; import com.wordnik.swagger.codegen.CliOption; diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CliOption.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CliOption.java index 4294d097116..8ea0b60db26 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CliOption.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CliOption.java @@ -1,8 +1,3 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package com.wordnik.swagger.codegen; public class CliOption { diff --git a/modules/swagger-codegen/src/main/java/config/Config.java b/modules/swagger-codegen/src/main/java/config/Config.java index 745824bd816..9bc4bf58624 100644 --- a/modules/swagger-codegen/src/main/java/config/Config.java +++ b/modules/swagger-codegen/src/main/java/config/Config.java @@ -1,8 +1,3 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package config; import com.google.common.collect.ImmutableMap; diff --git a/modules/swagger-codegen/src/main/java/config/ConfigParser.java b/modules/swagger-codegen/src/main/java/config/ConfigParser.java index 019061faa33..cbb1b78f645 100644 --- a/modules/swagger-codegen/src/main/java/config/ConfigParser.java +++ b/modules/swagger-codegen/src/main/java/config/ConfigParser.java @@ -1,8 +1,3 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package config; import com.fasterxml.jackson.databind.JsonNode;