Skip to content

Commit e64fc11

Browse files
author
e507804
committed
Added new option to define spring5 for resttemplate
1 parent aba5ca8 commit e64fc11

File tree

120 files changed

+11141
-24
lines changed

Some content is hidden

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

120 files changed

+11141
-24
lines changed

bin/java-petstore-all.sh

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
./bin/java-petstore-retrofit2-play24.sh
1515
./bin/java-petstore-jersey2-java6.sh
1616
./bin/java-petstore-resttemplate.sh
17+
./bin/java-petstore-resttemplate-spring5.sh
1718
./bin/java-petstore-resttemplate-withxml.sh
1819
./bin/java-petstore-resteasy.sh
1920
./bin/java-petstore-google-api-client.sh
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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-with-fake-endpoints-models-for-testing.yaml --artifact-id swagger-pestore-resttemplate-spring5 -l java -c bin/java-petstore-resttemplate.json -o samples/client/petstore/java/resttemplate-spring5 -DhideGenerationTimestamp=true,spring5=true"
30+
31+
echo "Removing files and folders under samples/client/petstore/java/resttemplate/src/main"
32+
rm -rf samples/client/petstore/java/resttemplate-spring5/src/main
33+
find samples/client/petstore/java/resttemplate-spring5 -maxdepth 1 -type f ! -name "README.md" -exec rm {} +
34+
java $JAVA_OPTS -jar $executable $ags

modules/swagger-codegen-maven-plugin/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ mvn clean compile
6363

6464
### Custom Generator
6565

66-
Specifying a custom generator is a bit different. It doesn't support the classpath:/ syntax, but it does support the fully qualified name of the package. You can also specify your custom templates, which also get pulled in. Notice the dependency on a project, in the plugin scope. That would be your generator/template jar.
66+
Specifying a custom generator is a bit different. It doesn't support the classpath:/ syntax, but it does support the fully qualified name of the package. You can also specify your custom templates, whi
67+
ch also get pulled in. Notice the dependency on a project, in the plugin scope. That would be your generator/template jar.
6768

