Skip to content

Commit 7e67e3a

Browse files
authored
Lambda refactors (#16369)
* started source generator * copy the options * fixed visibility * added new sample * discarded changes to existing samples * discarded changes to existing samples * build new sample * changed package name due to file path length limit * reverted changes to manual tests * fixed all new manual tests * inject contexts into api * only one JsonConstructor * fixed spacing * reverted samples changes * reverted more unrelated changes * reverted more unrelated changes * minor refactors
1 parent da411b3 commit 7e67e3a

21 files changed

Lines changed: 61 additions & 77 deletions

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,9 @@ protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
447447
.put("camelcase", new CamelCaseLambda(true).generator(this))
448448
.put("pascalcase", new CamelCaseLambda(false).generator(this))
449449
.put("indented", new IndentedLambda())
450-
.put("indented_8", new IndentedLambda(8, " "))
451-
.put("indented_12", new IndentedLambda(12, " "))
452-
.put("indented_16", new IndentedLambda(16, " "));
450+
.put("indented_8", new IndentedLambda(8, " ", false))
451+
.put("indented_12", new IndentedLambda(12, " ", false))
452+
.put("indented_16", new IndentedLambda(16, " ", false));
453453
}
454454

455455
private void registerMustacheLambdas() {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,15 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
412412
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
413413
return super.addMustacheLambdas()
414414
.put("camelcase_param", new CamelCaseLambda().generator(this).escapeAsParamName(true))
415-
.put("required", new RequiredParameterLambda().generator(this))
415+
.put("required", new RequiredParameterLambda())
416416
.put("optional", new OptionalParameterLambda().generator(this))
417417
.put("joinWithComma", new JoinWithCommaLambda())
418418
.put("trimLineBreaks", new TrimLineBreaksLambda())
419-
.put("trimTrailingWhiteSpace", new TrimTrailingWhiteSpaceLambda())
419+
.put("trimTrailingWithNewLine", new TrimTrailingWhiteSpaceLambda(true))
420420
.put("first", new FirstLambda(" "))
421421
.put("firstDot", new FirstLambda("\\."))
422-
.put("indent3", new IndentedLambda(12, " "))
423-
.put("indent4", new IndentedLambda(16, " "));
422+
.put("indent3", new IndentedLambda(12, " ", false))
423+
.put("indent4", new IndentedLambda(16, " ", false));
424424
}
425425

426426
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public void processOpts() {
331331
@Override
332332
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
333333
return super.addMustacheLambdas()
334-
.put("multiline_comment_4", new IndentedLambda(4, " ", "///"));
334+
.put("multiline_comment_4", new IndentedLambda(4, " ", "///", false));
335335
}
336336

337337
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public void processOpts() {
224224
@Override
225225
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
226226
return super.addMustacheLambdas()
227-
.put("indented_4", new IndentedLambda(4, " "));
227+
.put("indented_4", new IndentedLambda(4, " ", false));
228228
}
229229

