forked from loafle/openapi-generator-original
Merge branch 'develop_2.0' into ruby-auth
This commit is contained in:
commit
9618960558
11
LICENSE
Normal file
11
LICENSE
Normal file
@ -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.
|
68
README.md
68
README.md
@ -60,6 +60,8 @@ NAME
|
||||
|
||||
SYNOPSIS
|
||||
swagger generate [(-a <authorization> | --auth <authorization>)]
|
||||
[(-c <configuration file> | --config <configuration file>)]
|
||||
[-D <system properties>]
|
||||
(-i <spec file> | --input-spec <spec file>)
|
||||
(-l <language> | --lang <language>)
|
||||
[(-o <output directory> | --output <output directory>)]
|
||||
@ -72,6 +74,16 @@ OPTIONS
|
||||
remotely. Pass in a URL-encoded string of name:header with a comma
|
||||
separating multiple values
|
||||
|
||||
-c <configuration file>, --config <configuration file>
|
||||
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 <system properties>
|
||||
sets specified system properties in the format of
|
||||
name=value,name=value
|
||||
|
||||
-i <spec file>, --input-spec <spec file>
|
||||
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:
|
||||
|
||||
```
|
||||
@ -273,7 +337,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.
|
||||
|
@ -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
|
||||
|
31
bin/retrofit-petstore.sh
Executable file
31
bin/retrofit-petstore.sh
Executable file
@ -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
|
@ -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();
|
||||
|
@ -0,0 +1,49 @@
|
||||
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<CodegenConfig> 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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,11 @@ 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. " +
|
||||
"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
|
||||
public void run() {
|
||||
@ -76,6 +84,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);
|
||||
|
||||
|
@ -0,0 +1,23 @@
|
||||
package com.wordnik.swagger.codegen;
|
||||
|
||||
public class CliOption {
|
||||
private final String opt;
|
||||
private String description;
|
||||
|
||||
public CliOption(String opt, String description) {
|
||||
this.opt = opt;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getOpt() {
|
||||
return opt;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
}
|
@ -140,4 +140,4 @@ public class Codegen extends DefaultGenerator {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -27,6 +27,7 @@ public interface CodegenConfig {
|
||||
String getTypeDeclaration(Property p);
|
||||
String getTypeDeclaration(String name);
|
||||
void processOpts();
|
||||
List<CliOption> cliOptions();
|
||||
String generateExamplePath(String path, Operation operation);
|
||||
|
||||
Set<String> reservedWords();
|
||||
|
@ -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;
|
||||
|
||||
|
@ -77,11 +77,24 @@ public class DefaultCodegen {
|
||||
protected String templateDir;
|
||||
protected Map<String, Object> additionalProperties = new HashMap<String, Object>();
|
||||
protected List<SupportingFile> supportingFiles = new ArrayList<SupportingFile>();
|
||||
protected List<CliOption> cliOptions = new ArrayList<CliOption>();
|
||||
|
||||
public List<CliOption> cliOptions() {
|
||||
return cliOptions;
|
||||
}
|
||||
|
||||
public void processOpts(){
|
||||
if(additionalProperties.containsKey("templateDir")) {
|
||||
this.setTemplateDir((String)additionalProperties.get("templateDir"));
|
||||
}
|
||||
|
||||
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
|
||||
@ -178,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);
|
||||
}
|
||||
@ -281,6 +302,9 @@ 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("modelPackage", "package for generated models"));
|
||||
cliOptions.add(new CliOption("apiPackage", "package for generated api classes"));
|
||||
}
|
||||
|
||||
|
||||
|
@ -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<String>(
|
||||
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", "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
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -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<String> foundationClasses = new HashSet<String>();
|
||||
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<String>(
|
||||
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", "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
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -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<String> (
|
||||
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<String>(
|
||||
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) + "<String, " + getTypeDeclaration(inner) + ">";
|
||||
}
|
||||
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<String, Object> postProcessOperations(Map<String, Object> objs) {
|
||||
Map<String, Object> operations = (Map<String, Object>)objs.get("operations");
|
||||
if(operations != null) {
|
||||
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
|
||||
for(CodegenOperation operation : ops) {
|
||||
if (operation.hasConsumes == Boolean.TRUE) {
|
||||
Map<String, String> 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;
|
||||
}
|
||||
}
|
33
modules/swagger-codegen/src/main/java/config/Config.java
Normal file
33
modules/swagger-codegen/src/main/java/config/Config.java
Normal file
@ -0,0 +1,33 @@
|
||||
package config;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Config {
|
||||
private Map<String, String> options;
|
||||
|
||||
public Config() {
|
||||
this.options = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
public Config(Map<String, String> properties) {
|
||||
this.options = properties;
|
||||
}
|
||||
|
||||
public Map<String, String> 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);
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
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 config from " + location);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
Config config = new Config();
|
||||
|
||||
try {
|
||||
JsonNode rootNode = mapper.readTree(new File(location));
|
||||
Iterator<Map.Entry<String, JsonNode>> optionNodes = rootNode.fields();
|
||||
|
||||
while (optionNodes.hasNext()) {
|
||||
Map.Entry<String, JsonNode> optionNode = (Map.Entry<String, JsonNode>) 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;
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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}}
|
@ -0,0 +1 @@
|
||||
{{#isBodyParam}}@Body {{{dataType}}} {{paramName}}{{/isBodyParam}}
|
@ -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}}
|
@ -0,0 +1 @@
|
||||
{{#isHeaderParam}}@Header("{{baseName}}") {{{dataType}}} {{paramName}}{{/isHeaderParam}}
|
@ -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}}
|
@ -0,0 +1 @@
|
||||
{{#isPathParam}}@Path("{{baseName}}") {{{dataType}}} {{paramName}}{{/isPathParam}}
|
143
modules/swagger-codegen/src/main/resources/retrofit/pom.mustache
Normal file
143
modules/swagger-codegen/src/main/resources/retrofit/pom.mustache
Normal file
@ -0,0 +1,143 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>{{groupId}}</groupId>
|
||||
<artifactId>{{artifactId}}</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>{{artifactId}}</name>
|
||||
<version>{{artifactVersion}}</version>
|
||||
<scm>
|
||||
<connection>scm:git:git@github.com:wordnik/swagger-mustache.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:wordnik/swagger-codegen.git</developerConnection>
|
||||
<url>https://github.com/wordnik/swagger-codegen</url>
|
||||
</scm>
|
||||
<prerequisites>
|
||||
<maven>2.2.0</maven>
|
||||
</prerequisites>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.12</version>
|
||||
<configuration>
|
||||
<systemProperties>
|
||||
<property>
|
||||
<name>loggerPath</name>
|
||||
<value>conf/log4j.properties</value>
|
||||
</property>
|
||||
</systemProperties>
|
||||
<argLine>-Xms512m -Xmx1500m</argLine>
|
||||
<parallel>methods</parallel>
|
||||
<forkMode>pertest</forkMode>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/lib</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- attach test jar -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
<goal>test-jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>build-helper-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>add_sources</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>add-source</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sources>
|
||||
<source>src/main/java</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>add_test_sources</id>
|
||||
<phase>generate-test-sources</phase>
|
||||
<goals>
|
||||
<goal>add-test-source</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sources>
|
||||
<source>src/test/java</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.wordnik</groupId>
|
||||
<artifactId>swagger-annotations</artifactId>
|
||||
<version>${swagger-annotations-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson-version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit</groupId>
|
||||
<artifactId>retrofit</artifactId>
|
||||
<version>${retrofit-version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- test dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<swagger-annotations-version>1.5.3-M1</swagger-annotations-version>
|
||||
<gson-version>2.3.1</gson-version>
|
||||
<retrofit-version>1.9.0</retrofit-version>
|
||||
<maven-plugin-version>1.0.0</maven-plugin-version>
|
||||
<junit-version>4.12</junit-version>
|
||||
</properties>
|
||||
</project>
|
@ -0,0 +1 @@
|
||||
{{#isQueryParam}}@Query("{{baseName}}") {{{dataType}}} {{paramName}}{{/isQueryParam}}
|
@ -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> S createService(Class<S> 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);
|
||||
}
|
||||
}
|
12
pom.xml
12
pom.xml
@ -350,6 +350,18 @@
|
||||
<module>samples/client/petstore/ruby</module>
|
||||
</modules>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>retrofit-client</id>
|
||||
<activation>
|
||||
<property>
|
||||
<name>env</name>
|
||||
<value>java</value>
|
||||
</property>
|
||||
</activation>
|
||||
<modules>
|
||||
<module>samples/client/petstore/retrofit</module>
|
||||
</modules>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>spring-mvc</id>
|
||||
<activation>
|
||||
|
@ -0,0 +1,3 @@
|
||||
<manifest package="io.swagger.client">
|
||||
<application />
|
||||
</manifest>
|
143
samples/client/petstore/retrofit/pom.xml
Normal file
143
samples/client/petstore/retrofit/pom.xml
Normal file
@ -0,0 +1,143 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-java-client</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>swagger-java-client</name>
|
||||
<version>1.0.0</version>
|
||||
<scm>
|
||||
<connection>scm:git:git@github.com:wordnik/swagger-mustache.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:wordnik/swagger-codegen.git</developerConnection>
|
||||
<url>https://github.com/wordnik/swagger-codegen</url>
|
||||
</scm>
|
||||
<prerequisites>
|
||||
<maven>2.2.0</maven>
|
||||
</prerequisites>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.12</version>
|
||||
<configuration>
|
||||
<systemProperties>
|
||||
<property>
|
||||
<name>loggerPath</name>
|
||||
<value>conf/log4j.properties</value>
|
||||
</property>
|
||||
</systemProperties>
|
||||
<argLine>-Xms512m -Xmx1500m</argLine>
|
||||
<parallel>methods</parallel>
|
||||
<forkMode>pertest</forkMode>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/lib</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- attach test jar -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
<goal>test-jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>build-helper-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>add_sources</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>add-source</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sources>
|
||||
<source>src/main/java</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>add_test_sources</id>
|
||||
<phase>generate-test-sources</phase>
|
||||
<goals>
|
||||
<goal>add-test-source</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sources>
|
||||
<source>src/test/java</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.wordnik</groupId>
|
||||
<artifactId>swagger-annotations</artifactId>
|
||||
<version>${swagger-annotations-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson-version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit</groupId>
|
||||
<artifactId>retrofit</artifactId>
|
||||
<version>${retrofit-version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- test dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<swagger-annotations-version>1.5.3-M1</swagger-annotations-version>
|
||||
<gson-version>2.3.1</gson-version>
|
||||
<retrofit-version>1.9.0</retrofit-version>
|
||||
<maven-plugin-version>1.0.0</maven-plugin-version>
|
||||
<junit-version>4.12</junit-version>
|
||||
</properties>
|
||||
</project>
|
@ -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> S createService(Class<S> 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);
|
||||
}
|
||||
}
|
@ -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<Pet>
|
||||
*/
|
||||
|
||||
@GET("/pet/findByStatus")
|
||||
List<Pet> findPetsByStatus(
|
||||
@Query("status") List<String> 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<Pet>
|
||||
*/
|
||||
|
||||
@GET("/pet/findByTags")
|
||||
List<Pet> findPetsByTags(
|
||||
@Query("tags") List<String> 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
|
||||
);
|
||||
|
||||
}
|
@ -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<String, Integer>
|
||||
*/
|
||||
|
||||
@GET("/store/inventory")
|
||||
Map<String, Integer> 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
|
||||
);
|
||||
|
||||
}
|
@ -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<User> body
|
||||
);
|
||||
|
||||
/**
|
||||
* Creates list of users with given input array
|
||||
*
|
||||
* @param body List of user object
|
||||
* @return Void
|
||||
*/
|
||||
|
||||
@POST("/user/createWithList")
|
||||
Void createUsersWithListInput(
|
||||
@Body List<User> 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
|
||||
);
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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<String> photoUrls = new ArrayList<String>() ;
|
||||
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@SerializedName("tags")
|
||||
private List<Tag> tags = new ArrayList<Tag>() ;
|
||||
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<String> getPhotoUrls() {
|
||||
return photoUrls;
|
||||
}
|
||||
public void setPhotoUrls(List<String> photoUrls) {
|
||||
this.photoUrls = photoUrls;
|
||||
}
|
||||
|
||||
public List<Tag> getTags() {
|
||||
return tags;
|
||||
}
|
||||
public void setTags(List<Tag> 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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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<Pet> 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<Tag> tags = new ArrayList<Tag>();
|
||||
Tag tag1 = new Tag();
|
||||
tag1.setName("friendly");
|
||||
tags.add(tag1);
|
||||
pet.setTags(tags);
|
||||
|
||||
api.updatePet(pet);
|
||||
|
||||
List<Pet> 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<String> photos = Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"});
|
||||
pet.setPhotoUrls(photos);
|
||||
|
||||
return pet;
|
||||
}
|
||||
}
|
@ -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<String, Integer> 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user