Skip to content

fix(java-spring): expose requestBody named examples to Mustache template context (#23607) - #24651

Closed
anupamchaubey wants to merge 7 commits into
OpenAPITools:masterfrom
anupamchaubey:fix/requestbody-examples-23607
Closed

fix(java-spring): expose requestBody named examples to Mustache template context (#23607)#24651
anupamchaubey wants to merge 7 commits into
OpenAPITools:masterfrom
anupamchaubey:fix/requestbody-examples-23607

Conversation

@anupamchaubey

@anupamchaubey anupamchaubey commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #23607.

Description of Changes
Updated AbstractJavaCodegen to correctly populate named requestBody examples from mediaType.getExamples() into codegenParameter.examples, ensuring they are properly exposed to Mustache templates.

Added a unit test in SpringCodegenTest along with a test OpenAPI specification YAML (requestbody_named_examples.yaml) to verify that named request body examples are correctly mapped and available for template rendering.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Expose named requestBody examples to Mustache templates for all generators via codegenParameter.examples. Previously templates could not access named examples; now they are populated from request body media types without changing existing example value fallbacks.

  • Add DefaultCodegen#setParameterExamples(RequestBody) and invoke it from both setParameterExampleValue(RequestBody) and fromRequestBody(...) to populate codegenParameter.examples from MediaType.getExamples().
  • Remove the Java-only override in AbstractJavaCodegen to centralize behavior in DefaultCodegen.
  • Update SpringCodegenTest: preprocess and set the OpenAPI; add requestbody_named_examples.yaml; assert named examples “Jessica” and “Ron” are exposed.

Written for commit 0ef86e7. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

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.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@wing328

wing328 commented Aug 13, 2026

Copy link
Copy Markdown
Member

thanks for the PR

did you have a chance to review the feedback from cubic-dev-ai?

calling generator.opts(input).generate(); seems not necessary?

@Mattias-Sehlstedt

Copy link
Copy Markdown
Contributor

Any particular reason as to why this is placed within the JavaCodegen and not in the DefaultCodegen? I would assume that this is something that all code generators most likely would want to access?

@Override
public void setParameterExampleValue(CodegenParameter codegenParameter, RequestBody requestBody) {
boolean isModel = (codegenParameter.isModel || (codegenParameter.isContainer && codegenParameter.getItems().isModel));
if (requestBody.getContent() != null && !requestBody.getContent().isEmpty()) {

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.

Is it an intentional change that we now silently continue processing rather than throwing an exception if the request body does not contain any content?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the thorough review, @Mattias-Sehlstedt!

  1. Why AbstractJavaCodegen instead of DefaultCodegen? You make a great point; exposing named examples is likely useful for other generators too. I implemented it here as a focused fix for Java, but I can look into shifting this up to DefaultCodegen to align with setParameterExamples if you think it belongs globally.

  2. Silent continue on empty content: Yes, that was an intentional adjustment to avoid abrupt runtime exceptions on non-standard specs, allowing the generator to proceed smoothly. However, if a strict check or warning is preferred there, I'd be happy to add it back!

@Mattias-Sehlstedt Mattias-Sehlstedt Aug 13, 2026

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.

To clarify if it was not clear to begin with: I am not affiliated with the project in any sense, and that these are my personal opinions.

  1. I would argue that it definitely belongs as a global configuration so that any language generator can utilize the data to construct better examples locally with the mustache files.

  2. Given that no one has raised any issue with a NPE being thrown unexpectedly I would leave that behavior as-is since I would argue that its age might even be an indicator of it not even being something that is possible (and if it is an issue, then someone will have to raise an issue specifically for that and highlight exactly when it occurs and what is a proper way of handling it).

Additionally, the current logic does not prevent any issues, since a null content will just defer to the setParameterExampleValue(CodegenParameter codegenParameter, RequestBody requestBody) in DefaultCodegen, which does not have null handling, and thus we get the same issue.

Note that the @Override on the method also seems to have fallen back one space.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks again for the clear feedback, @Mattias-Sehlstedt! I will refactor this to move the logic up into DefaultCodegen so all generators can benefit from it, restore the original null handling behavior, and fix the @OverRide indentation spacing. I'll push the updates shortly.

}

// FIX for #23607: Assign all named examples to the parameter so Mustache templates can access them
if (mediaType.getExamples() != null && !mediaType.getExamples().isEmpty()) {

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.

DefaultCodegen has setParameterExamples(CodegenParameter codegenParameter, Parameter parameter) which does basically the same as this. Is that something we should mimic for clarity?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing that out! Mimicking DefaultCodegen#setParameterExamples makes total sense for consistency and clarity. I can refactor this to align with that pattern.

@cubic-dev-ai cubic-dev-ai Bot left a comment

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.

All reported issues were addressed across 1 file (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@anupamchaubey

Copy link
Copy Markdown
Contributor Author

thanks for the PR

did you have a chance to review the feedback from cubic-dev-ai?

calling generator.opts(input).generate(); seems not necessary?

Thanks, @wing328! Yes, you were spot on—I've removed the unnecessary generator.opts(input).generate() call and cleaned up the test scaffolding.

if (hasExample) {
once(LOGGER).warn("Ignoring complex example on request body");
if (!isModel) {
Example example = mediaType.getExamples().values().iterator().next();

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.

This logic seems to duplicate what is done in the DefaultCodegen? Would it be possible to instead keep the logic so that it just defers to super.setParameterExampleValue(codegenParameter, requestBody); as it did before?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, @Mattias-Sehlstedt! I completely removed the custom override from AbstractJavaCodegen so that it relies purely on the global logic in DefaultCodegen now.

@cubic-dev-ai cubic-dev-ai Bot left a comment

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.

1 issue found across 2 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java:2467">
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.</violation>
</file>

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

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>

@Mattias-Sehlstedt

Copy link
Copy Markdown
Contributor

Hello again, I have created a more targeted change for the issue #24707 since this PR currently seems to leak logic with how the samples have changed.

@anupamchaubey

Copy link
Copy Markdown
Contributor Author

Thanks, @Mattias-Sehlstedt! That makes total sense. I appreciate you taking it across the finish line with a cleaner, more targeted approach.

@anupamchaubey
anupamchaubey deleted the fix/requestbody-examples-23607 branch August 14, 2026 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][JavaSpring] requestBody named examples (OAS3) not exposed to Mustache template variables

3 participants