Skip to content

Commit c4a41aa

Browse files
committed
fix(java-spring): expose requestBody named examples to Mustache template context (#23607)
1 parent 32e5f0b commit c4a41aa

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,9 @@ public void setParameterExampleValue(CodegenParameter codegenParameter, RequestB
17821782
}
17831783

17841784
if (mediaType.getExamples() != null && !mediaType.getExamples().isEmpty()) {
1785+
// FIX for #23607: Assign all named examples to the parameter so Mustache templates can access them
1786+
codegenParameter.examples = mediaType.getExamples();
1787+
17851788
Example example = mediaType.getExamples().values().iterator().next();
17861789
if (example.getValue() != null) {
17871790
if (isModel) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8690,4 +8690,38 @@ public void testReactiveSpringHttpInterfaceSupportListOfStringReturnTypeNoRespon
86908690
"Mono<Set<String>> getUserIdSet"
86918691
);
86928692
}
8693+
8694+
@Test
8695+
public void shouldExposeRequestBodyNamedExamplesToTemplateContext_issue23607() throws IOException {
8696+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
8697+
output.deleteOnExit();
8698+
8699+
OpenAPI openAPI = new OpenAPIParser().readLocation(
8700+
"src/test/resources/3_0/spring/requestbody_named_examples.yaml",
8701+
null,
8702+
new ParseOptions()
8703+
).getOpenAPI();
8704+
8705+
SpringCodegen codegen = new SpringCodegen();
8706+
codegen.setOutputDir(output.getAbsolutePath());
8707+
codegen.additionalProperties().put(INTERFACE_ONLY, "true");
8708+
8709+
ClientOptInput input = new ClientOptInput();
8710+
input.openAPI(openAPI);
8711+
input.config(codegen);
8712+
8713+
DefaultGenerator generator = new DefaultGenerator();
8714+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
8715+
generator.setGenerateMetadata(false);
8716+
generator.opts(input).generate();
8717+
8718+
// Verify via codegen operation inspection after initialization
8719+
Operation operation = openAPI.getPaths().get("/users").getPost();
8720+
CodegenOperation codegenOperation = codegen.fromOperation("/users", "POST", operation, null);
8721+
8722+
assertNotNull(codegenOperation.bodyParam);
8723+
assertNotNull(codegenOperation.bodyParam.examples);
8724+
assertTrue(codegenOperation.bodyParam.examples.containsKey("Jessica"));
8725+
assertTrue(codegenOperation.bodyParam.examples.containsKey("Ron"));
8726+
}
86938727
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Example API
4+
version: 1.0.0
5+
paths:
6+
/users:
7+
post:
8+
summary: Adds a new user
9+
tags:
10+
- User
11+
requestBody:
12+
content:
13+
application/json:
14+
schema:
15+
$ref: "#/components/schemas/User"
16+
examples:
17+
Jessica:
18+
value:
19+
id: 10
20+
name: Jessica Smith
21+
Ron:
22+
value:
23+
id: 11
24+
name: Ron Stewart
25+
responses:
26+
"200":
27+
description: OK
28+
components:
29+
schemas:
30+
User:
31+
type: object
32+
properties:
33+
id:
34+
type: integer
35+
name:
36+
type: string

0 commit comments

Comments
 (0)