6869
```xml
6970
<plugin>

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class JavaClientCodegen extends AbstractJavaCodegen
3737
public static final String PLAY_24 = "play24";
3838
public static final String PLAY_25 = "play25";
3939

40+
public static final String SPRING_5 = "spring5";
41+
4042
public static final String RETROFIT_1 = "retrofit";
4143
public static final String RETROFIT_2 = "retrofit2";
4244
public static final String REST_ASSURED = "rest-assured";
@@ -52,6 +54,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
5254
protected boolean performBeanValidation = false;
5355
protected boolean useGzipFeature = false;
5456
protected boolean useRuntimeException = false;
57+
protected boolean useSpring5 = false;
5558

5659

5760
public JavaClientCodegen() {
@@ -73,14 +76,15 @@ public JavaClientCodegen() {
7376
cliOptions.add(CliOption.newBoolean(PERFORM_BEANVALIDATION, "Perform BeanValidation"));
7477
cliOptions.add(CliOption.newBoolean(USE_GZIP_FEATURE, "Send gzip-encoded requests"));
7578
cliOptions.add(CliOption.newBoolean(USE_RUNTIME_EXCEPTION, "Use RuntimeException instead of Exception"));
79+
cliOptions.add(CliOption.newBoolean(SPRING_5, "Spring version. Default is 4, if the flag is set, Spring 5 is used. Currently only used with Resttemplate"));
7680

7781
supportedLibraries.put("jersey1", "HTTP client: Jersey client 1.19.4. JSON processing: Jackson 2.8.9. Enable Java6 support using '-DsupportJava6=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
7882
supportedLibraries.put("feign", "HTTP client: OpenFeign 9.4.0. JSON processing: Jackson 2.8.9");
7983
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.25.1. JSON processing: Jackson 2.8.9");
8084
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.8.1. Enable Parcelable models on Android using '-DparcelableModel=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
8185
supportedLibraries.put(RETROFIT_1, "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.3.1 (Retrofit 1.9.0). IMPORTANT NOTE: retrofit1.x is no longer actively maintained so please upgrade to 'retrofit2' instead.");
8286
supportedLibraries.put(RETROFIT_2, "HTTP client: OkHttp 3.8.0. JSON processing: Gson 2.6.1 (Retrofit 2.3.0). Enable the RxJava adapter using '-DuseRxJava[2]=true'. (RxJava 1.x or 2.x)");
83-
supportedLibraries.put("resttemplate", "HTTP client: Spring RestTemplate 4.3.9-RELEASE. JSON processing: Jackson 2.8.9");
87+
supportedLibraries.put("resttemplate", "HTTP client: Spring RestTemplate 4.3.9-RELEASE. JSON processing: Jackson 2.8.9. Using -Dspring5=true uses Spring 5.1.5 and Jackson 2.9.4");
8488
supportedLibraries.put("resteasy", "HTTP client: Resteasy client 3.1.3.Final. JSON processing: Jackson 2.8.9");
8589
supportedLibraries.put("vertx", "HTTP client: VertX client 3.2.4. JSON processing: Jackson 2.8.9");
8690
supportedLibraries.put("google-api-client", "HTTP client: Google API client 1.23.0. JSON processing: Jackson 2.8.9");
@@ -157,6 +161,10 @@ public void processOpts() {
157161
this.setUseRuntimeException(convertPropertyToBooleanAndWriteBack(USE_RUNTIME_EXCEPTION));
158162
}
159163

164+
if(additionalProperties.containsKey(SPRING_5)) {
165+
this.setUseSpring5(Boolean.valueOf(additionalProperties.get(SPRING_5).toString()));
166+
}
167+
160168
final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");
161169
final String authFolder = (sourceFolder + '/' + invokerPackage + ".auth").replace(".", "/");
162170
final String apiFolder = (sourceFolder + '/' + apiPackage).replace(".", "/");
@@ -611,4 +619,7 @@ static boolean isJsonVendorMimeType(String mime) {
611619
return mime != null && JSON_VENDOR_MIME_PATTERN.matcher(mime).matches();
612620
}
613621

622+
public void setUseSpring5(boolean useSpring5) {
623+
this.useSpring5 = useSpring5;
624+
}
614625
}

modules/swagger-codegen/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache

+18-9
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ import java.util.List;
6060
import java.util.Map;
6161
import java.util.Map.Entry;
6262
import java.util.TimeZone;
63-
import java.util.stream.IntStream;
6463

6564
import {{invokerPackage}}.auth.Authentication;
6665
import {{invokerPackage}}.auth.HttpBasicAuth;
@@ -336,6 +335,9 @@ public class ApiClient {
336335

337336
/**
338337
* Parse the given string into Date object.
338+
*
339+
* @param str Date
340+
* @return Date in string format
339341
*/
340342
public Date parseDate(String str) {
341343
try {
@@ -347,6 +349,9 @@ public class ApiClient {
347349

348350
/**
349351
* Format the given Date object into string.
352+
*
353+
* @param date Date
354+
* @return Date in string format
350355
*/
351356
public String formatDate(Date date) {
352357
return dateFormat.format(date);
@@ -527,24 +532,28 @@ public class ApiClient {
527532
updateParamsForAuth(authNames, queryParams, headerParams);
528533
529534
Map<String, String> params = new HashMap<>();
530-
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path).encode();
535+
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
531536
532537
if (queryParams != null) {
533538
builder.queryParams(queryParams);
534539
LinkedMultiValueMap<String, String> keys = new LinkedMultiValueMap<>();
535540
536541
for (Map.Entry<String, List<String>> e : queryParams.entrySet()) {
537-
IntStream.range(0, e.getValue().size())
538-
.forEach(i -> {
539-
keys.add(e.getKey(), "{" + e.getKey() + i + "}");
540-
params.put(e.getKey() + i, e.getValue().get(i));
541-
});
542+
for(int i = 0; i < e.getValue().size(); i++) {
543+
keys.add(e.getKey(), "{" + e.getKey() + i + "}");
544+
params.put(e.getKey() + i, e.getValue().get(i));
545+
}
542546
}
543547

544548
builder.queryParams(keys);
545549
}
546-
547-
final BodyBuilder requestBuilder = RequestEntity.method(method, builder.buildAndExpand(params).toUri());
550+
551+
{{#spring5}}
552+
final BodyBuilder requestBuilder = RequestEntity.method(method, builder.encode().buildAndExpand(params).toUri());
553+
{{/spring5}}
554+
{{^spring5}}
555+
final BodyBuilder requestBuilder = RequestEntity.method(method, builder.buildAndExpand(params).toUri());
556+
{{/spring5}}
548557
if(accept != null) {
549558
requestBuilder.accept(accept.toArray(new MediaType[accept.size()]));
550559
}

modules/swagger-codegen/src/main/resources/Java/libraries/resttemplate/pom.mustache

+13
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@
217217
<artifactId>spring-web</artifactId>
218218
<version>${spring-web-version}</version>
219219
</dependency>
220+
{{#spring5}}
221+
<dependency>
222+
<groupId>org.springframework</groupId>
223+
<artifactId>spring-context</artifactId>
224+
<version>${spring-web-version}</version>
225+
</dependency>
226+
{{/spring5}}
220227

221228
<!-- JSON processing: jackson -->
222229
<dependency>
@@ -287,8 +294,14 @@
287294
<properties>
288295
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
289296
<swagger-annotations-version>1.5.17</swagger-annotations-version>
297+
{{#spring5}}
298+
<spring-web-version>5.1.5.RELEASE</spring-web-version>
299+
<jackson-version>2.9.4</jackson-version>
300+
{{/spring5}}
301+
{{^spring5}}
290302
<spring-web-version>4.3.9.RELEASE</spring-web-version>
291303
<jackson-version>2.8.9</jackson-version>
304+
{{/spring5}}
292305
{{#joda}}
293306
<jodatime-version>2.9.9</jodatime-version>
294307
{{/joda}}

modules/swagger-codegen/src/test/java/io/swagger/codegen/options/JavaClientOptionsProvider.java

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public Map<String, String> createOptions() {
2727
options.put(JavaClientCodegen.USE_GZIP_FEATURE, "false");
2828
options.put(JavaClientCodegen.USE_RUNTIME_EXCEPTION, "false");
2929
options.put(JavaClientCodegen.JAVA8_MODE, "false");
30+
options.put(JavaClientCodegen.SPRING_5, "false");
3031
return options;
3132
}
3233

pom.xml.circleci

+1
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@
836836
<module>samples/client/petstore/java/retrofit2rx</module>
837837
<module>samples/client/petstore/jaxrs-cxf-client</module>
838838
<module>samples/client/petstore/java/resttemplate</module>
839+
<module>samples/client/petstore/java/resttemplate-spring5</module>
839840
<module>samples/client/petstore/java/resttemplate-withXml</module>
840841
<module>samples/client/petstore/java/vertx</module>
841842
<module>samples/client/petstore/java/resteasy</module>

pom.xml.circleci.java7

+1
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@
836836
<module>samples/client/petstore/java/retrofit2rx</module>
837837
<module>samples/client/petstore/jaxrs-cxf-client</module>
838838
<module>samples/client/petstore/java/resttemplate</module>
839+
<module>samples/client/petstore/java/resttemplate-spring5</module>
839840
<module>samples/client/petstore/java/resttemplate-withXml</module>
840841
<module>samples/client/petstore/java/vertx</module>
841842
<module>samples/client/petstore/java/resteasy</module>

pom.xml.jenkins

+1
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,7 @@
878878
<module>samples/client/petstore/java/retrofit2rx</module>
879879
<module>samples/client/petstore/jaxrs-cxf-client</module>
880880
<module>samples/client/petstore/java/resttemplate</module>
881+
<module>samples/client/petstore/java/resttemplate-spring5</module>
881882
<module>samples/client/petstore/java/resttemplate-withXml</module>
882883
<module>samples/client/petstore/java/vertx</module>
883884
<module>samples/client/petstore/java/resteasy</module>

pom.xml.jenkins.java7

+1
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@
830830
<module>samples/client/petstore/java/retrofit2</module>
831831
<module>samples/client/petstore/java/retrofit2rx</module>
832832
<module>samples/client/petstore/java/resttemplate</module>
833+
<module>samples/client/petstore/java/resttemplate-spring5</module>
833834
<module>samples/client/petstore/java/resttemplate-withXml</module>
834835
<module>samples/client/petstore/java/google-api-client</module>
835836
<module>samples/client/petstore/kotlin/</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
*.class
2+
3+
# Mobile Tools for Java (J2ME)
4+
.mtj.tmp/
5+
6+
# Package Files #
7+
*.jar
8+
*.war
9+
*.ear
10+
11+
# exclude jar for gradle wrapper
12+
!gradle/wrapper/*.jar
13+
14+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
15+
hs_err_pid*
16+
17+
# build files
18+
**/target
19+
target
20+
.gradle
21+
build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Swagger Codegen Ignore
2+
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.4.5-SNAPSHOT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Generated by: https://github.com/swagger-api/swagger-codegen.git
3+
#
4+
language: java
5+
jdk:
6+
- oraclejdk8
7+
- oraclejdk7
8+
before_install:
9+
# ensure gradlew has proper permission
10+
- chmod a+x ./gradlew
11+
script:
12+
# test using maven
13+
- mvn test
14+
# uncomment below to test using gradle
15+
# - gradle test
16+
# uncomment below to test using sbt
17+
# - sbt test

0 commit comments

Comments
 (0)