Skip to content

Commit a7af1c5

Browse files
committed
Jersey2/3: Allow multiple Accept-Header entries
1 parent ecaf3ea commit a7af1c5

File tree

6 files changed

+38
-8
lines changed

6 files changed

+38
-8
lines changed

bin/configs/java-jersey2-8.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ library: jersey2
44
inputSpec: modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml
55
templateDir: modules/openapi-generator/src/main/resources/Java
66
additionalProperties:
7+
withXml: true
78
artifactId: petstore-openapi3-jersey2-java8
89
hideGenerationTimestamp: true
910
serverPort: "8082"

bin/configs/java-jersey3.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ library: jersey3
44
inputSpec: modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml
55
templateDir: modules/openapi-generator/src/main/resources/Java
66
additionalProperties:
7+
withXml: true
78
useBeanValidation: true
89
artifactId: petstore-jersey3
910
hideGenerationTimestamp: true

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import java.util.HashMap;
5151
import java.util.LinkedHashMap;
5252
import java.util.LinkedHashSet;
5353
import java.util.List;
54+
import java.util.Set;
5455
import java.util.Arrays;
5556
import java.util.ArrayList;
5657
import java.util.Date;
@@ -922,8 +923,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
922923

923924
/**
924925
* Select the Accept header's value from the given accepts array:
925-
* if JSON exists in the given array, use it;
926-
* otherwise use all of them (joining into a string)
926+
* if JSON exists in the given array, makes sure there is only one entry of it.
927927
*
928928
* @param accepts The accepts array to select from
929929
* @return The Accept header to use. If the given array is empty,
@@ -933,12 +933,19 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
933933
if (accepts == null || accepts.length == 0) {
934934
return null;
935935
}
936+
Set<String> acceptHeaders = new LinkedHashSet<>();
937+
boolean foundJsonMime = false;
936938
for (String accept : accepts) {
937939
if (isJsonMime(accept)) {
938-
return accept;
940+
if (foundJsonMime) {
941+
continue;
942+
} else {
943+
foundJsonMime = true;
944+
}
939945
}
946+
acceptHeaders.add(accept);
940947
}
941-
return StringUtil.join(accepts, ",");
948+
return StringUtil.join(acceptHeaders, ",");
942949
}
943950

944951
/**

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,13 @@
332332
<artifactId>jersey-media-jaxb</artifactId>
333333
<version>${jersey-version}</version>
334334
</dependency>
335+
<!-- Also requires Jackson XML dataformat due to @JacksonXml*-annotations -->
336+
<dependency>
337+
<groupId>com.fasterxml.jackson.dataformat</groupId>
338+
<artifactId>jackson-dataformat-xml</artifactId>
339+
<!-- Jackson's databind and dataformat libraries seem to be released synchronously -->
340+
<version>${jackson-databind-version}</version>
341+
</dependency>
335342
{{/withXml}}
336343
{{#joda}}
337344
<dependency>

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import java.util.HashMap;
5151
import java.util.LinkedHashMap;
5252
import java.util.LinkedHashSet;
5353
import java.util.List;
54+
import java.util.Set;
5455
import java.util.Arrays;
5556
import java.util.ArrayList;
5657
import java.util.Date;
@@ -922,8 +923,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
922923

923924
/**
924925
* Select the Accept header's value from the given accepts array:
925-
* if JSON exists in the given array, use it;
926-
* otherwise use all of them (joining into a string)
926+
* if JSON exists in the given array, makes sure there is only one entry of it.
927927
*
928928
* @param accepts The accepts array to select from
929929
* @return The Accept header to use. If the given array is empty,
@@ -933,12 +933,19 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
933933
if (accepts == null || accepts.length == 0) {
934934
return null;
935935
}
936+
Set<String> acceptHeaders = new LinkedHashSet<>();
937+
boolean foundJsonMime = false;
936938
for (String accept : accepts) {
937939
if (isJsonMime(accept)) {
938-
return accept;
940+
if (foundJsonMime) {
941+
continue;
942+
} else {
943+
foundJsonMime = true;
944+
}
939945
}
946+
acceptHeaders.add(accept);
940947
}
941-
return StringUtil.join(accepts, ",");
948+
return StringUtil.join(acceptHeaders, ",");
942949
}
943950

944951
/**

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,13 @@
332332
<artifactId>jersey-media-jaxb</artifactId>
333333
<version>${jersey-version}</version>
334334
</dependency>
335+
<!-- Also requires Jackson XML dataformat due to @JacksonXml*-annotations -->
336+
<dependency>
337+
<groupId>com.fasterxml.jackson.dataformat</groupId>
338+
<artifactId>jackson-dataformat-xml</artifactId>
339+
<!-- Jackson's databind and dataformat libraries seem to be released synchronously -->
340+
<version>${jackson-databind-version}</version>
341+
</dependency>
335342
{{/withXml}}
336343
{{#joda}}
337344
<dependency>

0 commit comments

Comments
 (0)