Skip to content

Commit d8a87c2

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

File tree

13 files changed

+99
-19
lines changed

13 files changed

+99
-19
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

+11
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,6 +76,7 @@ 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");
@@ -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-8
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ public class ApiClient {
336336

337337
/**
338338
* Parse the given string into Date object.
339+
*
340+
* @param str Date
341+
* @return Date in string format
339342
*/
340343
public Date parseDate(String str) {
341344
try {
@@ -347,6 +350,9 @@ public class ApiClient {
347350

348351
/**
349352
* Format the given Date object into string.
353+
*
354+
* @param date Date
355+
* @return Date in string format
350356
*/
351357
public String formatDate(Date date) {
352358
return dateFormat.format(date);
@@ -527,24 +533,28 @@ public class ApiClient {
527533
updateParamsForAuth(authNames, queryParams, headerParams);
528534
529535
Map<String, String> params = new HashMap<>();
530-
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path).encode();
536+
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
531537
532538
if (queryParams != null) {
533539
builder.queryParams(queryParams);
534540
LinkedMultiValueMap<String, String> keys = new LinkedMultiValueMap<>();
535541
536542
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-
});
543+
for(int i = 0; i < e.getValue().size(); i++) {
544+
keys.add(e.getKey(), "{" + e.getKey() + i + "}");
545+
params.put(e.getKey() + i, e.getValue().get(i));
546+
}
542547
}
543548

544549
builder.queryParams(keys);
545550
}
546-
547-
final BodyBuilder requestBuilder = RequestEntity.method(method, builder.buildAndExpand(params).toUri());
551+
552+
{{#spring5}}
553+
final BodyBuilder requestBuilder = RequestEntity.method(method, builder.encode().buildAndExpand(params).toUri());
554+
{{/spring5}}
555+
{{^spring5}}
556+
final BodyBuilder requestBuilder = RequestEntity.method(method, builder.buildAndExpand(params).toUri());
557+
{{/spring5}}
548558
if(accept != null) {
549559
requestBuilder.accept(accept.toArray(new MediaType[accept.size()]));
550560
}

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

+14-1
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>
290-
<spring-web-version>4.3.9.RELEASE</spring-web-version>
297+
{{#spring5}}
298+
<spring-web-version>5.1.5.RELEASE</spring-web-version>
291299
<jackson-version>2.8.9</jackson-version>
300+
{{/spring5}}
301+
{{^spring5}}
302+
<spring-web-version>4.3.9.RELEASE</spring-web-version>
303+
<jackson-version>2.9.4</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>

samples/client/petstore/java/resttemplate/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@
250250
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
251251
<swagger-annotations-version>1.5.17</swagger-annotations-version>
252252
<spring-web-version>4.3.9.RELEASE</spring-web-version>
253-
<jackson-version>2.8.9</jackson-version>
253+
<jackson-version>2.9.4</jackson-version>
254254
<jackson-threetenbp-version>2.6.4</jackson-threetenbp-version>
255255
<maven-plugin-version>1.0.0</maven-plugin-version>
256256
<junit-version>4.12</junit-version>

samples/client/petstore/java/resttemplate/src/main/java/io/swagger/client/ApiClient.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ public ApiClient setDateFormat(DateFormat dateFormat) {
324324

325325
/**
326326
* Parse the given string into Date object.
327+
*
328+
* @param str Date
329+
* @return Date in string format
327330
*/
328331
public Date parseDate(String str) {
329332
try {
@@ -335,6 +338,9 @@ public Date parseDate(String str) {
335338

336339
/**
337340
* Format the given Date object into string.
341+
*
342+
* @param date Date
343+
* @return Date in string format
338344
*/
339345
public String formatDate(Date date) {
340346
return dateFormat.format(date);
@@ -515,24 +521,23 @@ public <T> T invokeAPI(String path, HttpMethod method, MultiValueMap<String, Str
515521
updateParamsForAuth(authNames, queryParams, headerParams);
516522

517523
Map<String, String> params = new HashMap<>();
518-
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path).encode();
524+
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
519525

520526
if (queryParams != null) {
521527
builder.queryParams(queryParams);
522528
LinkedMultiValueMap<String, String> keys = new LinkedMultiValueMap<>();
523529

524530
for (Map.Entry<String, List<String>> e : queryParams.entrySet()) {
525-
IntStream.range(0, e.getValue().size())
526-
.forEach(i -> {
527-
keys.add(e.getKey(), "{" + e.getKey() + i + "}");
528-
params.put(e.getKey() + i, e.getValue().get(i));
529-
});
531+
for(int i = 0; i < e.getValue().size(); i++) {
532+
keys.add(e.getKey(), "{" + e.getKey() + i + "}");
533+
params.put(e.getKey() + i, e.getValue().get(i));
534+
}
530535
}
531536

532537
builder.queryParams(keys);
533538
}
534-
535-
final BodyBuilder requestBuilder = RequestEntity.method(method, builder.buildAndExpand(params).toUri());
539+
540+
final BodyBuilder requestBuilder = RequestEntity.method(method, builder.buildAndExpand(params).toUri());
536541
if(accept != null) {
537542
requestBuilder.accept(accept.toArray(new MediaType[accept.size()]));
538543
}

0 commit comments

Comments
 (0)