Skip to content

Commit

Permalink
[Java] parameterize formParams (OpenAPITools#5784)
Browse files Browse the repository at this point in the history
* parameterize formParams

fixes OpenAPITools#5782

* sync generated code

* update to latest version of spring, replace MultiValueMap#put with #addAll, remove unused imports

* restore java.util.HashMap import

* update spring version in pom/gradle templates

* re-generate resttemplate samples

* fix test

* fix test

* parameterize an additional usage of LinkedMultiValueMap

* re-generate resttemplate samples

* re-generate webclient samples

* manually edit FILES to replace \ with /
  • Loading branch information
jonfreedman authored Aug 12, 2020
1 parent a1484da commit 9ba34b2
Show file tree
Hide file tree
Showing 128 changed files with 738 additions and 787 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {{invokerPackage}}.ApiClient;
{{#imports}}import {{import}};
{{/imports}}

{{^fullJavaUtil}}import java.util.ArrayList;
import java.util.Collections;
{{^fullJavaUtil}}import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand All @@ -19,7 +18,6 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -117,7 +115,7 @@ public class {{classname}} {
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
final MultiValueMap<String, String> cookieParams = new LinkedMultiValueMap<String, String>();
final MultiValueMap formParams = new LinkedMultiValueMap();{{#hasQueryParams}}
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();{{#hasQueryParams}}

{{#queryParams}}queryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{collectionFormat}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}));{{#hasMore}}
{{/hasMore}}{{/queryParams}}{{/hasQueryParams}}{{#hasHeaderParams}}
Expand All @@ -131,7 +129,7 @@ public class {{classname}} {
{{/hasMore}}{{/cookieParams}}{{/hasCookieParams}}{{#hasFormParams}}

{{#formParams}}if ({{paramName}} != null)
formParams.{{^collectionFormat}}add{{/collectionFormat}}{{#collectionFormat}}put{{/collectionFormat}}("{{baseName}}", {{#isFile}}{{^collectionFormat}}{{#useAbstractionForFiles}}{{paramName}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}new FileSystemResource({{paramName}}){{/useAbstractionForFiles}}{{/collectionFormat}}{{/isFile}}{{#isFile}}{{#collectionFormat}}{{paramName}}.stream(){{^useAbstractionForFiles}}.map(FileSystemResource::new){{/useAbstractionForFiles}}.collect(Collectors.toList()){{/collectionFormat}}{{/isFile}}{{^isFile}}{{paramName}}{{/isFile}});{{#hasMore}}
formParams.{{^collectionFormat}}add{{/collectionFormat}}{{#collectionFormat}}addAll{{/collectionFormat}}("{{baseName}}", {{#isFile}}{{^collectionFormat}}{{#useAbstractionForFiles}}{{paramName}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}new FileSystemResource({{paramName}}){{/useAbstractionForFiles}}{{/collectionFormat}}{{/isFile}}{{#isFile}}{{#collectionFormat}}{{paramName}}.stream(){{^useAbstractionForFiles}}.map(FileSystemResource::new){{/useAbstractionForFiles}}.collect(Collectors.toList()){{/collectionFormat}}{{/isFile}}{{^isFile}}{{paramName}}{{/isFile}});{{#hasMore}}
{{/hasMore}}{{/formParams}}{{/hasFormParams}}

final String[] localVarAccepts = { {{#hasProduces}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if(hasProperty('target') && target == 'android') {
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
Expand All @@ -48,7 +48,7 @@ if(hasProperty('target') && target == 'android') {
{{/java8}}
{{/supportJava6}}
}

// Rename the aar correctly
libraryVariants.all { variant ->
variant.outputs.each { output ->
Expand All @@ -64,7 +64,7 @@ if(hasProperty('target') && target == 'android') {
provided 'javax.annotation:jsr250-api:1.0'
}
}

afterEvaluate {
android.libraryVariants.all { variant ->
def task = project.tasks.create "jar${variant.name.capitalize()}", Jar
Expand All @@ -76,12 +76,12 @@ if(hasProperty('target') && target == 'android') {
artifacts.add('archives', task);
}
}

task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}

artifacts {
archives sourcesJar
}
Expand Down Expand Up @@ -111,7 +111,7 @@ if(hasProperty('target') && target == 'android') {
pom.artifactId = '{{artifactId}}'
}
}

task execute(type:JavaExec) {
main = System.getProperty('mainClass')
classpath = sourceSets.main.runtimeClasspath
Expand All @@ -123,7 +123,7 @@ ext {
jackson_version = "2.10.4"
jackson_databind_version = "2.10.4"
jackson_databind_nullable_version = "0.2.1"
spring_web_version = "4.3.9.RELEASE"
spring_web_version = "5.2.5.RELEASE"
jodatime_version = "2.9.9"
junit_version = "4.13"
{{#threetenbp}}
Expand All @@ -135,6 +135,7 @@ dependencies {
compile "io.swagger:swagger-annotations:$swagger_annotations_version"
compile "com.google.code.findbugs:jsr305:3.0.2"
compile "org.springframework:spring-web:$spring_web_version"
compile "org.springframework:spring-context:$spring_web_version"
compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_databind_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations-version}</version>
</dependency>

<!-- @Nullable annotation -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
Expand All @@ -236,6 +236,11 @@
<artifactId>spring-web</artifactId>
<version>${spring-web-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-web-version}</version>
</dependency>

<!-- JSON processing: jackson -->
<dependency>
Expand Down Expand Up @@ -317,7 +322,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<swagger-annotations-version>1.5.22</swagger-annotations-version>
<spring-web-version>4.3.9.RELEASE</spring-web-version>
<spring-web-version>5.2.5.RELEASE</spring-web-version>
<jackson-version>2.10.4</jackson-version>
<jackson-databind-version>2.10.4</jackson-databind-version>
<jackson-databind-nullable-version>0.2.1</jackson-databind-nullable-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public class ApiClient {
*/
protected BodyInserter<?, ? super ClientHttpRequest> selectBody(Object obj, MultiValueMap<String, Object> formParams, MediaType contentType) {
if(MediaType.APPLICATION_FORM_URLENCODED.equals(contentType)) {
MultiValueMap<String, String> map = new LinkedMultiValueMap();
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
formParams
.toSingleValueMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ public void testRestTemplateFormMultipart() throws IOException {
TestUtils.assertFileContains(defaultApi,
//multiple files
"multipartArrayWithHttpInfo(List<File> files)",
"formParams.put(\"files\", files.stream().map(FileSystemResource::new).collect(Collectors.toList()));",
"formParams.addAll(\"files\", files.stream().map(FileSystemResource::new).collect(Collectors.toList()));",

//mixed
"multipartMixedWithHttpInfo(File file, MultipartMixedMarker marker)",
Expand Down Expand Up @@ -960,7 +960,7 @@ public void testRestTemplateWithUseAbstractionForFiles() throws IOException {
//multiple files
"multipartArray(java.util.Collection<org.springframework.core.io.Resource> files)",
"multipartArrayWithHttpInfo(java.util.Collection<org.springframework.core.io.Resource> files)",
"formParams.put(\"files\", files.stream().collect(Collectors.toList()));",
"formParams.addAll(\"files\", files.stream().collect(Collectors.toList()));",

//mixed
"multipartMixed(org.springframework.core.io.Resource file, MultipartMixedMarker marker)",
Expand Down
15 changes: 8 additions & 7 deletions samples/client/petstore/java/resttemplate-withXml/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if(hasProperty('target') && target == 'android') {

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
Expand All @@ -36,7 +36,7 @@ if(hasProperty('target') && target == 'android') {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

// Rename the aar correctly
libraryVariants.all { variant ->
variant.outputs.each { output ->
Expand All @@ -52,7 +52,7 @@ if(hasProperty('target') && target == 'android') {
provided 'javax.annotation:jsr250-api:1.0'
}
}

afterEvaluate {
android.libraryVariants.all { variant ->
def task = project.tasks.create "jar${variant.name.capitalize()}", Jar
Expand All @@ -64,12 +64,12 @@ if(hasProperty('target') && target == 'android') {
artifacts.add('archives', task);
}
}

task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}

artifacts {
archives sourcesJar
}
Expand All @@ -87,7 +87,7 @@ if(hasProperty('target') && target == 'android') {
pom.artifactId = 'petstore-resttemplate-withxml'
}
}

task execute(type:JavaExec) {
main = System.getProperty('mainClass')
classpath = sourceSets.main.runtimeClasspath
Expand All @@ -99,7 +99,7 @@ ext {
jackson_version = "2.10.4"
jackson_databind_version = "2.10.4"
jackson_databind_nullable_version = "0.2.1"
spring_web_version = "4.3.9.RELEASE"
spring_web_version = "5.2.5.RELEASE"
jodatime_version = "2.9.9"
junit_version = "4.13"
jackson_threeten_version = "2.9.10"
Expand All @@ -109,6 +109,7 @@ dependencies {
compile "io.swagger:swagger-annotations:$swagger_annotations_version"
compile "com.google.code.findbugs:jsr305:3.0.2"
compile "org.springframework:spring-web:$spring_web_version"
compile "org.springframework:spring-context:$spring_web_version"
compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_databind_version"
Expand Down
9 changes: 7 additions & 2 deletions samples/client/petstore/java/resttemplate-withXml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations-version}</version>
</dependency>

<!-- @Nullable annotation -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
Expand All @@ -217,6 +217,11 @@
<artifactId>spring-web</artifactId>
<version>${spring-web-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-web-version}</version>
</dependency>

<!-- JSON processing: jackson -->
<dependency>
Expand Down Expand Up @@ -275,7 +280,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<swagger-annotations-version>1.5.22</swagger-annotations-version>
<spring-web-version>4.3.9.RELEASE</spring-web-version>
<spring-web-version>5.2.5.RELEASE</spring-web-version>
<jackson-version>2.10.4</jackson-version>
<jackson-databind-version>2.10.4</jackson-databind-version>
<jackson-databind-nullable-version>0.2.1</jackson-databind-nullable-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.openapitools.client.model.Client;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -18,7 +17,6 @@
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -82,7 +80,7 @@ public ResponseEntity<Client> call123testSpecialTagsWithHttpInfo(Client body) th
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
final MultiValueMap<String, String> cookieParams = new LinkedMultiValueMap<String, String>();
final MultiValueMap formParams = new LinkedMultiValueMap();
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

final String[] localVarAccepts = {
"application/json"
Expand Down
Loading

0 comments on commit 9ba34b2

Please sign in to comment.