Skip to content

Commit 6b7c5ed

Browse files
saigiridhar21wing328
authored andcommitted
feat(java-okhttpgson): Making API response headers case insensitive (#3029)
* feat(java-okhttpgson): Making API response headers case insensitive * feat(java-okhttpgson): Adding documentation * feat(java-okhttpgson): Removing tabs * feat(java-okhttpgson): Removing tabs
1 parent b512174 commit 6b7c5ed

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed

docs/generators/java.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,5 @@ sidebar_label: java
5454
|useRuntimeException|Use RuntimeException instead of Exception| |false|
5555
|feignVersion|Version of OpenFeign: '10.x', '9.x' (default)| |false|
5656
|useReflectionEqualsHashCode|Use org.apache.commons.lang3.builder for equals and hashCode in the models. WARNING: This will fail under a security manager, unless the appropriate permissions are set up correctly and also there's potential performance impact.| |false|
57+
|caseInsensitiveResponseHeaders|Make API response's headers case-insensitive in okhttp-gson library| |false|
5758
|library|library template (sub-template) to use|<dl><dt>**jersey1**</dt><dd>HTTP client: Jersey client 1.19.x. JSON processing: Jackson 2.8.x. Enable Java6 support using '-DsupportJava6=true'. Enable gzip request encoding using '-DuseGzipFeature=true'. IMPORTANT NOTE: jersey 1.x is no longer actively maintained so please upgrade to 'jersey2' or other HTTP libaries instead.</dd><dt>**jersey2**</dt><dd>HTTP client: Jersey client 2.25.1. JSON processing: Jackson 2.8.x</dd><dt>**feign**</dt><dd>HTTP client: OpenFeign 9.x or 10.x. JSON processing: Jackson 2.8.x. To enable OpenFeign 10.x, set the 'feignVersion' option to '10.x'</dd><dt>**okhttp-gson**</dt><dd>[DEFAULT] HTTP client: OkHttp 3.x. JSON processing: Gson 2.8.x. Enable Parcelable models on Android using '-DparcelableModel=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.</dd><dt>**retrofit**</dt><dd>HTTP client: OkHttp 2.x. JSON processing: Gson 2.x (Retrofit 1.9.0). IMPORTANT NOTE: retrofit1.x is no longer actively maintained so please upgrade to 'retrofit2' instead.</dd><dt>**retrofit2**</dt><dd>HTTP client: OkHttp 3.x. JSON processing: Gson 2.x (Retrofit 2.3.0). Enable the RxJava adapter using '-DuseRxJava[2]=true'. (RxJava 1.x or 2.x)</dd><dt>**resttemplate**</dt><dd>HTTP client: Spring RestTemplate 4.x. JSON processing: Jackson 2.8.x</dd><dt>**webclient**</dt><dd>HTTP client: Spring WebClient 5.x. JSON processing: Jackson 2.9.x</dd><dt>**resteasy**</dt><dd>HTTP client: Resteasy client 3.x. JSON processing: Jackson 2.8.x</dd><dt>**vertx**</dt><dd>HTTP client: VertX client 3.x. JSON processing: Jackson 2.8.x</dd><dt>**google-api-client**</dt><dd>HTTP client: Google API client 1.x. JSON processing: Jackson 2.8.x</dd><dt>**rest-assured**</dt><dd>HTTP client: rest-assured : 3.x. JSON processing: Gson 2.x. Only for Java8</dd><dl>|okhttp-gson|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
5454
public static final String PARCELABLE_MODEL = "parcelableModel";
5555
public static final String USE_RUNTIME_EXCEPTION = "useRuntimeException";
5656
public static final String USE_REFLECTION_EQUALS_HASHCODE = "useReflectionEqualsHashCode";
57+
public static final String CASE_INSENSITIVE_RESPONSE_HEADERS = "caseInsensitiveResponseHeaders";
5758

5859
public static final String PLAY_24 = "play24";
5960
public static final String PLAY_25 = "play25";
@@ -90,6 +91,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
9091
protected boolean useGzipFeature = false;
9192
protected boolean useRuntimeException = false;
9293
protected boolean useReflectionEqualsHashCode = false;
94+
protected boolean caseInsensitiveResponseHeaders = false;
9395
protected String authFolder;
9496

9597
public JavaClientCodegen() {
@@ -122,6 +124,8 @@ public JavaClientCodegen() {
122124
cliOptions.add(CliOption.newBoolean(USE_RUNTIME_EXCEPTION, "Use RuntimeException instead of Exception"));
123125
cliOptions.add(CliOption.newBoolean(FEIGN_VERSION, "Version of OpenFeign: '10.x', '9.x' (default)"));
124126
cliOptions.add(CliOption.newBoolean(USE_REFLECTION_EQUALS_HASHCODE, "Use org.apache.commons.lang3.builder for equals and hashCode in the models. WARNING: This will fail under a security manager, unless the appropriate permissions are set up correctly and also there's potential performance impact."));
127+
cliOptions.add(CliOption.newBoolean(CASE_INSENSITIVE_RESPONSE_HEADERS, "Make API response's headers case-insensitive in " + OKHTTP_GSON + " library"));
128+
125129

126130
supportedLibraries.put(JERSEY1, "HTTP client: Jersey client 1.19.x. JSON processing: Jackson 2.8.x. Enable Java6 support using '-DsupportJava6=true'. Enable gzip request encoding using '-DuseGzipFeature=true'. IMPORTANT NOTE: jersey 1.x is no longer actively maintained so please upgrade to 'jersey2' or other HTTP libaries instead.");
127131
supportedLibraries.put(JERSEY2, "HTTP client: Jersey client 2.25.1. JSON processing: Jackson 2.8.x");
@@ -232,6 +236,10 @@ public void processOpts() {
232236
this.setUseReflectionEqualsHashCode(convertPropertyToBooleanAndWriteBack(USE_REFLECTION_EQUALS_HASHCODE));
233237
}
234238

239+
if (additionalProperties.containsKey(CASE_INSENSITIVE_RESPONSE_HEADERS)) {
240+
this.setUseReflectionEqualsHashCode(convertPropertyToBooleanAndWriteBack(CASE_INSENSITIVE_RESPONSE_HEADERS));
241+
}
242+
235243
final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");
236244
final String apiFolder = (sourceFolder + '/' + apiPackage).replace(".", "/");
237245
authFolder = (sourceFolder + '/' + invokerPackage + ".auth").replace(".", "/");
@@ -659,6 +667,10 @@ public void setUseReflectionEqualsHashCode(boolean useReflectionEqualsHashCode)
659667
this.useReflectionEqualsHashCode = useReflectionEqualsHashCode;
660668
}
661669

670+
public void setCaseInsensitiveResponseHeaders(final Boolean caseInsensitiveResponseHeaders) {
671+
this.caseInsensitiveResponseHeaders = caseInsensitiveResponseHeaders;
672+
}
673+
662674
final private static Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application\\/json(;.*)?");
663675
final private static Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application\\/vnd.(.*)+json(;.*)?");
664676

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiResponse.mustache

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ package {{invokerPackage}};
44

55
import java.util.List;
66
import java.util.Map;
7+
{{#caseInsensitiveResponseHeaders}}
8+
import java.util.Map.Entry;
9+
import java.util.TreeMap;
10+
{{/caseInsensitiveResponseHeaders}}
711

812
/**
913
* API response returned by API call.
@@ -30,7 +34,13 @@ public class ApiResponse<T> {
3034
*/
3135
public ApiResponse(int statusCode, Map<String, List<String>> headers, T data) {
3236
this.statusCode = statusCode;
33-
this.headers = headers;
37+
{{#caseInsensitiveResponseHeaders}}
38+
Map<String, List<String>> responseHeaders = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
39+
for(Entry<String, List<String>> entry : headers.entrySet()){
40+
responseHeaders.put(entry.getKey().toLowerCase(), entry.getValue());
41+
}
42+
{{/caseInsensitiveResponseHeaders}}
43+
this.headers = {{#caseInsensitiveResponseHeaders}}responseHeaders{{/caseInsensitiveResponseHeaders}}{{^caseInsensitiveResponseHeaders}}headers{{/caseInsensitiveResponseHeaders}};
3444
this.data = data;
3545
}
3646

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{{>licenseInfo}}
2+
3+
package {{invokerPackage}};
4+
5+
import java.util.Map;
6+
import java.util.List;
7+
{{#caseInsensitiveResponseHeaders}}
8+
import java.util.Map.Entry;
9+
import java.util.TreeMap;
10+
{{/caseInsensitiveResponseHeaders}}
11+
12+
{{>generatedAnnotation}}
13+
public class ApiException extends{{#useRuntimeException}} RuntimeException {{/useRuntimeException}}{{^useRuntimeException}} Exception {{/useRuntimeException}}{
14+
private int code = 0;
15+
private Map<String, List<String>> responseHeaders = null;
16+
private String responseBody = null;
17+
18+
public ApiException() {}
19+
20+
public ApiException(Throwable throwable) {
21+
super(throwable);
22+
}
23+
24+
public ApiException(String message) {
25+
super(message);
26+
}
27+
28+
public ApiException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders, String responseBody) {
29+
super(message, throwable);
30+
this.code = code;
31+
{{#caseInsensitiveResponseHeaders}}
32+
Map<String, List<String>> headers = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
33+
for(Entry<String, List<String>> entry : responseHeaders.entrySet()){
34+
headers.put(entry.getKey().toLowerCase(), entry.getValue());
35+
}
36+
{{/caseInsensitiveResponseHeaders}}
37+
this.responseHeaders = {{#caseInsensitiveResponseHeaders}}headers{{/caseInsensitiveResponseHeaders}}{{^caseInsensitiveResponseHeaders}}responseHeaders{{/caseInsensitiveResponseHeaders}};
38+
this.responseBody = responseBody;
39+
}
40+
41+
public ApiException(String message, int code, Map<String, List<String>> responseHeaders, String responseBody) {
42+
this(message, (Throwable) null, code, responseHeaders, responseBody);
43+
}
44+
45+
public ApiException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders) {
46+
this(message, throwable, code, responseHeaders, null);
47+
}
48+
49+
public ApiException(int code, Map<String, List<String>> responseHeaders, String responseBody) {
50+
this((String) null, (Throwable) null, code, responseHeaders, responseBody);
51+
}
52+
53+
public ApiException(int code, String message) {
54+
super(message);
55+
this.code = code;
56+
}
57+
58+
public ApiException(int code, String message, Map<String, List<String>> responseHeaders, String responseBody) {
59+
this(code, message);
60+
{{#caseInsensitiveResponseHeaders}}
61+
Map<String, List<String>> headers = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
62+
for(Entry<String, List<String>> entry : responseHeaders.entrySet()){
63+
headers.put(entry.getKey().toLowerCase(), entry.getValue());
64+
}
65+
{{/caseInsensitiveResponseHeaders}}
66+
this.responseHeaders = {{#caseInsensitiveResponseHeaders}}headers{{/caseInsensitiveResponseHeaders}}{{^caseInsensitiveResponseHeaders}}responseHeaders{{/caseInsensitiveResponseHeaders}};
67+
this.responseBody = responseBody;
68+
}
69+
70+
/**
71+
* Get the HTTP status code.
72+
*
73+
* @return HTTP status code
74+
*/
75+
public int getCode() {
76+
return code;
77+
}
78+
79+
/**
80+
* Get the HTTP response headers.
81+
*
82+
* @return A map of list of string
83+
*/
84+
public Map<String, List<String>> getResponseHeaders() {
85+
return responseHeaders;
86+
}
87+
88+
/**
89+
* Get the HTTP response body.
90+
*
91+
* @return Response body in the form of string
92+
*/
93+
public String getResponseBody() {
94+
return responseBody;
95+
}
96+
}

0 commit comments

Comments
 (0)