Improve generation of README which has long description (#400)

* 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
This commit is contained in:
Akihito Nakano 2018-07-03 22:25:27 +09:00 committed by William Cheng
parent dcc0c17a29
commit 0137763997
5 changed files with 59 additions and 3 deletions

View File

@ -75,6 +75,8 @@ public interface CodegenConfig {
String escapeText(String text);
String escapeTextWhileAllowingNewLines(String text);
String escapeUnsafeCharacters(String input);
String escapeReservedWord(String name);

View File

@ -414,6 +414,31 @@ public class DefaultCodegen implements CodegenConfig {
.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

View File

@ -216,9 +216,11 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
// 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());
}

View File

@ -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:

View File

@ -235,6 +235,33 @@ public class DefaultCodegenTest {
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();