Skip to content

Commit 987fd77

Browse files
Peaches491jimschubert
authored andcommitted
[cpp-restsdk] Support multi-line descriptions (#753)
* Update IndentedLambda to take optional prefix * Add `multiline_comment_4` to CppRestSdkClient * Update cpp-restsdk example
1 parent a0984a9 commit 987fd77

39 files changed

Lines changed: 88 additions & 39 deletions

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919

2020
import io.swagger.v3.oas.models.media.Schema;
2121

22+
import com.google.common.collect.ImmutableMap;
23+
import com.samskivert.mustache.Mustache;
2224
import org.openapitools.codegen.CodegenConfig;
2325
import org.openapitools.codegen.CodegenProperty;
2426
import org.openapitools.codegen.DefaultCodegen;
27+
import org.openapitools.codegen.mustache.IndentedLambda;
2528
import org.slf4j.Logger;
2629
import org.slf4j.LoggerFactory;
2730

2831
import java.util.Arrays;
32+
import java.util.Map;
2933

3034
abstract public class AbstractCppCodegen extends DefaultCodegen implements CodegenConfig {
3135
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractCppCodegen.class);
@@ -242,4 +246,23 @@ public String toBooleanGetter(String name) {
242246
public String getTypeDeclaration(String str) {
243247
return "std::shared_ptr<" + toModelName(str) + ">";
244248
}
249+
250+
public void processOpts() {
251+
super.processOpts();
252+
addMustacheLambdas(additionalProperties);
253+
}
254+
255+
private void addMustacheLambdas(Map<String, Object> objs) {
256+
257+
Map<String, Mustache.Lambda> lambdas = new ImmutableMap.Builder<String, Mustache.Lambda>()
258+
.put("multiline_comment_4", new IndentedLambda(4, " ", "///"))
259+
.build();
260+
261+
if (objs.containsKey("lambda")) {
262+
LOGGER.warn("A property named 'lambda' already exists. Mustache lambdas renamed from 'lambda' to '_lambda'.");
263+
objs.put("_lambda", lambdas);
264+
} else {
265+
objs.put("lambda", lambdas);
266+
}
267+
}
245268
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/mustache/IndentedLambda.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@
4343
*/
4444
public class IndentedLambda implements Mustache.Lambda {
4545
private final int prefixSpaceCount;
46+
private final String prefix;
4647
private int spaceCode;
4748

4849
/**
4950
* Constructs a new instance of {@link IndentedLambda}, with an indent count of 4 spaces
5051
*/
5152
public IndentedLambda() {
52-
this(4, " ");
53+
this(4, " ", null);
5354
}
5455

5556
/**
@@ -59,15 +60,38 @@ public IndentedLambda() {
5960
* @param indentionCharacter String representation of the character used in the indent (e.g. " ", "\t", ".").
6061
*/
6162
public IndentedLambda(int prefixSpaceCount, String indentionCharacter) {
62-
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0));
63+
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), null);
64+
}
65+
66+
/**
67+
* Constructs a new instance of {@link IndentedLambda}, with customized indent count and intention character
68+
*
69+
* @param prefixSpaceCount The number of indented characters to apply as a prefix to a fragment.
70+
* @param indentionCharacter String representation of the character used in the indent (e.g. " ", "\t", ".").
71+
* @param prefix An optional prefix to prepend before the line (useful for multi-line comments).
72+
*/
73+
public IndentedLambda(int prefixSpaceCount, String indentionCharacter, String prefix) {
74+
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), prefix);
6375
}
6476

6577
/**
6678
* Constructs a new instance of {@link IndentedLambda}
6779
*
6880
* @param prefixSpaceCount The number of indented characters to apply as a prefix to a fragment.
81+
* @param indentionCodePoint Code point of the single character used for indentation.
6982
*/
7083
private IndentedLambda(int prefixSpaceCount, int indentionCodePoint) {
84+
this(prefixSpaceCount, indentionCodePoint, null);
85+
}
86+
87+
/**
88+
* Constructs a new instance of {@link IndentedLambda}
89+
*
90+
* @param prefixSpaceCount The number of indented characters to apply as a prefix to a fragment.
91+
* @param indentionCodePoint Code point of the single character used for indentation.
92+
* @param prefix An optional prefix to prepend before the line (useful for multi-line comments).
93+
*/
94+
private IndentedLambda(int prefixSpaceCount, int indentionCodePoint, String prefix) {
7195
if (prefixSpaceCount <= 0) {
7296
throw new IllegalArgumentException("prefixSpaceCount must be greater than 0");
7397
}
@@ -78,6 +102,7 @@ private IndentedLambda(int prefixSpaceCount, int indentionCodePoint) {
78102

79103
this.prefixSpaceCount = prefixSpaceCount;
80104
this.spaceCode = indentionCodePoint;
105+
this.prefix = prefix;
81106
}
82107

83108
@Override
@@ -96,6 +121,7 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
96121
// So, we want to skip the first line.
97122
if (i > 0) {
98123
sb.append(prefixedIndention);
124+
if (prefix != null) sb.append(prefix);
99125
}
100126

101127
sb.append(line);

modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-header.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public:
6262
/// {{notes}}
6363
/// </remarks>
6464
{{#allParams}}
65-
/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
65+
/// <param name="{{paramName}}">{{#lambda.multiline_comment_4}}{{description}}{{/lambda.multiline_comment_4}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
6666
{{/allParams}}
6767
pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{operationId}}(
6868
{{#allParams}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.2-SNAPSHOT
1+
3.2.1-SNAPSHOT

samples/client/petstore/cpp-restsdk/ApiClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.2-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.2.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.2-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.2.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiConfiguration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.2-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.2.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiConfiguration.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.2-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.2.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiException.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.2-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.2.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiException.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.2-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.2.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

0 commit comments

Comments
 (0)