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

Configuration option to disable HTML escaping when using Gson #298

Merged
merged 2 commits into from
Jun 14, 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 @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you forget to put the {{#disableHtmlEscaping}} ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I did not forget, I chose to always declare the variable and conditionally set disableHtmlEscaping:

{{#disableHtmlEscaping}}
builder.disableHtmlEscaping();
{{/disableHtmlEscaping}}

This keeps the complexity of the mustache code low, and makes it easy to accommodate more options in the future. For example:

{{#disableHtmlEscaping}}
builder.disableHtmlEscaping();
{{/disableHtmlEscaping}}
{{#someNewOption}}
builder.someNewOption();
{{/someNewOption}}

I can modify the template to handle the "no options" situation to avoid declaring the variable in generated Java-classes, if that is preferable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's my bad, since the path to the file was truncated by github UI, I did not realize it was a generated sample. Makes total sense! Keep it that way :)

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all good

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all good

return builder;
}

private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {
Expand Down