Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -2454,6 +2454,23 @@ public void setParameterExamples(CodegenParameter codegenParameter, Parameter pa
}
}

/**
* Return the examples of the request body parameter.
*
* @param codegenParameter Codegen parameter
* @param requestBody Request body
*/
public void setParameterExamples(CodegenParameter codegenParameter, RequestBody requestBody) {
if (requestBody.getContent() != null && !requestBody.getContent().isEmpty()) {
for (MediaType mediaType : requestBody.getContent().values()) {
if (mediaType.getExamples() != null && !mediaType.getExamples().isEmpty()) {
codegenParameter.examples = mediaType.getExamples();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: TypeScript generators still do not expose named request-body examples because their override bypasses this base implementation. Apply the shared examples population in that override as well, or refactor the body-processing path so every override receives it.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java, line 2467:

<comment>TypeScript generators still do not expose named request-body examples because their override bypasses this base implementation. Apply the shared examples population in that override as well, or refactor the body-processing path so every override receives it.</comment>

<file context>
@@ -2454,6 +2454,23 @@ public void setParameterExamples(CodegenParameter codegenParameter, Parameter pa
+        if (requestBody.getContent() != null && !requestBody.getContent().isEmpty()) {
+            for (MediaType mediaType : requestBody.getContent().values()) {
+                if (mediaType.getExamples() != null && !mediaType.getExamples().isEmpty()) {
+                    codegenParameter.examples = mediaType.getExamples();
+                    break;
+                }
</file context>

break;
}
}
}
}

/**
* Return the example value of the parameter.
*
Expand All @@ -2463,6 +2480,9 @@ public void setParameterExamples(CodegenParameter codegenParameter, Parameter pa
public void setParameterExampleValue(CodegenParameter codegenParameter, RequestBody requestBody) {
Content content = requestBody.getContent();

// Reuse the helper method to correctly catch examples across all media types
setParameterExamples(codegenParameter, requestBody);

Optional<Object> contentExample = ExamplesUtils.getContentExample(content);

if (contentExample.isPresent()) {
Expand Down Expand Up @@ -8290,6 +8310,9 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
codegenParameter.vendorExtensions.putAll(body.getExtensions());
}

// Ensure request body named examples are populated globally for ALL generators
setParameterExamples(codegenParameter, body);

String name = null;
LOGGER.debug("Request body = {}", body);
Schema schema = ModelUtils.getSchemaFromRequestBody(body);
Expand Down Expand Up @@ -8417,11 +8440,6 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
if (original.getMinimum() != null) {
codegenParameter.setMinimum(String.valueOf(original.getMinimum().doubleValue()));
}
/* comment out below as we don't store `title` in the codegen parametera the moment
if (original.getTitle() != null) {
codegenParameter.setTitle(original.getTitle());
}
*/
}

return codegenParameter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1751,30 +1751,7 @@ public void setParameterExampleValue(CodegenParameter codegenParameter, Paramete

setParameterExampleValue(codegenParameter);
}

/**
* Return the example value of the parameter. Overrides the parent method in DefaultCodegen
* to not set examples on complex models, as they don't compile properly.
*
* @param codegenParameter Codegen parameter
* @param requestBody Request body
*/
@Override
public void setParameterExampleValue(CodegenParameter codegenParameter, RequestBody requestBody) {
boolean isModel = (codegenParameter.isModel || (codegenParameter.isContainer && codegenParameter.getItems().isModel));

MediaType mediaType = requestBody.getContent().values().iterator().next();
boolean hasExample = mediaType.getExample() != null || (mediaType.getExamples() != null && !mediaType.getExamples().isEmpty());
if (isModel) {
if (hasExample) {
once(LOGGER).warn("Ignoring complex example on request body");
}
setParameterExampleValue(codegenParameter);
} else {
super.setParameterExampleValue(codegenParameter, requestBody);
}
}


@Override
public void setParameterExampleValue(CodegenParameter p) {
String example;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8690,4 +8690,29 @@ public void testReactiveSpringHttpInterfaceSupportListOfStringReturnTypeNoRespon
"Mono<Set<String>> getUserIdSet"
);
}

@Test
public void shouldExposeRequestBodyNamedExamplesToTemplateContext_issue23607() throws IOException {
OpenAPI openAPI = new OpenAPIParser().readLocation(
"src/test/resources/3_0/spring/requestbody_named_examples.yaml",
null,
new ParseOptions()
).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.additionalProperties().put(INTERFACE_ONLY, "true");

// Preprocess and set OpenAPI to mirror DefaultGenerator behavior
codegen.preprocessOpenAPI(openAPI);
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
codegen.setOpenAPI(openAPI);

// Verify via codegen operation inspection after initialization
Operation operation = openAPI.getPaths().get("/users").getPost();
CodegenOperation codegenOperation = codegen.fromOperation("/users", "POST", operation, null);

assertNotNull(codegenOperation.bodyParam);
assertNotNull(codegenOperation.bodyParam.examples);
assertTrue(codegenOperation.bodyParam.examples.containsKey("Jessica"));
assertTrue(codegenOperation.bodyParam.examples.containsKey("Ron"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
openapi: 3.0.0
info:
title: Example API
version: 1.0.0
paths:
/users:
post:
summary: Adds a new user
tags:
- User
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/User"
examples:
Jessica:
value:
id: 10
name: Jessica Smith
Ron:
value:
id: 11
name: Ron Stewart
responses:
"200":
description: OK
components:
schemas:
User:
type: object
properties:
id:
type: integer
name:
type: string
Loading