Skip to content

Commit cccdfd2

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

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

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/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
/**

0 commit comments

Comments
 (0)