Skip to content

Commit

Permalink
Improve generation of README which has long description (#400)
Browse files Browse the repository at this point in the history
* Add `appDescriptionWithNewLines`

* Add test case for escapeText as well

* Ruby client allows new lines in README

* Add doc comment

* fix issue related to github web gui

* the case of no description provided

* Run `./bin/utils/ensure-up-to-date`

https://app.shippable.com/github/OpenAPITools/openapi-generator/runs/1118/1/console
  • Loading branch information
ackintosh authored and wing328 committed Jul 3, 2018
1 parent dcc0c17 commit 0137763
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public interface CodegenConfig {

String escapeText(String text);

String escapeTextWhileAllowingNewLines(String text);

String escapeUnsafeCharacters(String input);

String escapeReservedWord(String name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,31 @@ public String escapeText(String input) {
.replace("\"", "\\\""));
}

/**
* Escape characters while allowing new lines
*
* @param input String to be escaped
* @return escaped string
*/
public String escapeTextWhileAllowingNewLines(String input) {
if (input == null) {
return input;
}

// remove \t
// replace \ with \\
// replace " with \"
// outter unescape to retain the original multi-byte characters
// finally escalate characters avoiding code injection
return escapeUnsafeCharacters(
StringEscapeUtils.unescapeJava(
StringEscapeUtils.escapeJava(input)
.replace("\\/", "/"))
.replaceAll("[\\t]", " ")
.replace("\\", "\\\\")
.replace("\"", "\\\""));
}

/**
* override with any special text escaping logic to handle unsafe
* characters so as to avoid code injection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,11 @@ private void configureOpenAPIInfo() {
// set a default description if none if provided
config.additionalProperties().put("appDescription",
"No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)");
config.additionalProperties().put("appDescriptionWithNewLines", config.additionalProperties().get("appDescription"));
config.additionalProperties().put("unescapedAppDescription", "No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)");
} else {
config.additionalProperties().put("appDescription", config.escapeText(info.getDescription()));
config.additionalProperties().put("appDescriptionWithNewLines", config.escapeTextWhileAllowingNewLines(info.getDescription()));
config.additionalProperties().put("unescapedAppDescription", info.getDescription());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

{{moduleName}} - the Ruby gem for the {{appName}}

{{#appDescription}}
{{{appDescription}}}
{{/appDescription}}
{{#appDescriptionWithNewLines}}
{{{appDescriptionWithNewLines}}}
{{/appDescriptionWithNewLines}}

This SDK is automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,33 @@ public void testGetSchemaTypeWithComposedSchemaWithOneOf() {
Assert.assertNotNull(type);
}

@Test
public void testEscapeText() {
final DefaultCodegen codegen = new DefaultCodegen();

Assert.assertEquals(codegen.escapeText("\n"), " ");
Assert.assertEquals(codegen.escapeText("\r"), " ");
Assert.assertEquals(codegen.escapeText("\t"), " ");
Assert.assertEquals(codegen.escapeText("\\"), "\\\\");
Assert.assertEquals(codegen.escapeText("\""), "\\\"");
Assert.assertEquals(codegen.escapeText("\\/"), "/");
}

@Test
public void testEscapeTextWhileAllowingNewLines() {
final DefaultCodegen codegen = new DefaultCodegen();

// allow new lines
Assert.assertEquals(codegen.escapeTextWhileAllowingNewLines("\n"), "\n");
Assert.assertEquals(codegen.escapeTextWhileAllowingNewLines("\r"), "\r");

// escape other special characters
Assert.assertEquals(codegen.escapeTextWhileAllowingNewLines("\t"), " ");
Assert.assertEquals(codegen.escapeTextWhileAllowingNewLines("\\"), "\\\\");
Assert.assertEquals(codegen.escapeTextWhileAllowingNewLines("\""), "\\\"");
Assert.assertEquals(codegen.escapeTextWhileAllowingNewLines("\\/"), "/");
}

@Test
public void updateCodegenPropertyEnum() {
final DefaultCodegen codegen = new DefaultCodegen();
Expand Down

0 comments on commit 0137763

Please sign in to comment.