mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-07 10:58:55 +00:00
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:
parent
dcc0c17a29
commit
0137763997
@ -75,6 +75,8 @@ public interface CodegenConfig {
|
||||
|
||||
String escapeText(String text);
|
||||
|
||||
String escapeTextWhileAllowingNewLines(String text);
|
||||
|
||||
String escapeUnsafeCharacters(String input);
|
||||
|
||||
String escapeReservedWord(String name);
|
||||
|
@ -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
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
@ -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:
|
||||
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user