feat(validation): warn when schema default value is not in enum#23429
Open
ChihweiLHBird wants to merge 1 commit intoOpenAPITools:masterfrom
Open
feat(validation): warn when schema default value is not in enum#23429ChihweiLHBird wants to merge 1 commit intoOpenAPITools:masterfrom
ChihweiLHBird wants to merge 1 commit intoOpenAPITools:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
1 issue found across 4 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/utils/ModelUtils.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java:1110">
P2: `getAllSchemasInDocument` does not actually cover all document schemas; orphaned schemas in non-`components/schemas` sections can be skipped.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java
Show resolved
Hide resolved
261db8e to
2bd53a9
Compare
Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
2bd53a9 to
f616c1f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #23428
When a schema defines both
enumanddefaultbut the default is not one of the enum values, generators can produce incorrect code (e.g., Python emits unquoted defaults causingNameError). This is technically valid JSON Schema but a common spec authoring mistake.This PR adds a per-occurrence WARNING to
validate --recommendthat flags each schema wheredefaultis not inenum:Changes
RuleConfiguration.java-- New toggleenableDefaultNotInEnumRecommendation, controllable via system propertyopenapi.generator.rule.default-not-in-enumOpenApiEvaluator.java-- Per-occurrence check inside thevalidate()method, using the same pattern as unused schemasModelUtils.java-- NewgetAllSchemasInDocument()method that traverses the entire document (paths + components), not justcomponents/schemas, so inline schemas in request bodies and responses are also coveredOpenApiEvaluatorTest.java-- 10 test cases: positive/negative, rule toggle, global recommendations toggle, multiple occurrences, integer enums, inline request body, inline responsePR checklist
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)Summary by cubic
Adds a validation warning when a schema’s
defaultis not one of itsenumvalues. Runs undervalidate --recommendand covers inline path schemas and orphaned component schemas.OpenApiEvaluatorfor enum/default mismatch.ModelUtils.getAllSchemasInDocument()traverses paths andcomponents/schemas(inline + orphaned).RuleConfigurationor system propertyopenapi.generator.rule.default-not-in-enum.OpenApiEvaluatorTestfor rule toggles, integer enums, multiple occurrences, and inline request/response schemas.Written for commit f616c1f. Summary will update on new commits.