Skip to content

Commit 991fb55

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

File tree

8 files changed

+114
-8
lines changed

8 files changed

+114
-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/build.gradle.mustache

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ ext {
124124
{{#useReflectionEqualsHashCode}}
125125
commons_lang3_version = "3.17.0"
126126
{{/useReflectionEqualsHashCode}}
127+
{{#withXml}}
128+
{{#useJakartaEe}}
129+
jaxb_version = "4.0.6"
130+
{{/useJakartaEe}}
131+
{{^useJakartaEe}}
132+
jaxb_version = "2.3.9"
133+
{{/useJakartaEe}}
134+
{{/withXml}}
127135
}
128136

129137
dependencies {
@@ -142,6 +150,13 @@ dependencies {
142150
implementation "com.fasterxml.jackson.core:jackson-core:$jackson_version"
143151
implementation "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
144152
implementation "com.fasterxml.jackson.core:jackson-databind:$jackson_databind_version"
153+
{{#withXml}}
154+
implementation "org.glassfish.jersey.media:jersey-media-jaxb:$jersey_version"
155+
implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:$jackson_databind_version"
156+
if (JavaVersion.current().isJava11Compatible()) {
157+
implementation "org.glassfish.jaxb:jaxb-runtime:$jaxb_version"
158+
}
159+
{{/withXml}}
145160
{{#openApiNullable}}
146161
implementation "org.openapitools:jackson-databind-nullable:$jackson_databind_nullable_version"
147162
{{/openApiNullable}}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,23 @@
255255
</plugins>
256256
</build>
257257
</profile>
258+
{{#withXml}}
259+
<profile>
260+
<id>jaxb-runtime</id>
261+
<activation>
262+
<jdk>
263+
[11,)
264+
</jdk>
265+
</activation>
266+
<dependencies>
267+
<dependency>
268+
<groupId>org.glassfish.jaxb</groupId>
269+
<artifactId>jaxb-runtime</artifactId>
270+
<version>4.0.6</version>
271+
</dependency>
272+
</dependencies>
273+
</profile>
274+
{{/withXml}}
258275
</profiles>
259276
260277
<dependencies>
@@ -332,6 +349,13 @@
332349
<artifactId>jersey-media-jaxb</artifactId>
333350
<version>${jersey-version}</version>
334351
</dependency>
352+
<!-- Also requires Jackson XML dataformat due to @JacksonXml*-annotations -->
353+
<dependency>
354+
<groupId>com.fasterxml.jackson.dataformat</groupId>
355+
<artifactId>jackson-dataformat-xml</artifactId>
356+
<!-- Jackson's databind and dataformat libraries seem to be released synchronously -->
357+
<version>${jackson-databind-version}</version>
358+
</dependency>
335359
{{/withXml}}
336360
{{#joda}}
337361
<dependency>
@@ -411,10 +435,16 @@
411435
{{#useJakartaEe}}
412436
<jakarta-annotation-version>2.1.1</jakarta-annotation-version>
413437
<beanvalidation-version>3.0.2</beanvalidation-version>
438+
{{#withXml}}
439+
<jaxb-version>4.0.6</jaxb-version>
440+
{{/withXml}}
414441
{{/useJakartaEe}}
415442
{{^useJakartaEe}}
416443
<jakarta-annotation-version>1.3.5</jakarta-annotation-version>
417444
<beanvalidation-version>2.0.2</beanvalidation-version>
445+
{{#withXml}}
446+
<jaxb-version>2.3.9</jaxb-version>
447+
{{/withXml}}
418448
{{/useJakartaEe}}
419449
<junit-version>5.10.0</junit-version>
420450
{{#hasHttpSignatureMethods}}

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/build.gradle.mustache

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ ext {
119119
{{#useReflectionEqualsHashCode}}
120120
commons_lang3_version = "3.17.0"
121121
{{/useReflectionEqualsHashCode}}
122+
{{#withXml}}
123+
{{#useJakartaEe}}
124+
jaxb_version = "4.0.6"
125+
{{/useJakartaEe}}
126+
{{^useJakartaEe}}
127+
jaxb_version = "2.3.9"
128+
{{/useJakartaEe}}
129+
{{/withXml}}
122130
}
123131

124132
dependencies {
@@ -132,6 +140,13 @@ dependencies {
132140
implementation "com.fasterxml.jackson.core:jackson-core:$jackson_version"
133141
implementation "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
134142
implementation "com.fasterxml.jackson.core:jackson-databind:$jackson_databind_version"
143+
{{#withXml}}
144+
implementation "org.glassfish.jersey.media:jersey-media-jaxb:$jersey_version"
145+
implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:$jackson_databind_version"
146+
if (JavaVersion.current().isJava11Compatible()) {
147+
implementation "org.glassfish.jaxb:jaxb-runtime:$jaxb_version"
148+
}
149+
{{/withXml}}
135150
{{#openApiNullable}}
136151
implementation "org.openapitools:jackson-databind-nullable:$jackson_databind_nullable_version"
137152
{{/openApiNullable}}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,23 @@
255255
</plugins>
256256
</build>
257257
</profile>
258+
{{#withXml}}
259+
<profile>
260+
<id>jaxb-runtime</id>
261+
<activation>
262+
<jdk>
263+
[11,)
264+
</jdk>
265+
</activation>
266+
<dependencies>
267+
<dependency>
268+
<groupId>org.glassfish.jaxb</groupId>
269+
<artifactId>jaxb-runtime</artifactId>
270+
<version>${jaxb-version}</version>
271+
</dependency>
272+
</dependencies>
273+
</profile>
274+
{{/withXml}}
258275
</profiles>
259276
260277
<dependencies>
@@ -332,6 +349,13 @@
332349
<artifactId>jersey-media-jaxb</artifactId>
333350
<version>${jersey-version}</version>
334351
</dependency>
352+
<!-- Also requires Jackson XML dataformat due to @JacksonXml*-annotations -->
353+
<dependency>
354+
<groupId>com.fasterxml.jackson.dataformat</groupId>
355+
<artifactId>jackson-dataformat-xml</artifactId>
356+
<!-- Jackson's databind and dataformat libraries seem to be released synchronously -->
357+
<version>${jackson-databind-version}</version>
358+
</dependency>
335359
{{/withXml}}
336360
{{#joda}}
337361
<dependency>
@@ -411,10 +435,16 @@
411435
{{#useJakartaEe}}
412436
<jakarta-annotation-version>2.1.1</jakarta-annotation-version>
413437
<beanvalidation-version>3.0.2</beanvalidation-version>
438+
{{#withXml}}
439+
<jaxb-version>4.0.6</jaxb-version>
440+
{{/withXml}}
414441
{{/useJakartaEe}}
415442
{{^useJakartaEe}}
416443
<jakarta-annotation-version>1.3.5</jakarta-annotation-version>
417444
<beanvalidation-version>2.0.2</beanvalidation-version>
445+
{{#withXml}}
446+
<jaxb-version>2.3.9</jaxb-version>
447+
{{/withXml}}
418448
{{/useJakartaEe}}
419449
<junit-version>5.10.0</junit-version>
420450
{{#hasHttpSignatureMethods}}

0 commit comments

Comments
 (0)