fix(java-spring): expose requestBody named examples to Mustache template context (#23607) - #24651
Conversation
There was a problem hiding this comment.
1 issue found across 3 files
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/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java">
<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java:8720">
P3: The test starts a full generator run into a temp directory and computes an `outputPath` variable, but the actual assertions never read the generated files — they call `codegen.fromOperation(...)` directly and check the returned `bodyParam.examples` map. As written, the `DefaultGenerator` + `generate()` block and the `outputPath` variable are dead scaffolding that adds runtime cost and noise without verifying anything. I'd suggest dropping the unused `outputPath` and either removing the redundant generator execution, or better, asserting on the rendered template output if the goal is to prove Mustache exposure.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| generator.setGenerateMetadata(false); | ||
|
|
||
| // Run generator | ||
| generator.opts(input).generate(); |
There was a problem hiding this comment.
P3: The test starts a full generator run into a temp directory and computes an outputPath variable, but the actual assertions never read the generated files — they call codegen.fromOperation(...) directly and check the returned bodyParam.examples map. As written, the DefaultGenerator + generate() block and the outputPath variable are dead scaffolding that adds runtime cost and noise without verifying anything. I'd suggest dropping the unused outputPath and either removing the redundant generator execution, or better, asserting on the rendered template output if the goal is to prove Mustache exposure.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java, line 8720:
<comment>The test starts a full generator run into a temp directory and computes an `outputPath` variable, but the actual assertions never read the generated files — they call `codegen.fromOperation(...)` directly and check the returned `bodyParam.examples` map. As written, the `DefaultGenerator` + `generate()` block and the `outputPath` variable are dead scaffolding that adds runtime cost and noise without verifying anything. I'd suggest dropping the unused `outputPath` and either removing the redundant generator execution, or better, asserting on the rendered template output if the goal is to prove Mustache exposure.</comment>
<file context>
@@ -8690,4 +8690,43 @@ public void testReactiveSpringHttpInterfaceSupportListOfStringReturnTypeNoRespon
+ generator.setGenerateMetadata(false);
+
+ // Run generator
+ generator.opts(input).generate();
+
+ // Verify or test directly via CodegenOperation model inspection
</file context>
3b09844 to
c4a41aa
Compare
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 in the Java Spring generator by propagating
MediaType.examplesintocodegenParameter.examples. This makes all named examples available for template rendering.AbstractJavaCodegen#setParameterExampleValueto assignmediaType.getExamples()tocodegenParameter.examplesfor requestBody params.SpringCodegenTestusingrequestbody_named_examples.yaml; verifiescodegenOperation.bodyParam.examplesincludes named examples ("Jessica", "Ron").Written for commit c4a41aa. Summary will update on new commits.