230230
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ protected String getEnumDefaultValue(String defaultValue, String dataType) {
308308
@Override
309309
protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() {
310310
ImmutableMap.Builder<String, Mustache.Lambda> lambdas = super.addMustacheLambdas();
311-
lambdas.put("indented_star_1", new IndentedLambda(1, " ", "* "));
312-
lambdas.put("indented_star_4", new IndentedLambda(5, " ", "* "));
311+
lambdas.put("indented_star_1", new IndentedLambda(1, " ", "* ", false));
312+
lambdas.put("indented_star_4", new IndentedLambda(5, " ", "* ", false));
313313
return lambdas;
314314
}
315315

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
public class CaseFormatLambda implements Mustache.Lambda {
2525
private CodegenConfig generator = null;
2626

27-
private CaseFormat initialFormat;
28-
private CaseFormat targetFormat;
27+
private final CaseFormat initialFormat;
28+
private final CaseFormat targetFormat;
2929

3030
public CaseFormatLambda(CaseFormat target, CaseFormat targetFormat) {
3131
this.initialFormat = target;
@@ -43,6 +43,9 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
4343
if (generator != null && generator.reservedWords().contains(text)) {
4444
text = generator.escapeReservedWord(text);
4545
}
46-
writer.write(text);
46+
47+
if (text != null) {
48+
writer.write(text);
49+
}
4750
}
4851
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
* {@code {{#lambda.escapeDollar}}{{name}}{{/lambda.escapeDollar}} }
3434
*/
3535
public class EscapeChar implements Mustache.Lambda {
36-
private String matchPattern;
37-
private String replacement;
36+
private final String matchPattern;
37+
private final String replacement;
3838

3939
/**
4040
* Constructs a new instance of {@link EscapeChar}, with the desired character to escape

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ public FirstLambda(String delimiter) {
4545

4646
@Override
4747
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
48-
49-
String a = fragment.execute();
50-
51-
5248
String[] parts = fragment.execute().split(this.delimiter);
5349

5450
writer.write(Arrays.stream(parts).findFirst().orElse(""));

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,25 @@
4444
public class IndentedLambda implements Mustache.Lambda {
4545
private final int prefixSpaceCount;
4646
private final String prefix;
47-
private int spaceCode;
47+
private final int spaceCode;
48+
private final boolean indentFirstLine;
4849

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

5657
/**
5758
* Constructs a new instance of {@link IndentedLambda}, with customized indent count and intention character
5859
*
5960
* @param prefixSpaceCount The number of indented characters to apply as a prefix to a fragment.
6061
* @param indentionCharacter String representation of the character used in the indent (e.g. " ", "\t", ".").
62+
* @param indentFirstLine Whether to indent the first line or not. Usually this is handled by the template already.
6163
*/
62-
public IndentedLambda(int prefixSpaceCount, String indentionCharacter) {
63-
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), null);
64+
public IndentedLambda(int prefixSpaceCount, String indentionCharacter, boolean indentFirstLine) {
65+
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), null, indentFirstLine);
6466
}
6567

6668
/**
@@ -69,19 +71,10 @@ public IndentedLambda(int prefixSpaceCount, String indentionCharacter) {
6971
* @param prefixSpaceCount The number of indented characters to apply as a prefix to a fragment.
7072
* @param indentionCharacter String representation of the character used in the indent (e.g. " ", "\t", ".").
7173
* @param prefix An optional prefix to prepend before the line (useful for multi-line comments).
74+
* @param indentFirstLine Whether to indent the first line or not. Usually this is handled by the template already.
7275
*/
73-
public IndentedLambda(int prefixSpaceCount, String indentionCharacter, String prefix) {
74-
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), prefix);
75-
}
76-
77-
/**
78-
* Constructs a new instance of {@link IndentedLambda}
79-
*
80-
* @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.
82-
*/
83-
private IndentedLambda(int prefixSpaceCount, int indentionCodePoint) {
84-
this(prefixSpaceCount, indentionCodePoint, null);
76+
public IndentedLambda(int prefixSpaceCount, String indentionCharacter, String prefix, boolean indentFirstLine) {
77+
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), prefix, indentFirstLine);
8578
}
8679

8780
/**
@@ -90,8 +83,9 @@ private IndentedLambda(int prefixSpaceCount, int indentionCodePoint) {
9083
* @param prefixSpaceCount The number of indented characters to apply as a prefix to a fragment.
9184
* @param indentionCodePoint Code point of the single character used for indentation.
9285
* @param prefix An optional prefix to prepend before the line (useful for multi-line comments).
86+
* @param indentFirstLine Whether to indent the first line or not. Usually this is handled by the template already.
9387
*/
94-
private IndentedLambda(int prefixSpaceCount, int indentionCodePoint, String prefix) {
88+
private IndentedLambda(int prefixSpaceCount, int indentionCodePoint, String prefix, boolean indentFirstLine) {
9589
if (prefixSpaceCount <= 0) {
9690
throw new IllegalArgumentException("prefixSpaceCount must be greater than 0");
9791
}
@@ -103,6 +97,7 @@ private IndentedLambda(int prefixSpaceCount, int indentionCodePoint, String pref
10397
this.prefixSpaceCount = prefixSpaceCount;
10498
this.spaceCode = indentionCodePoint;
10599
this.prefix = prefix;
100+
this.indentFirstLine = indentFirstLine;
106101
}
107102

108103
@Override
@@ -119,7 +114,7 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
119114
String line = lines[i];
120115
// Mustache will apply correct indentation to the first line of a template (to match declaration location).
121116
// So, we want to skip the first line.
122-
if (i > 0) {
117+
if (this.indentFirstLine || i > 0) {
123118
sb.append(prefixedIndention);
124119
if (prefix != null) sb.append(prefix);
125120
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import com.samskivert.mustache.Mustache;
2121
import com.samskivert.mustache.Template;
22-
import org.openapitools.codegen.CodegenConfig;
2322

2423
import java.io.IOException;
2524
import java.io.Writer;
@@ -38,16 +37,11 @@
3837
* </pre>
3938
*/
4039
public class JoinWithCommaLambda implements Mustache.Lambda {
41-
private CodegenConfig generator = null;
4240

4341
public JoinWithCommaLambda() {
4442

4543
}
4644

47-
public JoinWithCommaLambda generator(final CodegenConfig generator) {
48-
this.generator = generator;
49-
return this;
50-
}
5145

5246
@Override
5347
public void execute(Template.Fragment fragment, Writer writer) throws IOException {

0 commit comments

Comments
 (0)