Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] Configuration option to disable HTML escaping when using Gson #7966

Merged
merged 3 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
public static final String SUPPORT_ASYNC = "supportAsync";
public static final String WITH_XML = "withXml";
public static final String SUPPORT_JAVA6 = "supportJava6";
public static final String DISABLE_HTML_ESCAPING = "disableHtmlEscaping";

protected String dateLibrary = "threetenbp";
protected boolean supportAsync = false;
Expand Down Expand Up @@ -87,6 +88,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";
protected boolean supportJava6= false;
protected boolean disableHtmlEscaping = false;

public AbstractJavaCodegen() {
super();
Expand Down Expand Up @@ -175,6 +177,8 @@ public AbstractJavaCodegen() {
java8ModeOptions.put("false", "Various third party libraries as needed");
java8Mode.setEnum(java8ModeOptions);
cliOptions.add(java8Mode);

cliOptions.add(CliOption.newBoolean(DISABLE_HTML_ESCAPING, "Disable HTML escaping of JSON strings when using gson (needed to avoid problems with byte[] fields)"));
}

@Override
Expand All @@ -186,6 +190,10 @@ public void processOpts() {
}
additionalProperties.put(SUPPORT_JAVA6, supportJava6);

if (additionalProperties.containsKey(DISABLE_HTML_ESCAPING)) {
this.setDisableHtmlEscaping(Boolean.valueOf(additionalProperties.get(DISABLE_HTML_ESCAPING).toString()));
}
additionalProperties.put(DISABLE_HTML_ESCAPING, disableHtmlEscaping);

if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
Expand Down Expand Up @@ -1225,6 +1233,10 @@ public void setJava8Mode(boolean enabled) {
this.java8Mode = enabled;
}

public void setDisableHtmlEscaping(boolean disabled) {
this.disableHtmlEscaping = disabled;
}

public void setSupportAsync(boolean enabled) {
this.supportAsync = enabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public class JSON {
})
{{/parent}}
;
return fireBuilder.createGsonBuilder();
GsonBuilder builder = fireBuilder.createGsonBuilder();
{{#disableHtmlEscaping}}
builder.disableHtmlEscaping();
{{/disableHtmlEscaping}}
return builder;
}

private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public JavaOptionsProvider() {
.put(JavaClientCodegen.JAVA8_MODE, JAVA8_MODE_VALUE)
.put(CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING, "true")
.put(JavaClientCodegen.DATE_LIBRARY, "joda")
.put(JavaClientCodegen.DISABLE_HTML_ESCAPING, "false")
.put("hideGenerationTimestamp", "true")
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
//.put("supportJava6", "true")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public Map<String, String> createOptions() {
.put(JavaClientCodegen.WITH_XML, WITH_XML_VALUE)
//.put(JavaClientCodegen.DATE_LIBRARY, "joda")
.put("hideGenerationTimestamp", "true")
.put(JavaClientCodegen.DISABLE_HTML_ESCAPING, "false")
.put(JavaCXFServerCodegen.USE_BEANVALIDATION, USE_BEANVALIDATION)
.put("serverPort", "2345")
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
Expand Down
53 changes: 53 additions & 0 deletions samples/client/petstore/java/okhttp-gson/docs/DefaultApi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# DefaultApi

All URIs are relative to *http://petstore.swagger.io:80/v2*

Method | HTTP request | Description
------------- | ------------- | -------------
[**testBodyWithQueryParams**](DefaultApi.md#testBodyWithQueryParams) | **PUT** /fake/body-with-query-params |


<a name="testBodyWithQueryParams"></a>
# **testBodyWithQueryParams**
> testBodyWithQueryParams(body, query)



### Example
```java
// Import classes:
//import io.swagger.client.ApiException;
//import io.swagger.client.api.DefaultApi;


DefaultApi apiInstance = new DefaultApi();
User body = new User(); // User |
String query = "query_example"; // String |
try {
apiInstance.testBodyWithQueryParams(body, query);
} catch (ApiException e) {
System.err.println("Exception when calling DefaultApi#testBodyWithQueryParams");
e.printStackTrace();
}
```

### Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**body** | [**User**](User.md)| |
**query** | **String**| |

### Return type

null (empty response body)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: application/json
- **Accept**: Not defined

10 changes: 10 additions & 0 deletions samples/client/petstore/java/okhttp-gson/docs/EnumTest.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**enumString** | [**EnumStringEnum**](#EnumStringEnum) | | [optional]
**enumStringRequired** | [**EnumStringRequiredEnum**](#EnumStringRequiredEnum) | |
**enumInteger** | [**EnumIntegerEnum**](#EnumIntegerEnum) | | [optional]
**enumNumber** | [**EnumNumberEnum**](#EnumNumberEnum) | | [optional]
**outerEnum** | [**OuterEnum**](OuterEnum.md) | | [optional]
Expand All @@ -19,6 +20,15 @@ LOWER | &quot;lower&quot;
EMPTY | &quot;&quot;


<a name="EnumStringRequiredEnum"></a>
## Enum: EnumStringRequiredEnum
Name | Value
---- | -----
UPPER | &quot;UPPER&quot;
LOWER | &quot;lower&quot;
EMPTY | &quot;&quot;


<a name="EnumIntegerEnum"></a>
## Enum: EnumIntegerEnum
Name | Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public Class getClassForElement(JsonElement readElement) {
}
})
;
return fireBuilder.createGsonBuilder();
GsonBuilder builder = fireBuilder.createGsonBuilder();
return builder;
}

private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Swagger Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* OpenAPI spec version: 1.0.0
* Contact: apiteam@swagger.io
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/


package io.swagger.client.api;

import io.swagger.client.ApiCallback;
import io.swagger.client.ApiClient;
import io.swagger.client.ApiException;
import io.swagger.client.ApiResponse;
import io.swagger.client.Configuration;
import io.swagger.client.Pair;
import io.swagger.client.ProgressRequestBody;
import io.swagger.client.ProgressResponseBody;

import com.google.gson.reflect.TypeToken;

import java.io.IOException;


import io.swagger.client.model.User;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DefaultApi {
private ApiClient apiClient;

public DefaultApi() {
this(Configuration.getDefaultApiClient());
}

public DefaultApi(ApiClient apiClient) {
this.apiClient = apiClient;
}

public ApiClient getApiClient() {
return apiClient;
}

public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}

/**
* Build call for testBodyWithQueryParams
* @param body (required)
* @param query (required)
* @param progressListener Progress listener
* @param progressRequestListener Progress request listener
* @return Call to execute
* @throws ApiException If fail to serialize the request body object
*/
public com.squareup.okhttp.Call testBodyWithQueryParamsCall(User body, String query, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
Object localVarPostBody = body;

// create path and map variables
String localVarPath = "/fake/body-with-query-params";

List<Pair> localVarQueryParams = new ArrayList<Pair>();
List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
if (query != null)
localVarQueryParams.addAll(apiClient.parameterToPair("query", query));

Map<String, String> localVarHeaderParams = new HashMap<String, String>();

Map<String, Object> localVarFormParams = new HashMap<String, Object>();

final String[] localVarAccepts = {

};
final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
if (localVarAccept != null) localVarHeaderParams.put("Accept", localVarAccept);

final String[] localVarContentTypes = {
"application/json"
};
final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
localVarHeaderParams.put("Content-Type", localVarContentType);

if(progressListener != null) {
apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
@Override
public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain) throws IOException {
com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.body(new ProgressResponseBody(originalResponse.body(), progressListener))
.build();
}
});
}

String[] localVarAuthNames = new String[] { };
return apiClient.buildCall(localVarPath, "PUT", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarAuthNames, progressRequestListener);
}

@SuppressWarnings("rawtypes")
private com.squareup.okhttp.Call testBodyWithQueryParamsValidateBeforeCall(User body, String query, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {

// verify the required parameter 'body' is set
if (body == null) {
throw new ApiException("Missing the required parameter 'body' when calling testBodyWithQueryParams(Async)");
}

// verify the required parameter 'query' is set
if (query == null) {
throw new ApiException("Missing the required parameter 'query' when calling testBodyWithQueryParams(Async)");
}


com.squareup.okhttp.Call call = testBodyWithQueryParamsCall(body, query, progressListener, progressRequestListener);
return call;

}

/**
*
*
* @param body (required)
* @param query (required)
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
*/
public void testBodyWithQueryParams(User body, String query) throws ApiException {
testBodyWithQueryParamsWithHttpInfo(body, query);
}

/**
*
*
* @param body (required)
* @param query (required)
* @return ApiResponse&lt;Void&gt;
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
*/
public ApiResponse<Void> testBodyWithQueryParamsWithHttpInfo(User body, String query) throws ApiException {
com.squareup.okhttp.Call call = testBodyWithQueryParamsValidateBeforeCall(body, query, null, null);
return apiClient.execute(call);
}

/**
* (asynchronously)
*
* @param body (required)
* @param query (required)
* @param callback The callback to be executed when the API call finishes
* @return The request call
* @throws ApiException If fail to process the API call, e.g. serializing the request body object
*/
public com.squareup.okhttp.Call testBodyWithQueryParamsAsync(User body, String query, final ApiCallback<Void> callback) throws ApiException {

ProgressResponseBody.ProgressListener progressListener = null;
ProgressRequestBody.ProgressRequestListener progressRequestListener = null;

if (callback != null) {
progressListener = new ProgressResponseBody.ProgressListener() {
@Override
public void update(long bytesRead, long contentLength, boolean done) {
callback.onDownloadProgress(bytesRead, contentLength, done);
}
};

progressRequestListener = new ProgressRequestBody.ProgressRequestListener() {
@Override
public void onRequestProgress(long bytesWritten, long contentLength, boolean done) {
callback.onUploadProgress(bytesWritten, contentLength, done);
}
};
}

com.squareup.okhttp.Call call = testBodyWithQueryParamsValidateBeforeCall(body, query, progressListener, progressRequestListener);
apiClient.executeAsync(call, callback);
return call;
}
}
Loading