Skip to content

Commit

Permalink
Configuration option to disable HTML escaping when using Gson (#298)
Browse files Browse the repository at this point in the history
* Configuration option to disable HTML escaping when using Gson

The default implementation of Gson will escape certain characters by default. This
includes the `=` character, which is used in base64 encoding and cause problems when
deserializing the value to a base64 encoded string in a service.

Adding an option for disabling this feature makes it easier to generate client code
with sane defaults.

* Update Petstore sample
  • Loading branch information
stianlik authored and wing328 committed Jun 14, 2018
1 parent f8e61ac commit 680a2bc
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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 @@ -93,7 +94,8 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
protected boolean serializeBigDecimalAsString = false;
protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";
protected boolean supportJava6 = false;
protected boolean supportJava6= false;
protected boolean disableHtmlEscaping = false;

public AbstractJavaCodegen() {
super();
Expand Down Expand Up @@ -185,6 +187,7 @@ public AbstractJavaCodegen() {
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 @@ -196,6 +199,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 @@ -1222,6 +1229,10 @@ public void setSupportAsync(boolean enabled) {
this.supportAsync = enabled;
}

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

@Override
public String escapeQuotationMark(String input) {
// remove " to avoid code injection
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 @@ -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
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
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

0 comments on commit 680a2bc

Please sign in to comment.