Skip to content

Commit 3a262c3

Browse files
committed
Use newly introduced lambdas in Spring's API template to avoid generating uncompilable example code
1 parent 9cc1ad0 commit 3a262c3

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import io.swagger.codegen.*;
66
import io.swagger.codegen.languages.features.BeanValidationFeatures;
77
import io.swagger.codegen.languages.features.OptionalFeatures;
8+
import io.swagger.codegen.mustache.SplitStringLambda;
9+
import io.swagger.codegen.mustache.TrimWhitespaceLambda;
810
import io.swagger.models.Operation;
911
import io.swagger.models.Path;
1012
import io.swagger.models.Swagger;
@@ -351,6 +353,10 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
351353
writer.write(fragment.execute().replaceAll("\\r|\\n", ""));
352354
}
353355
});
356+
357+
additionalProperties.put("lambdaTrimWhitespace", new TrimWhitespaceLambda());
358+
359+
additionalProperties.put("lambdaSplitString", new SplitStringLambda());
354360
}
355361

356362
@Override

modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public interface {{classname}} {
113113
{{#examples}}
114114
if (getAcceptHeader().get().contains("{{{contentType}}}")) {
115115
try {
116-
String exampleString = "{{#lambdaRemoveLineBreak}}{{#lambdaEscapeDoubleQuote}}{{{example}}}{{/lambdaEscapeDoubleQuote}}{{/lambdaRemoveLineBreak}}";
116+
String exampleString = {{#lambdaSplitString}}{{#lambdaRemoveLineBreak}}{{#lambdaEscapeDoubleQuote}}{{#lambdaTrimWhitespace}}{{{example}}}{{/lambdaTrimWhitespace}}{{/lambdaEscapeDoubleQuote}}{{/lambdaRemoveLineBreak}}{{/lambdaSplitString}};
117117
return {{#async}}CompletableFuture.completedFuture({{/async}}new ResponseEntity<>(getObjectMapper().get().readValue(exampleString, {{>exampleReturnTypes}}.class), HttpStatus.NOT_IMPLEMENTED){{#async}}){{/async}};
118118
} catch (IOException e) {
119119
log.error("Couldn't serialize response for content type {{{contentType}}}", e);
@@ -133,4 +133,4 @@ public interface {{classname}} {
133133

134134
{{/operation}}
135135
}
136-
{{/operations}}
136+
{{/operations}}

0 commit comments

Comments
 (0)