From 013776399764e8c1958af0748931785bfc4bd83c Mon Sep 17 00:00:00 2001 From: Akihito Nakano Date: Tue, 3 Jul 2018 22:25:27 +0900 Subject: [PATCH] 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 --- .../openapitools/codegen/CodegenConfig.java | 2 ++ .../openapitools/codegen/DefaultCodegen.java | 25 +++++++++++++++++ .../codegen/DefaultGenerator.java | 2 ++ .../src/main/resources/ruby/README.mustache | 6 ++--- .../codegen/DefaultCodegenTest.java | 27 +++++++++++++++++++ 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConfig.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConfig.java index fef5d1f510a..e8bb770fba6 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConfig.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConfig.java @@ -75,6 +75,8 @@ public interface CodegenConfig { String escapeText(String text); + String escapeTextWhileAllowingNewLines(String text); + String escapeUnsafeCharacters(String input); String escapeReservedWord(String name); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index c4ef8f114af..94183f154c5 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -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 diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java index 361f9c41cb2..4ddf0d86649 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java @@ -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()); } diff --git a/modules/openapi-generator/src/main/resources/ruby/README.mustache b/modules/openapi-generator/src/main/resources/ruby/README.mustache index 42e0f170ccf..8703af147ad 100644 --- a/modules/openapi-generator/src/main/resources/ruby/README.mustache +++ b/modules/openapi-generator/src/main/resources/ruby/README.mustache @@ -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: diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index 65948656314..7c866dab664 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -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();