Skip to content

Commit fb1c2f3

Browse files
[Bugfix][Java] Fixed jersey clients for multiple file upload (#19476)
* Fixed jersey3 client for multiple file upload * Updated sample * Fixed typo * Fix * Fix for jersey2
1 parent c733bb6 commit fb1c2f3

File tree

12 files changed

+396
-204
lines changed
  • modules/openapi-generator/src/main/resources/Java/libraries
  • samples
    • client
      • others/java
        • jersey2-oneOf-Mixed/src/main/java/org/openapitools/client
        • jersey2-oneOf-duplicates/src/main/java/org/openapitools/client
      • petstore/java
    • openapi3/client
      • extensions/x-auth-id-alias/java/jersey2-java8/src/main/java/org/openapitools/client
      • petstore/java
        • jersey2-java8/src/main/java/org/openapitools/client
        • jersey2-java8-special-characters/src/main/java/org/openapitools/client
        • jersey2-java8-swagger1/src/main/java/org/openapitools/client
        • jersey2-java8-swagger2/src/main/java/org/openapitools/client

12 files changed

+396
-204
lines changed

modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -988,24 +988,10 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
988988
if (contentType.startsWith("multipart/form-data")) {
989989
MultiPart multiPart = new MultiPart();
990990
for (Entry<String, Object> param: formParams.entrySet()) {
991-
if (param.getValue() instanceof File) {
992-
File file = (File) param.getValue();
993-
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
994-
.fileName(file.getName()).size(file.length()).build();
995-
996-
// Attempt to probe the content type for the file so that the form part is more correctly
997-
// and precisely identified, but fall back to application/octet-stream if that fails.
998-
MediaType type;
999-
try {
1000-
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
1001-
} catch (IOException | IllegalArgumentException e) {
1002-
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
1003-
}
1004-
1005-
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
991+
if (param.getValue() instanceof Iterable) {
992+
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
1006993
} else {
1007-
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
1008-
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
994+
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
1009995
}
1010996
}
1011997
entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
@@ -1034,6 +1020,36 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
10341020
return entity;
10351021
}
10361022

1023+
/**
1024+
* Adds the object with the provided key to the MultiPart.
1025+
* Based on the object type sets Content-Disposition and Content-Type.
1026+
*
1027+
* @param obj Object
1028+
* @param key Key of the object
1029+
* @param multiPart MultiPart to add the form param to
1030+
*/
1031+
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
1032+
if (value instanceof File) {
1033+
File file = (File) value;
1034+
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key)
1035+
.fileName(file.getName()).size(file.length()).build();
1036+
1037+
// Attempt to probe the content type for the file so that the form part is more correctly
1038+
// and precisely identified, but fall back to application/octet-stream if that fails.
1039+
MediaType type;
1040+
try {
1041+
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
1042+
} catch (IOException | IllegalArgumentException e) {
1043+
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
1044+
}
1045+
1046+
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
1047+
} else {
1048+
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key).build();
1049+
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(value)));
1050+
}
1051+
}
1052+
10371053
/**
10381054
* Serialize the given Java object into string according the given
10391055
* Content-Type (only JSON, HTTP form is supported for now).

modules/openapi-generator/src/main/resources/Java/libraries/jersey3/ApiClient.mustache

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -988,24 +988,10 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
988988
if (contentType.startsWith("multipart/form-data")) {
989989
MultiPart multiPart = new MultiPart();
990990
for (Entry<String, Object> param: formParams.entrySet()) {
991-
if (param.getValue() instanceof File) {
992-
File file = (File) param.getValue();
993-
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
994-
.fileName(file.getName()).size(file.length()).build();
995-
996-
// Attempt to probe the content type for the file so that the form part is more correctly
997-
// and precisely identified, but fall back to application/octet-stream if that fails.
998-
MediaType type;
999-
try {
1000-
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
1001-
} catch (IOException | IllegalArgumentException e) {
1002-
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
1003-
}
1004-
1005-
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
991+
if (param.getValue() instanceof Iterable) {
992+
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
1006993
} else {
1007-
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
1008-
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
994+
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
1009995
}
1010996
}
1011997
entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
@@ -1034,6 +1020,36 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
10341020
return entity;
10351021
}
10361022

1023+
/**
1024+
* Adds the object with the provided key to the MultiPart.
1025+
* Based on the object type sets Content-Disposition and Content-Type.
1026+
*
1027+
* @param obj Object
1028+
* @param key Key of the object
1029+
* @param multiPart MultiPart to add the form param to
1030+
*/
1031+
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
1032+
if (value instanceof File) {
1033+
File file = (File) value;
1034+
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key)
1035+
.fileName(file.getName()).size(file.length()).build();
1036+
1037+
// Attempt to probe the content type for the file so that the form part is more correctly
1038+
// and precisely identified, but fall back to application/octet-stream if that fails.
1039+
MediaType type;
1040+
try {
1041+
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
1042+
} catch (IOException | IllegalArgumentException e) {
1043+
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
1044+
}
1045+
1046+
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
1047+
} else {
1048+
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key).build();
1049+
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(value)));
1050+
}
1051+
}
1052+
10371053
/**
10381054
* Serialize the given Java object into string according the given
10391055
* Content-Type (only JSON, HTTP form is supported for now).

samples/client/others/java/jersey2-oneOf-Mixed/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -767,24 +767,10 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
767767
if (contentType.startsWith("multipart/form-data")) {
768768
MultiPart multiPart = new MultiPart();
769769
for (Entry<String, Object> param: formParams.entrySet()) {
770-
if (param.getValue() instanceof File) {
771-
File file = (File) param.getValue();
772-
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
773-
.fileName(file.getName()).size(file.length()).build();
774-
775-
// Attempt to probe the content type for the file so that the form part is more correctly
776-
// and precisely identified, but fall back to application/octet-stream if that fails.
777-
MediaType type;
778-
try {
779-
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
780-
} catch (IOException | IllegalArgumentException e) {
781-
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
782-
}
783-
784-
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
770+
if (param.getValue() instanceof Iterable) {
771+
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
785772
} else {
786-
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
787-
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
773+
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
788774
}
789775
}
790776
entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
@@ -813,6 +799,36 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
813799
return entity;
814800
}
815801

802+
/**
803+
* Adds the object with the provided key to the MultiPart.
804+
* Based on the object type sets Content-Disposition and Content-Type.
805+
*
806+
* @param obj Object
807+
* @param key Key of the object
808+
* @param multiPart MultiPart to add the form param to
809+
*/
810+
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
811+
if (value instanceof File) {
812+
File file = (File) value;
813+
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key)
814+
.fileName(file.getName()).size(file.length()).build();
815+
816+
// Attempt to probe the content type for the file so that the form part is more correctly
817+
// and precisely identified, but fall back to application/octet-stream if that fails.
818+
MediaType type;
819+
try {
820+
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
821+
} catch (IOException | IllegalArgumentException e) {
822+
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
823+
}
824+
825+
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
826+
} else {
827+
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key).build();
828+
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(value)));
829+
}
830+
}
831+
816832
/**
817833
* Serialize the given Java object into string according the given
818834
* Content-Type (only JSON, HTTP form is supported for now).

samples/client/others/java/jersey2-oneOf-duplicates/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -767,24 +767,10 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
767767
if (contentType.startsWith("multipart/form-data")) {
768768
MultiPart multiPart = new MultiPart();
769769
for (Entry<String, Object> param: formParams.entrySet()) {
770-
if (param.getValue() instanceof File) {
771-
File file = (File) param.getValue();
772-
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
773-
.fileName(file.getName()).size(file.length()).build();
774-
775-
// Attempt to probe the content type for the file so that the form part is more correctly
776-
// and precisely identified, but fall back to application/octet-stream if that fails.
777-
MediaType type;
778-
try {
779-
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
780-
} catch (IOException | IllegalArgumentException e) {
781-
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
782-
}
783-
784-
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
770+
if (param.getValue() instanceof Iterable) {
771+
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
785772
} else {
786-
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
787-
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
773+
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
788774
}
789775
}
790776
entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
@@ -813,6 +799,36 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
813799
return entity;
814800
}
815801

802+
/**
803+
* Adds the object with the provided key to the MultiPart.
804+
* Based on the object type sets Content-Disposition and Content-Type.
805+
*
806+
* @param obj Object
807+
* @param key Key of the object
808+
* @param multiPart MultiPart to add the form param to
809+
*/
810+
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
811+
if (value instanceof File) {
812+
File file = (File) value;
813+
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key)
814+
.fileName(file.getName()).size(file.length()).build();
815+
816+
// Attempt to probe the content type for the file so that the form part is more correctly
817+
// and precisely identified, but fall back to application/octet-stream if that fails.
818+
MediaType type;
819+
try {
820+
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
821+
} catch (IOException | IllegalArgumentException e) {
822+
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
823+
}
824+
825+
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
826+
} else {
827+
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key).build();
828+
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(value)));
829+
}
830+
}
831+
816832
/**
817833
* Serialize the given Java object into string according the given
818834
* Content-Type (only JSON, HTTP form is supported for now).

samples/client/petstore/java/jersey2-java8-localdatetime/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -908,24 +908,10 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
908908
if (contentType.startsWith("multipart/form-data")) {
909909
MultiPart multiPart = new MultiPart();
910910
for (Entry<String, Object> param: formParams.entrySet()) {
911-
if (param.getValue() instanceof File) {
912-
File file = (File) param.getValue();
913-
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
914-
.fileName(file.getName()).size(file.length()).build();
915-
916-
// Attempt to probe the content type for the file so that the form part is more correctly
917-
// and precisely identified, but fall back to application/octet-stream if that fails.
918-
MediaType type;
919-
try {
920-
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
921-
} catch (IOException | IllegalArgumentException e) {
922-
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
923-
}
924-
925-
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
911+
if (param.getValue() instanceof Iterable) {
912+
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
926913
} else {
927-
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
928-
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
914+
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
929915
}
930916
}
931917
entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
@@ -954,6 +940,36 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
954940
return entity;
955941
}
956942

943+
/**
944+
* Adds the object with the provided key to the MultiPart.
945+
* Based on the object type sets Content-Disposition and Content-Type.
946+
*
947+
* @param obj Object
948+
* @param key Key of the object
949+
* @param multiPart MultiPart to add the form param to
950+
*/
951+
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
952+
if (value instanceof File) {
953+
File file = (File) value;
954+
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key)
955+
.fileName(file.getName()).size(file.length()).build();
956+
957+
// Attempt to probe the content type for the file so that the form part is more correctly
958+
// and precisely identified, but fall back to application/octet-stream if that fails.
959+
MediaType type;
960+
try {
961+
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
962+
} catch (IOException | IllegalArgumentException e) {
963+
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
964+
}
965+
966+
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
967+
} else {
968+
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key).build();
969+
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(value)));
970+
}
971+
}
972+
957973
/**
958974
* Serialize the given Java object into string according the given
959975
* Content-Type (only JSON, HTTP form is supported for now).

samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -908,24 +908,10 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
908908
if (contentType.startsWith("multipart/form-data")) {
909909
MultiPart multiPart = new MultiPart();
910910
for (Entry<String, Object> param: formParams.entrySet()) {
911-
if (param.getValue() instanceof File) {
912-
File file = (File) param.getValue();
913-
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
914-
.fileName(file.getName()).size(file.length()).build();
915-
916-
// Attempt to probe the content type for the file so that the form part is more correctly
917-
// and precisely identified, but fall back to application/octet-stream if that fails.
918-
MediaType type;
919-
try {
920-
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
921-
} catch (IOException | IllegalArgumentException e) {
922-
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
923-
}
924-
925-
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
911+
if (param.getValue() instanceof Iterable) {
912+
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
926913
} else {
927-
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
928-
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
914+
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
929915
}
930916
}
931917
entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
@@ -954,6 +940,36 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
954940
return entity;
955941
}
956942

943+
/**
944+
* Adds the object with the provided key to the MultiPart.
945+
* Based on the object type sets Content-Disposition and Content-Type.
946+
*
947+
* @param obj Object
948+
* @param key Key of the object
949+
* @param multiPart MultiPart to add the form param to
950+
*/
951+
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
952+
if (value instanceof File) {
953+
File file = (File) value;
954+
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key)
955+
.fileName(file.getName()).size(file.length()).build();
956+
957+
// Attempt to probe the content type for the file so that the form part is more correctly
958+
// and precisely identified, but fall back to application/octet-stream if that fails.
959+
MediaType type;
960+
try {
961+
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
962+
} catch (IOException | IllegalArgumentException e) {
963+
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
964+
}
965+
966+
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
967+
} else {
968+
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key).build();
969+
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(value)));
970+
}
971+
}
972+
957973
/**
958974
* Serialize the given Java object into string according the given
959975
* Content-Type (only JSON, HTTP form is supported for now).

0 commit comments

Comments
 (0)