Skip to content

Commit 6db358d

Browse files
committed
Merge remote-tracking branch 'upstream/master'
Conflicts: modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java
2 parents f13b4de + 564fa04 commit 6db358d

File tree

142 files changed

+5810
-540
lines changed

Some content is hidden

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

142 files changed

+5810
-540
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ atlassian-ide-plugin.xml
4848
packages/
4949
.pub
5050
.packages
51+
.vagrant/
5152

5253
samples/client/petstore/php/SwaggerClient-php/composer.lock
5354
samples/client/petstore/php/SwaggerClient-php/vendor/

README.md

+22-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Check out [Swagger-Spec](https://github.com/OAI/OpenAPI-Specification) for addit
2323
- [Building](#building)
2424
- [Docker](#docker)
2525
- [Build and run](#build-and-run-using-docker)
26-
- [Build a Node.js server stub](#build-a-nodejs-server-stub)
26+
- [Run docker in Vagrant](#run-docker-in-vagrant)
2727
- [Public Docker image](#public-docker-image)
2828
- [Homebrew](#homebrew)
2929
- Generators
@@ -104,19 +104,21 @@ mvn package
104104

105105
```
106106
git clone https://github.com/swagger-api/swagger-codegen
107-
108107
cd swagger-codegen
109-
110108
./run-in-docker.sh mvn package
111109
```
112110

113-
#### Build a Node.js server stub
114111

112+
113+
#### Run Docker in Vagrant
114+
Prerequisite: install [Vagrant](https://www.vagrantup.com/downloads.html) and [VirtualBox](https://www.virtualbox.org/wiki/Downloads).
115115
```
116-
./run-in-docker.sh generate \
117-
-i http://petstore.swagger.io/v2/swagger.json \
118-
-l nodejs \
119-
-o samples/server/petstore/nodejs
116+
git clone http://github.com/swagger-api/swagger-codegen.git
117+
cd swagger-codegen
118+
vagrant up
119+
vagrant ssh
120+
cd /vagrant
121+
sudo ./run-in-docker.sh mvn package
120122
```
121123

122124
#### Public Docker image
@@ -408,7 +410,7 @@ Your config file for Java can look like
408410
```json
409411
{
410412
"groupId":"com.my.company",
411-
"artifactId":"MyClent",
413+
"artifactId":"MyClient",
412414
"artifactVersion":"1.2.0",
413415
"library":"feign"
414416
}
@@ -534,13 +536,22 @@ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
534536
-o samples/server/petstore/scalatra
535537
```
536538

537-
### Java JAX-RS
539+
### Java JAX-RS (Jersey v1.18)
538540

539541
```
540542
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
541543
-i http://petstore.swagger.io/v2/swagger.json \
542544
-l jaxrs \
543-
-o samples/server/petstore/jaxrs
545+
-o samples/server/petstore/jaxrs-jersey
546+
```
547+
548+
### Java JAX-RS (Apache CXF 3)
549+
550+
```
551+
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
552+
-i http://petstore.swagger.io/v2/swagger.json \
553+
-l jaxrs-cxf \
554+
-o samples/server/petstore/jaxrs-cxf
544555
```
545556

546557
### Java Spring MVC

Vagrantfile

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
5+
VAGRANTFILE_API_VERSION = "2"
6+
7+
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
8+
9+
config.vm.box = "ubuntu/trusty64"
10+
11+
config.vm.provider "virtualbox" do |v|
12+
v.name = "swagger-codegen"
13+
v.memory = 2048
14+
v.cpus = 2
15+
end
16+
17+
config.vm.box_check_update = true
18+
19+
#SSH
20+
config.ssh.forward_agent = true
21+
22+
config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'"
23+
24+
#Provision
25+
config.vm.provision "shell", inline: <<-SHELL
26+
sudo apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
27+
sudo sh -c 'echo "deb https://apt.dockerproject.org/repo ubuntu-trusty main" > /etc/apt/sources.list.d/docker.list'
28+
sudo apt-cache policy docker-engine
29+
sudo apt-get update
30+
sudo apt-get upgrade -y
31+
sudo apt-get install -y docker-engine
32+
SHELL
33+
34+
end

bin/java-petstore-retrofit2rx.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"library": "retrofit2",
3+
"artifactId": "swagger-petstore-retrofit2-rx"
4+
}

bin/java-petstore-retrofit2rx.sh

+31
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 -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l java -c bin/java-petstore-retrofit2rx.json -o samples/client/petstore/java/retrofit2rx -DuseRxJava=true"
30+
31+
java $JAVA_OPTS -jar $executable $ags

bin/jaxrs-cxf-petstore-server.sh

+31
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/JavaJaxRS -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l jaxrs-cxf -o samples/server/petstore/jaxrs-cxf"
30+
31+
java $JAVA_OPTS -jar $executable $ags

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import io.swagger.codegen.ClientOptInput;
66
import io.swagger.codegen.CodegenConstants;
77
import io.swagger.codegen.DefaultGenerator;
8-
import io.swagger.codegen.cmd.utils.OptionUtils;
8+
import io.swagger.codegen.utils.OptionUtils;
99
import io.swagger.codegen.config.CodegenConfigurator;
1010
import org.apache.commons.lang3.tuple.Pair;
1111
import org.slf4j.Logger;

modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Meta.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"specify, and includes default templates to include.")
3434
public class Meta implements Runnable {
3535

36-
private static final Logger LOG = LoggerFactory.getLogger(Meta.class);
36+
private static final Logger LOGGER = LoggerFactory.getLogger(Meta.class);
3737

3838
private static final String TEMPLATE_DIR_CLASSPATH = "codegen";
3939
private static final String MUSTACHE_EXTENSION = ".mustache";
@@ -53,7 +53,7 @@ public class Meta implements Runnable {
5353
@Override
5454
public void run() {
5555
final File targetDir = new File(outputFolder);
56-
LOG.info("writing to folder [{}]", targetDir.getAbsolutePath());
56+
LOGGER.info("writing to folder [{}]", targetDir.getAbsolutePath());
5757

5858
String mainClass = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, name) + "Generator";
5959

@@ -108,13 +108,13 @@ public File convert(SupportingFile support) {
108108
String formatted = template;
109109

110110
if (support.templateFile.endsWith(MUSTACHE_EXTENSION)) {
111-
LOG.info("writing file to {}", outputFile.getAbsolutePath());
111+
LOGGER.info("writing file to {}", outputFile.getAbsolutePath());
112112
formatted = Mustache.compiler().withLoader(loader(generator))
113113
.defaultValue("")
114114
.compile(template)
115115
.execute(data);
116116
} else {
117-
LOG.info("copying file to {}", outputFile.getAbsolutePath());
117+
LOGGER.info("copying file to {}", outputFile.getAbsolutePath());
118118
}
119119

120120
FileUtils.writeStringToFile(outputFile, formatted);

modules/swagger-codegen-cli/src/test/java/io/swagger/codegen/cmd/utils/OptionUtilsTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.swagger.codegen.cmd.utils;
22

3+
import io.swagger.codegen.utils.OptionUtils;
34
import org.apache.commons.lang3.tuple.Pair;
45
import org.junit.Test;
56

modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java

+29
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import config.Config;
2020
import config.ConfigParser;
2121
import io.swagger.codegen.*;
22+
import io.swagger.codegen.utils.OptionUtils;
2223
import io.swagger.models.Swagger;
2324
import io.swagger.parser.SwaggerParser;
25+
import org.apache.commons.lang3.tuple.Pair;
2426
import org.apache.maven.plugin.AbstractMojo;
2527
import org.apache.maven.plugin.MojoExecutionException;
2628
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -31,6 +33,7 @@
3133
import java.io.File;
3234
import java.util.HashMap;
3335
import java.util.HashSet;
36+
import java.util.List;
3437
import java.util.Map;
3538
import java.util.Set;
3639

@@ -161,6 +164,20 @@ public void execute() throws MojoExecutionException {
161164
}
162165

163166
if (configOptions != null) {
167+
if(configOptions.containsKey("import-mappings")) {
168+
Map<String, String> mappings = createMapFromKeyValuePairs(configOptions.remove("import-mappings").toString());
169+
config.importMapping().putAll(mappings);
170+
}
171+
172+
if(configOptions.containsKey("type-mappings")) {
173+
Map<String, String> mappings = createMapFromKeyValuePairs(configOptions.remove("type-mappings").toString());
174+
config.typeMapping().putAll(mappings);
175+
}
176+
177+
if(configOptions.containsKey("instantiation-types")) {
178+
Map<String, String> mappings = createMapFromKeyValuePairs(configOptions.remove("instantiation-types").toString());
179+
config.instantiationTypes().putAll(mappings);
180+
}
164181
addAdditionalProperties(config, definedOptions, configOptions);
165182
}
166183

@@ -207,4 +224,16 @@ private void addAdditionalProperties(CodegenConfig config, Set<String> definedOp
207224
}
208225
}
209226
}
227+
228+
private static Map<String, String> createMapFromKeyValuePairs(String commaSeparatedKVPairs) {
229+
final List<Pair<String, String>> pairs = OptionUtils.parseCommaSeparatedTuples(commaSeparatedKVPairs);
230+
231+
Map<String, String> result = new HashMap<String, String>();
232+
233+
for (Pair<String, String> pair : pairs) {
234+
result.put(pair.getLeft(), pair.getRight());
235+
}
236+
237+
return result;
238+
}
210239
}

modules/swagger-codegen/src/main/java/config/ConfigParser.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
import java.util.Iterator;
88
import java.util.Map;
99

10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
1013
public class ConfigParser {
1114

15+
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigParser.class);
16+
1217
public static Config read(String location) {
1318

14-
System.out.println("reading config from " + location);
19+
LOGGER.info("reading config from " + location);
1520

1621
ObjectMapper mapper = new ObjectMapper();
1722

@@ -27,11 +32,11 @@ public static Config read(String location) {
2732
if (optionNode.getValue().isValueNode()) {
2833
config.setOption(optionNode.getKey(), optionNode.getValue().asText());
2934
} else {
30-
System.out.println("omitting non-value node " + optionNode.getKey());
35+
LOGGER.warn("omitting non-value node " + optionNode.getKey());
3136
}
3237
}
3338
} catch (Exception e) {
34-
System.out.println(e.getMessage());
39+
LOGGER.error(e.getMessage());
3540
return null;
3641
}
3742

modules/swagger-codegen/src/main/java/io/swagger/codegen/AbstractGenerator.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010
import java.io.OutputStreamWriter;
1111
import java.io.Reader;
1212
import java.io.Writer;
13+
import java.util.Scanner;
1314
import java.util.regex.Pattern;
1415

16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
1519
public abstract class AbstractGenerator {
20+
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractGenerator.class);
1621

1722
@SuppressWarnings("static-method")
1823
public File writeToFile(String filename, String contents) throws IOException {
19-
System.out.println("writing file " + filename);
24+
LOGGER.info("writing file " + filename);
2025
File output = new File(filename);
2126

2227
if (output.getParent() != null && !new File(output.getParent()).exists()) {
@@ -37,10 +42,10 @@ public String readTemplate(String name) {
3742
if (reader == null) {
3843
throw new RuntimeException("no file found");
3944
}
40-
java.util.Scanner s = new java.util.Scanner(reader).useDelimiter("\\A");
45+
Scanner s = new Scanner(reader).useDelimiter("\\A");
4146
return s.hasNext() ? s.next() : "";
4247
} catch (Exception e) {
43-
e.printStackTrace();
48+
LOGGER.error(e.getMessage());
4449
}
4550
throw new RuntimeException("can't load template " + name);
4651
}
@@ -53,7 +58,7 @@ public Reader getTemplateReader(String name) {
5358
}
5459
return new InputStreamReader(is);
5560
} catch (Exception e) {
56-
e.printStackTrace();
61+
LOGGER.error(e.getMessage());
5762
}
5863
throw new RuntimeException("can't load template " + name);
5964
}

0 commit comments

Comments
 (0)