fix(core): avoid dangling request-model import for skipped form models - #24807
fix(core): avoid dangling request-model import for skipped form models#24807SubhamAshok wants to merge 5 commits into
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/main/java/org/openapitools/codegen/DefaultCodegen.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java:8311">
P2: The import-suppression signal here diverges from how DefaultGenerator decides whether a form model is actually generated. DefaultGenerator skips a model only when it is listed in `unusedModels` (used exclusively as a form parameter), and it resolves the `skipFormModel` default through `getGeneratorPropertyDefaultSwitch(CodegenConstants.SKIP_FORM_MODEL, true)`, which honors per-generator `generatorPropertyDefaults`. The new `isSkipFormModel()` instead always falls back to `GlobalSettings` default `"true"` and the condition suppresses imports purely on content type, ignoring whether the model is in `unusedModels`. When a multipart request-body schema references a model that is genuinely generated (used elsewhere, so not in `unusedModels`), this condition drops its import, producing a missing-import failure — the inverse of the bug being fixed. The two signals should match.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
|
|
||
| cmtContent.put(contentType, codegenMt); | ||
| if (schemaProp != null) { | ||
| if (schemaProp != null && (!isFormContentType(contentType) || !isSkipFormModel())) { |
There was a problem hiding this comment.
P2: The import-suppression signal here diverges from how DefaultGenerator decides whether a form model is actually generated. DefaultGenerator skips a model only when it is listed in unusedModels (used exclusively as a form parameter), and it resolves the skipFormModel default through getGeneratorPropertyDefaultSwitch(CodegenConstants.SKIP_FORM_MODEL, true), which honors per-generator generatorPropertyDefaults. The new isSkipFormModel() instead always falls back to GlobalSettings default "true" and the condition suppresses imports purely on content type, ignoring whether the model is in unusedModels. When a multipart request-body schema references a model that is genuinely generated (used elsewhere, so not in unusedModels), this condition drops its import, producing a missing-import failure — the inverse of the bug being fixed. The two signals should match.
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 8311:
<comment>The import-suppression signal here diverges from how DefaultGenerator decides whether a form model is actually generated. DefaultGenerator skips a model only when it is listed in `unusedModels` (used exclusively as a form parameter), and it resolves the `skipFormModel` default through `getGeneratorPropertyDefaultSwitch(CodegenConstants.SKIP_FORM_MODEL, true)`, which honors per-generator `generatorPropertyDefaults`. The new `isSkipFormModel()` instead always falls back to `GlobalSettings` default `"true"` and the condition suppresses imports purely on content type, ignoring whether the model is in `unusedModels`. When a multipart request-body schema references a model that is genuinely generated (used elsewhere, so not in `unusedModels`), this condition drops its import, producing a missing-import failure — the inverse of the bug being fixed. The two signals should match.</comment>
<file context>
@@ -8285,7 +8308,7 @@ protected LinkedHashMap<String, CodegenMediaType> getContent(Content content, Se
cmtContent.put(contentType, codegenMt);
- if (schemaProp != null) {
+ if (schemaProp != null && (!isFormContentType(contentType) || !isSkipFormModel())) {
addImports(imports, schemaProp.getImports(true, importBaseType, generatorMetadata.getFeatureSet()));
}
</file context>
HDPark95
left a comment
There was a problem hiding this comment.
Verified at 3e336bb: reverting only DefaultCodegen.java fails the new dangling-import test, so it is pinned.
isSkipFormModel() reads GlobalSettings only, but DefaultGenerator resolves skipFormModel as GlobalSettings -> generatorPropertyDefaults -> true. On the issue spec with setGeneratorPropertyDefault(SKIP_FORM_MODEL, "false") I measured: model generated, import dropped, the opposite of what your second test asserts. That test also passes on unmodified master, so it does not cover this path.
Could the guard reuse that resolution, or getSchemasUsedOnlyInFormParam?
|
Thanks for the feedback! I updated the implementation:
|
There was a problem hiding this comment.
All reported issues were addressed across 4 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
There was a problem hiding this comment.
All reported issues were addressed across 2 files (changes from recent commits).
Tip: Review your code locally with the cubic CLI to iterate faster.
Re-trigger cubic
There was a problem hiding this comment.
All reported issues were addressed across 2 files (changes from recent commits).
Tip: Review your code locally with the cubic CLI to iterate faster.
Re-trigger cubic
jpfinne
left a comment
There was a problem hiding this comment.
Create a general utility method to retrieve the best configuration
| Boolean skipFormModel = GlobalSettings.getProperty(CodegenConstants.SKIP_FORM_MODEL) != null ? | ||
| Boolean.valueOf(GlobalSettings.getProperty(CodegenConstants.SKIP_FORM_MODEL)) : | ||
| getGeneratorPropertyDefaultSwitch(CodegenConstants.SKIP_FORM_MODEL, true); | ||
| Boolean skipFormModel = null; |
There was a problem hiding this comment.
@SubhamAshok I had the same kind of issue when I wrote DefaultCodegen.convertPropertyToBooleanAndWriteBack
The configuration could come from a cliOption or from additionalProperties. Before there were discrepancies, leading to side effects if the code or the template used additionalProperties or getter in the CodeGen.
It think we could have the same for GlobalSettings.
Maybe we can have something like:
boolean convertGlobalSettingsPropertyToBooleanAndWriteBack(String propertyKey, Consumer<Boolean> booleanSetter, Supplier<Boolean> defaultIfMissing)
There was a problem hiding this comment.
Thanks for the suggestion! Added convertGlobalPropertyToBooleanAndWriteBack in DefaultGenerator to resolve the global/generator defaults and write them back into additionalProperties. configureGeneratorProperties and generateModels now use this single standard path.
| Boolean skipFormModel = GlobalSettings.getProperty(CodegenConstants.SKIP_FORM_MODEL) != null ? | ||
| Boolean.valueOf(GlobalSettings.getProperty(CodegenConstants.SKIP_FORM_MODEL)) : | ||
| getGeneratorPropertyDefaultSwitch(CodegenConstants.SKIP_FORM_MODEL, true); | ||
| Boolean skipFormModel = config.additionalProperties().containsKey(CodegenConstants.SKIP_FORM_MODEL) ? |
There was a problem hiding this comment.
@SubhamAshok Same as above. This looks way too convulated
9ba5af1 to
b16feb6
Compare
PR checklist
Description of the change
Fixes #24727.
When an operation's
requestBodydefines multiple content types (for example,application/jsonandmultipart/form-data),InlineModelResolvercreates an inline model for the form schema. InDefaultCodegen#getContent, all media types inrequestBody.getContent()were unconditionally adding their schema types toimports.When
skipFormModelis enabled (defaulttrue),DefaultGeneratorskips generating the model class for schemas used only in form parameters. This caused the generated API interface to emit a dangling import for a non-existent model class and fail compilation.This fix updates
DefaultCodegen#getContentto avoid registering imports for form/multipart media types whenskipFormModelis enabled.Tests
testMultipleRequestBodyContentTypesDanglingImport_issue24727andtestMultipleRequestBodyContentTypesWithSkipFormModelFalse_issue24727inSpringCodegenTest.java.SpringCodegenTest,DefaultCodegenTest, andModelUtilsTestall pass.Summary by cubic
Fixes #24727 so
DefaultCodegen#getContentno longer emits dangling imports for form models skipped byskipFormModel(default true). Previously every form/multipart schema type was added to imports even when its model class wasn't generated, which broke compilation; now only schemas used exclusively in form parameters are filtered out, and shared form models still generate.Bug Fixes
isFormContentType,isSkipFormModel, and a cachedgetSchemasUsedOnlyInFormParamlookup inDefaultCodegen.DefaultGeneratorresolvesskipFormModelonce inconfigureGeneratorProperties, stores the normalized value inadditionalProperties, andgenerateModelsreads it from there so generation and import filtering use the same value.SpringCodegenTestcases and fixturesissue_24727.yamlandissue_24727_shared.yamlcovering default behavior,skipFormModel=false, and shared form models.Written for commit b16feb6. Summary will update on new commits.