Skip to content

Commit 03ed433

Browse files
authored
Merge pull request #7966 from stianlik/gson-disableHtmlEscaping
[Java] Configuration option to disable HTML escaping when using Gson
2 parents a6f9e98 + feb8b16 commit 03ed433

File tree

10 files changed

+395
-3
lines changed

10 files changed

+395
-3
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
5454
public static final String SUPPORT_ASYNC = "supportAsync";
5555
public static final String WITH_XML = "withXml";
5656
public static final String SUPPORT_JAVA6 = "supportJava6";
57+
public static final String DISABLE_HTML_ESCAPING = "disableHtmlEscaping";
5758

5859
protected String dateLibrary = "threetenbp";
5960
protected boolean supportAsync = false;
@@ -86,6 +87,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
8687
protected String apiDocPath = "docs/";
8788
protected String modelDocPath = "docs/";
8889
protected boolean supportJava6= false;
90+
protected boolean disableHtmlEscaping = false;
8991

9092
public AbstractJavaCodegen() {
9193
super();
@@ -176,6 +178,8 @@ public AbstractJavaCodegen() {
176178
java8ModeOptions.put("false", "Various third party libraries as needed");
177179
java8Mode.setEnum(java8ModeOptions);
178180
cliOptions.add(java8Mode);
181+
182+
cliOptions.add(CliOption.newBoolean(DISABLE_HTML_ESCAPING, "Disable HTML escaping of JSON strings when using gson (needed to avoid problems with byte[] fields)"));
179183
}
180184

181185
@Override
@@ -187,6 +191,10 @@ public void processOpts() {
187191
}
188192
additionalProperties.put(SUPPORT_JAVA6, supportJava6);
189193

194+
if (additionalProperties.containsKey(DISABLE_HTML_ESCAPING)) {
195+
this.setDisableHtmlEscaping(Boolean.valueOf(additionalProperties.get(DISABLE_HTML_ESCAPING).toString()));
196+
}
197+
additionalProperties.put(DISABLE_HTML_ESCAPING, disableHtmlEscaping);
190198

191199
if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
192200
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
@@ -1238,6 +1246,10 @@ public void setJava8Mode(boolean enabled) {
12381246
this.java8Mode = enabled;
12391247
}
12401248

1249+
public void setDisableHtmlEscaping(boolean disabled) {
1250+
this.disableHtmlEscaping = disabled;
1251+
}
1252+
12411253
public void setSupportAsync(boolean enabled) {
12421254
this.supportAsync = enabled;
12431255
}

modules/swagger-codegen/src/main/resources/Java/JSON.mustache

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ public class JSON {
7878
})
7979
{{/parent}}
8080
;
81-
return fireBuilder.createGsonBuilder();
81+
GsonBuilder builder = fireBuilder.createGsonBuilder();
82+
{{#disableHtmlEscaping}}
83+
builder.disableHtmlEscaping();
84+
{{/disableHtmlEscaping}}
85+
return builder;
8286
}
8387

8488
private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {

modules/swagger-codegen/src/test/java/io/swagger/codegen/options/JavaOptionsProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public JavaOptionsProvider() {
7171
.put(JavaClientCodegen.JAVA8_MODE, JAVA8_MODE_VALUE)
7272
.put(CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING, "true")
7373
.put(JavaClientCodegen.DATE_LIBRARY, "joda")
74+
.put(JavaClientCodegen.DISABLE_HTML_ESCAPING, "false")
7475
.put("hideGenerationTimestamp", "true")
7576
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
7677
//.put("supportJava6", "true")

modules/swagger-codegen/src/test/java/io/swagger/codegen/options/JaxRSServerOptionsProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public Map<String, String> createOptions() {
8989
.put(JavaClientCodegen.WITH_XML, WITH_XML_VALUE)
9090
//.put(JavaClientCodegen.DATE_LIBRARY, "joda")
9191
.put("hideGenerationTimestamp", "true")
92+
.put(JavaClientCodegen.DISABLE_HTML_ESCAPING, "false")
9293
.put(JavaCXFServerCodegen.USE_BEANVALIDATION, USE_BEANVALIDATION)
9394
.put("serverPort", "2345")
9495
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# DefaultApi
2+
3+
All URIs are relative to *http://petstore.swagger.io:80/v2*
4+
5+
Method | HTTP request | Description
6+
------------- | ------------- | -------------
7+
[**testBodyWithQueryParams**](DefaultApi.md#testBodyWithQueryParams) | **PUT** /fake/body-with-query-params |
8+
9+
10+
<a name="testBodyWithQueryParams"></a>
11+
# **testBodyWithQueryParams**
12+
> testBodyWithQueryParams(body, query)
13+
14+
15+
16+
### Example
17+
```java
18+
// Import classes:
19+
//import io.swagger.client.ApiException;
20+
//import io.swagger.client.api.DefaultApi;
21+
22+
23+
DefaultApi apiInstance = new DefaultApi();
24+
User body = new User(); // User |
25+
String query = "query_example"; // String |
26+
try {
27+
apiInstance.testBodyWithQueryParams(body, query);
28+
} catch (ApiException e) {
29+
System.err.println("Exception when calling DefaultApi#testBodyWithQueryParams");
30+
e.printStackTrace();
31+
}
32+
```
33+
34+
### Parameters
35+
36+
Name | Type | Description | Notes
37+
------------- | ------------- | ------------- | -------------
38+
**body** | [**User**](User.md)| |
39+
**query** | **String**| |
40+
41+
### Return type
42+
43+
null (empty response body)
44+
45+
### Authorization
46+
47+
No authorization required
48+
49+
### HTTP request headers
50+
51+
- **Content-Type**: application/json
52+
- **Accept**: Not defined
53+

samples/client/petstore/java/okhttp-gson/docs/EnumTest.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Name | Type | Description | Notes
66
------------ | ------------- | ------------- | -------------
77
**enumString** | [**EnumStringEnum**](#EnumStringEnum) | | [optional]
8+
**enumStringRequired** | [**EnumStringRequiredEnum**](#EnumStringRequiredEnum) | |
89
**enumInteger** | [**EnumIntegerEnum**](#EnumIntegerEnum) | | [optional]
910
**enumNumber** | [**EnumNumberEnum**](#EnumNumberEnum) | | [optional]
1011
**outerEnum** | [**OuterEnum**](OuterEnum.md) | | [optional]
@@ -19,6 +20,15 @@ LOWER | &quot;lower&quot;
1920
EMPTY | &quot;&quot;
2021

2122

23+
<a name="EnumStringRequiredEnum"></a>
24+
## Enum: EnumStringRequiredEnum
25+
Name | Value
26+
---- | -----
27+
UPPER | &quot;UPPER&quot;
28+
LOWER | &quot;lower&quot;
29+
EMPTY | &quot;&quot;
30+
31+
2232
<a name="EnumIntegerEnum"></a>
2333
## Enum: EnumIntegerEnum
2434
Name | Value

samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/JSON.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public Class getClassForElement(JsonElement readElement) {
6464
}
6565
})
6666
;
67-
return fireBuilder.createGsonBuilder();
67+
GsonBuilder builder = fireBuilder.createGsonBuilder();
68+
return builder;
6869
}
6970

7071
private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Swagger Petstore
3+
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
4+
*
5+
* OpenAPI spec version: 1.0.0
6+
* Contact: [email protected]
7+
*
8+
* NOTE: This class is auto generated by the swagger code generator program.
9+
* https://github.com/swagger-api/swagger-codegen.git
10+
* Do not edit the class manually.
11+
*/
12+
13+
14+
package io.swagger.client.api;
15+
16+
import io.swagger.client.ApiCallback;
17+
import io.swagger.client.ApiClient;
18+
import io.swagger.client.ApiException;
19+
import io.swagger.client.ApiResponse;
20+
import io.swagger.client.Configuration;
21+
import io.swagger.client.Pair;
22+
import io.swagger.client.ProgressRequestBody;
23+
import io.swagger.client.ProgressResponseBody;
24+
25+
import com.google.gson.reflect.TypeToken;
26+
27+
import java.io.IOException;
28+
29+
30+
import io.swagger.client.model.User;
31+
32+
import java.lang.reflect.Type;
33+
import java.util.ArrayList;
34+
import java.util.HashMap;
35+
import java.util.List;
36+
import java.util.Map;
37+
38+
public class DefaultApi {
39+
private ApiClient apiClient;
40+
41+
public DefaultApi() {
42+
this(Configuration.getDefaultApiClient());
43+
}
44+
45+
public DefaultApi(ApiClient apiClient) {
46+
this.apiClient = apiClient;
47+
}
48+
49+
public ApiClient getApiClient() {
50+
return apiClient;
51+
}
52+
53+
public void setApiClient(ApiClient apiClient) {
54+
this.apiClient = apiClient;
55+
}
56+
57+
/**
58+
* Build call for testBodyWithQueryParams
59+
* @param body (required)
60+
* @param query (required)
61+
* @param progressListener Progress listener
62+
* @param progressRequestListener Progress request listener
63+
* @return Call to execute
64+
* @throws ApiException If fail to serialize the request body object
65+
*/
66+
public com.squareup.okhttp.Call testBodyWithQueryParamsCall(User body, String query, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
67+
Object localVarPostBody = body;
68+
69+
// create path and map variables
70+
String localVarPath = "/fake/body-with-query-params";
71+
72+
List<Pair> localVarQueryParams = new ArrayList<Pair>();
73+
List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
74+
if (query != null)
75+
localVarQueryParams.addAll(apiClient.parameterToPair("query", query));
76+
77+
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
78+
79+
Map<String, Object> localVarFormParams = new HashMap<String, Object>();
80+
81+
final String[] localVarAccepts = {
82+
83+
};
84+
final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
85+
if (localVarAccept != null) localVarHeaderParams.put("Accept", localVarAccept);
86+
87+
final String[] localVarContentTypes = {
88+
"application/json"
89+
};
90+
final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
91+
localVarHeaderParams.put("Content-Type", localVarContentType);
92+
93+
if(progressListener != null) {
94+
apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
95+
@Override
96+
public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain) throws IOException {
97+
com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
98+
return originalResponse.newBuilder()
99+
.body(new ProgressResponseBody(originalResponse.body(), progressListener))
100+
.build();
101+
}
102+
});
103+
}
104+
105+
String[] localVarAuthNames = new String[] { };
106+
return apiClient.buildCall(localVarPath, "PUT", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarAuthNames, progressRequestListener);
107+
}
108+
109+
@SuppressWarnings("rawtypes")
110+
private com.squareup.okhttp.Call testBodyWithQueryParamsValidateBeforeCall(User body, String query, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
111+
112+
// verify the required parameter 'body' is set
113+
if (body == null) {
114+
throw new ApiException("Missing the required parameter 'body' when calling testBodyWithQueryParams(Async)");
115+
}
116+
117+
// verify the required parameter 'query' is set
118+
if (query == null) {
119+
throw new ApiException("Missing the required parameter 'query' when calling testBodyWithQueryParams(Async)");
120+
}
121+
122+
123+
com.squareup.okhttp.Call call = testBodyWithQueryParamsCall(body, query, progressListener, progressRequestListener);
124+
return call;
125+
126+
}
127+
128+
/**
129+
*
130+
*
131+
* @param body (required)
132+
* @param query (required)
133+
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
134+
*/
135+
public void testBodyWithQueryParams(User body, String query) throws ApiException {
136+
testBodyWithQueryParamsWithHttpInfo(body, query);
137+
}
138+
139+
/**
140+
*
141+
*
142+
* @param body (required)
143+
* @param query (required)
144+
* @return ApiResponse&lt;Void&gt;
145+
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
146+
*/
147+
public ApiResponse<Void> testBodyWithQueryParamsWithHttpInfo(User body, String query) throws ApiException {
148+
com.squareup.okhttp.Call call = testBodyWithQueryParamsValidateBeforeCall(body, query, null, null);
149+
return apiClient.execute(call);
150+
}
151+
152+
/**
153+
* (asynchronously)
154+
*
155+
* @param body (required)
156+
* @param query (required)
157+
* @param callback The callback to be executed when the API call finishes
158+
* @return The request call
159+
* @throws ApiException If fail to process the API call, e.g. serializing the request body object
160+
*/
161+
public com.squareup.okhttp.Call testBodyWithQueryParamsAsync(User body, String query, final ApiCallback<Void> callback) throws ApiException {
162+
163+
ProgressResponseBody.ProgressListener progressListener = null;
164+
ProgressRequestBody.ProgressRequestListener progressRequestListener = null;
165+
166+
if (callback != null) {
167+
progressListener = new ProgressResponseBody.ProgressListener() {
168+
@Override
169+
public void update(long bytesRead, long contentLength, boolean done) {
170+
callback.onDownloadProgress(bytesRead, contentLength, done);
171+
}
172+
};
173+
174+
progressRequestListener = new ProgressRequestBody.ProgressRequestListener() {
175+
@Override
176+
public void onRequestProgress(long bytesWritten, long contentLength, boolean done) {
177+
callback.onUploadProgress(bytesWritten, contentLength, done);
178+
}
179+
};
180+
}
181+
182+
com.squareup.okhttp.Call call = testBodyWithQueryParamsValidateBeforeCall(body, query, progressListener, progressRequestListener);
183+
apiClient.executeAsync(call, callback);
184+
return call;
185+
}
186+
}

0 commit comments

Comments
 (0)