Skip to content

Commit 622b5c1

Browse files
committed
Merge remote-tracking branch 'origin/develop_2.0' into java-accept-contenttype
Conflicts: modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache modules/swagger-codegen/src/main/resources/Java/api.mustache samples/client/petstore/java/src/main/java/io/swagger/client/ApiClient.java samples/client/petstore/java/src/main/java/io/swagger/client/api/PetApi.java samples/client/petstore/java/src/main/java/io/swagger/client/api/StoreApi.java samples/client/petstore/java/src/main/java/io/swagger/client/api/UserApi.java
2 parents 951b7a9 + 26d9215 commit 622b5c1

File tree

145 files changed

+5169
-1734
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+5169
-1734
lines changed

LICENSE

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Copyright 2015 SmartBear Software
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at [apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
6+
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.

README.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ NAME
6060
6161
SYNOPSIS
6262
swagger generate [(-a <authorization> | --auth <authorization>)]
63+
[(-c <configuration file> | --config <configuration file>)]
64+
[-D <system properties>]
6365
(-i <spec file> | --input-spec <spec file>)
6466
(-l <language> | --lang <language>)
6567
[(-o <output directory> | --output <output directory>)]
@@ -72,6 +74,16 @@ OPTIONS
7274
remotely. Pass in a URL-encoded string of name:header with a comma
7375
separating multiple values
7476
77+
-c <configuration file>, --config <configuration file>
78+
Path to json configuration file. File content should be in a json
79+
format {"optionKey":"optionValue", "optionKey1":"optionValue1"...}
80+
Supported options can be different for each language. Run
81+
config-help -l {lang} command for language specific config options.
82+
83+
-D <system properties>
84+
sets specified system properties in the format of
85+
name=value,name=value
86+
7587
-i <spec file>, --input-spec <spec file>
7688
location of the swagger spec, as URL or file (required)
7789
@@ -166,8 +178,60 @@ SwaggerYamlGenerator.java
166178
TizenClientCodegen.java
167179
```
168180

169-
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.
181+
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.
182+
183+
```
184+
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
185+
-i http://petstore.swagger.io/v2/swagger.json \
186+
-l java \
187+
-o samples/client/petstore/java \
188+
-c path/to/config.json
189+
```
190+
Supported config options can be different per language. Running `config-help -l {lang}` will show available options.
191+
192+
```
193+
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jarr config-help -l java
194+
```
195+
196+
Output
197+
198+
```
199+
CONFIG OPTIONS
200+
modelPackage
201+
package for generated models
202+
203+
apiPackage
204+
package for generated api classes
205+
206+
invokerPackage
207+
root package for generated code
208+
209+
groupId
210+
groupId in generated pom.xml
211+
212+
artifactId
213+
artifactId in generated pom.xml
214+
215+
artifactVersion
216+
artifact version in generated pom.xml
217+
218+
sourceFolder
219+
source folder for generated code
220+
```
221+
222+
Your config file for java can look like
223+
224+
```
225+
{
226+
"groupId":"com.my.company",
227+
"artifactId":"MyClent",
228+
"artifactVersion":"1.2.0"
229+
}
230+
```
231+
232+
For all the unspecified options default values will be used.
170233

234+
Another way to override default options is to extend config class for specific language.
171235
To change, for example, the prefix for the Objective-C generated files, simply subclass the ObjcClientCodegen.java:
172236

173237
```
@@ -273,7 +337,7 @@ Note! The templates are included in the library generated. If you want to modi
273337
License
274338
-------
275339

276-
Copyright 2015 Reverb Technologies, Inc.
340+
Copyright 2015 SmartBear Software
277341

278342
Licensed under the Apache License, Version 2.0 (the "License");
279343
you may not use this file except in compliance with the License.

bin/all-petstore.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ cd $APP_DIR
2727
./bin/qt5-petstore.sh
2828
./bin/php-petstore.sh
2929
./bin/python-petstore.sh
30+
./bin/retrofit-petstore.sh
3031
./bin/ruby-petstore.sh
3132
./bin/objc-petstore.sh
3233
./bin/scala-petstore.sh

bin/retrofit-petstore.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
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"
30+
31+
java $JAVA_OPTS -jar $executable $ags

modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/SwaggerCodegen.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.wordnik.swagger.codegen;
22

3+
import com.wordnik.swagger.codegen.cmd.ConfigHelp;
34
import com.wordnik.swagger.codegen.cmd.Generate;
45
import com.wordnik.swagger.codegen.cmd.Langs;
56
import com.wordnik.swagger.codegen.cmd.Meta;
@@ -27,7 +28,8 @@ public static void main(String[] args) {
2728
Generate.class,
2829
Meta.class,
2930
Langs.class,
30-
Help.class
31+
Help.class,
32+
ConfigHelp.class
3133
);
3234

3335
builder.build().parse(args).run();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.wordnik.swagger.codegen.cmd;
2+
3+
import com.wordnik.swagger.codegen.CliOption;
4+
import com.wordnik.swagger.codegen.CodegenConfig;
5+
import io.airlift.airline.Command;
6+
import io.airlift.airline.Option;
7+
import java.util.ServiceLoader;
8+
import static java.util.ServiceLoader.load;
9+
10+
@Command(name = "config-help", description = "Config help for chosen lang")
11+
public class ConfigHelp implements Runnable {
12+
13+
@Option(name = {"-l", "--lang"}, title = "language", required = true,
14+
description = "language to get config help for")
15+
private String lang;
16+
17+
@Override
18+
public void run() {
19+
System.out.println();
20+
CodegenConfig config = forName(lang);
21+
System.out.println("CONFIG OPTIONS");
22+
for (CliOption langCliOption : config.cliOptions()) {
23+
System.out.println("\t" + langCliOption.getOpt());
24+
System.out.println("\t " + langCliOption.getDescription());
25+
System.out.println();
26+
}
27+
}
28+
29+
/**
30+
* Tries to load config class with SPI first, then with class name directly from classpath
31+
* @param name name of config, or full qualified class name in classpath
32+
* @return config class
33+
*/
34+
private static CodegenConfig forName(String name) {
35+
ServiceLoader<CodegenConfig> loader = load(CodegenConfig.class);
36+
for (CodegenConfig config : loader) {
37+
if (config.getName().equals(name)) {
38+
return config;
39+
}
40+
}
41+
42+
// else try to load directly
43+
try {
44+
return (CodegenConfig) Class.forName(name).newInstance();
45+
} catch (Exception e) {
46+
throw new RuntimeException("Can't load config class with name ".concat(name), e);
47+
}
48+
}
49+
}

modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/Generate.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.wordnik.swagger.codegen.cmd;
22

3+
import com.wordnik.swagger.codegen.CliOption;
34
import com.wordnik.swagger.codegen.ClientOptInput;
45
import com.wordnik.swagger.codegen.ClientOpts;
56
import com.wordnik.swagger.codegen.CodegenConfig;
67
import com.wordnik.swagger.codegen.DefaultGenerator;
78
import com.wordnik.swagger.models.Swagger;
9+
import config.Config;
10+
import config.ConfigParser;
811
import io.airlift.airline.Command;
912
import io.airlift.airline.Option;
1013
import io.swagger.parser.SwaggerParser;
@@ -57,6 +60,11 @@ public class Generate implements Runnable {
5760
@Option( name= {"-D"}, title = "system properties", description = "sets specified system properties in " +
5861
"the format of name=value,name=value")
5962
private String systemProperties;
63+
64+
@Option( name= {"-c", "--config"}, title = "configuration file", description = "Path to json configuration file. " +
65+
"File content should be in a json format {\"optionKey\":\"optionValue\", \"optionKey1\":\"optionValue1\"...} " +
66+
"Supported options can be different for each language. Run config-help -l {lang} command for language specific config options.")
67+
private String configFile;
6068

6169
@Override
6270
public void run() {
@@ -76,6 +84,17 @@ public void run() {
7684
if (null != templateDir) {
7785
config.additionalProperties().put(TEMPLATE_DIR_PARAM, new File(templateDir).getAbsolutePath());
7886
}
87+
88+
if(null != configFile){
89+
Config genConfig = ConfigParser.read(configFile);
90+
if (null != genConfig) {
91+
for (CliOption langCliOption : config.cliOptions()) {
92+
if (genConfig.hasOption(langCliOption.getOpt())) {
93+
config.additionalProperties().put(langCliOption.getOpt(), genConfig.getOption(langCliOption.getOpt()));
94+
}
95+
}
96+
}
97+
}
7998

8099
input.setConfig(config);
81100

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.wordnik.swagger.codegen;
2+
3+
public class CliOption {
4+
private final String opt;
5+
private String description;
6+
7+
public CliOption(String opt, String description) {
8+
this.opt = opt;
9+
this.description = description;
10+
}
11+
12+
public String getOpt() {
13+
return opt;
14+
}
15+
16+
public String getDescription() {
17+
return description;
18+
}
19+
20+
public void setDescription(String description) {
21+
this.description = description;
22+
}
23+
}

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/Codegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,4 @@ public static CodegenConfig getConfig(String name) {
140140
}
141141
}
142142
}
143-
}
143+
}

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CodegenConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public interface CodegenConfig {
2727
String getTypeDeclaration(Property p);
2828
String getTypeDeclaration(String name);
2929
void processOpts();
30+
List<CliOption> cliOptions();
3031
String generateExamplePath(String path, Operation operation);
3132

3233
Set<String> reservedWords();

0 commit comments

Comments
 (0)