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).
Note that the @Override on the method also seems to have fallen back one space.
| } | ||
|
|
||
| // 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. |
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 named examples to Mustache templates in the Java Spring generator and set a scalar example for non-model request bodies. Previously templates could not access named examples; now they are available on
codegenParameter.examples, and non-model bodies use the first named example forcodegenParameter.example(models still ignore complex examples and log once).mediaType.getExamples()tocodegenParameter.examples; falls back to existing example logic when none are provided.codegenParameter.examplefrom the first named example’s value.Written for commit 5f90fdd. Summary will update on new commits.