-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
[cpp-restsdk] Support multi-line descriptions #753
[cpp-restsdk] Support multi-line descriptions #753
Conversation
This will currently dump 1:1 text in the new spec format:
Other generators could potentially be broken by a change in DefaultCodegen as well, although I can't think of any others off the top of my head. Even code generators may output different templates where such an escape may not be ideal. For instance, if a generator outputs openapi.yaml for version 3.0.1 (as above) alongside code. Or if a non-HTML based README is generated, if descriptions are expected to be unencoded and HTML doc outputs re-encode (the change here might result in a double-encoded entity). A better place for escaping textual data for templates is in the template itself. I think a lambda would be best suited for this, especially because the issue doesn't necessarily seem to be with HTML escaping, but that the template isn't wrapping the quote characters with the new line. That is, calling I've created numerous mustache lambdas. You could follow the pattern of the AbstractCSharpCodegen to implement something similarly. Example:
And in
This would require a modification to IndentedLambda: diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/mustache/IndentedLambda.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/mustache/IndentedLambda.java
index d1b0b3826..3db3e10f9 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/mustache/IndentedLambda.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/mustache/IndentedLambda.java
@@ -43,13 +43,14 @@ import java.io.Writer;
*/
public class IndentedLambda implements Mustache.Lambda {
private final int prefixSpaceCount;
+ private final String prefix;
private int spaceCode;
/**
* Constructs a new instance of {@link IndentedLambda}, with an indent count of 4 spaces
*/
public IndentedLambda() {
- this(4, " ");
+ this(4, " ", null);
}
/**
@@ -57,17 +58,20 @@ public class IndentedLambda implements Mustache.Lambda {
*
* @param prefixSpaceCount The number of indented characters to apply as a prefix to a fragment.
* @param indentionCharacter String representation of the character used in the indent (e.g. " ", "\t", ".").
+ * @param prefix An optional prefix to prepend before the line (useful for multi-line comments.
*/
- public IndentedLambda(int prefixSpaceCount, String indentionCharacter) {
- this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0));
+ public IndentedLambda(int prefixSpaceCount, String indentionCharacter, String prefix) {
+ this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), prefix);
}
/**
* Constructs a new instance of {@link IndentedLambda}
*
* @param prefixSpaceCount The number of indented characters to apply as a prefix to a fragment.
+ * @param indentionCodePoint Code point of the single character used for indentation.
+ * @param prefix An optional prefix to prepend before the line (useful for multi-line comments.
*/
- private IndentedLambda(int prefixSpaceCount, int indentionCodePoint) {
+ private IndentedLambda(int prefixSpaceCount, int indentionCodePoint, String prefix) {
if (prefixSpaceCount <= 0) {
throw new IllegalArgumentException("prefixSpaceCount must be greater than 0");
}
@@ -78,6 +82,7 @@ public class IndentedLambda implements Mustache.Lambda {
this.prefixSpaceCount = prefixSpaceCount;
this.spaceCode = indentionCodePoint;
+ this.prefix = prefix;
}
@Override
@@ -96,6 +101,7 @@ public class IndentedLambda implements Mustache.Lambda {
// So, we want to skip the first line.
if (i > 0) {
sb.append(prefixedIndention);
+ if (prefix != null) sb.append(prefix);
}
sb.append(line); But could then be used like so:
As we support new templating engines, Mustache lambdas obviously won't be reusable. It might make sense to extract multi-line indentation and multi-line description quoting logic to an infrastructural service that can be consumed by the Mustache lambda or any other template engine extensions we might have in the future. |
8b58443
to
fa4fa3c
Compare
@jimschubert Thank you for the thorough explanation! Implemented the changes as you've suggested worked flawlessly. 👍 |
escapeText
on all references to description
@Danielku15 any concerns with this approach? If not, I think we can merge. |
@jimschubert No concerns, change look good to me. |
* master: (32 commits) Fixed date formatting in typescript node client (OpenAPITools#786) better explain usage (OpenAPITools#794) Fix float/double default value in C# generator (OpenAPITools#791) Enhancements to documentation generators (samples, default values, etc) (OpenAPITools#790) Remove duplicate variable declaration (OpenAPITools#792) Issue 758 root resource (OpenAPITools#771) Do not declare destructor as default when destructor is explicitly declared. (OpenAPITools#732) Fix C# client enum issue (OpenAPITools#774) [JavaScript] Update vulnerable dependencies (OpenAPITools#784) [Ruby] Fix method split (OpenAPITools#780) [Java][jaxrs-jersey] add sample with jaxrs-jersey + openapi v3 (OpenAPITools#778) update groupId in pom (OpenAPITools#779) [cpp-restsdk] Support multi-line descriptions (OpenAPITools#753) [Core] Resolve Inline Models (OpenAPITools#736) [gradle] Support nullable system property values (OpenAPITools#764) Correct URL for openapi-generator.cli.sh in README.md (OpenAPITools#770) Fixed the generation of model properties whose data type is a composed (allOf) schema (OpenAPITools#704) [JAX-RS][Spec] Add samples to CircleCI (OpenAPITools#759) minor update to python generator usage (OpenAPITools#762) [C++][Restbed/Pistache] Added fix for byte array (OpenAPITools#752) ...
* Update IndentedLambda to take optional prefix * Add `multiline_comment_4` to CppRestSdkClient * Update cpp-restsdk example
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
and./bin/security/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
.master
,4.0.x
. Default:master
.Description of the PR
Using the YAML multi-line string notation will cause
cpp-rest-sdk
codegen to fail, with the newlines making it into the generated code:Yields:
The uncommented
FooAPI.\" </param>
, causes a compilation error.This PR adds
escapeText
to all references to the numerousdescription
references acrossDefaultCodegen.java