fix(java-spring): expose requestBody named examples to Mustache template context (#23607) - #24651
Conversation
There was a problem hiding this comment.
All reported issues were addressed across 3 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
3b09844 to
c4a41aa
Compare
|
thanks for the PR did you have a chance to review the feedback from cubic-dev-ai? calling |
|
Any particular reason as to why this is placed within the |
| @Override | ||
| public void setParameterExampleValue(CodegenParameter codegenParameter, RequestBody requestBody) { | ||
| boolean isModel = (codegenParameter.isModel || (codegenParameter.isContainer && codegenParameter.getItems().isModel)); | ||
| if (requestBody.getContent() != null && !requestBody.getContent().isEmpty()) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Thanks for the thorough review, @Mattias-Sehlstedt!
-
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.
-
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!
There was a problem hiding this comment.
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.
-
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.
-
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.
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
DefaultCodegen has setParameterExamples(CodegenParameter codegenParameter, Parameter parameter) which does basically the same as this. Is that something we should mimic for clarity?
There was a problem hiding this comment.
Thanks for pointing that out! Mimicking DefaultCodegen#setParameterExamples makes total sense for consistency and clarity. I can refactor this to align with that pattern.
There was a problem hiding this comment.
All reported issues were addressed across 1 file (changes from recent commits).
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
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(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Thanks, @Mattias-Sehlstedt! I completely removed the custom override from AbstractJavaCodegen so that it relies purely on the global logic in DefaultCodegen now.
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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>
|
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. |
|
Thanks, @Mattias-Sehlstedt! That makes total sense. I appreciate you taking it across the finish line with a cleaner, more targeted approach. |
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
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.
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.DefaultCodegen#setParameterExamples(RequestBody)and invoke it from bothsetParameterExampleValue(RequestBody)andfromRequestBody(...)to populatecodegenParameter.examplesfromMediaType.getExamples().AbstractJavaCodegento centralize behavior inDefaultCodegen.SpringCodegenTest: preprocess and set the OpenAPI; addrequestbody_named_examples.yaml; assert named examples “Jessica” and “Ron” are exposed.Written for commit 0ef86e7. Summary will update on new commits.