Skip to content

feat(validation): warn when schema default value is not in enum#23429

Open
ChihweiLHBird wants to merge 1 commit intoOpenAPITools:masterfrom
ChihweiLHBird:zhiwei/validate-default-not-in-enum
Open

feat(validation): warn when schema default value is not in enum#23429
ChihweiLHBird wants to merge 1 commit intoOpenAPITools:masterfrom
ChihweiLHBird:zhiwei/validate-default-not-in-enum

Conversation

@ChihweiLHBird
Copy link
Copy Markdown
Contributor

@ChihweiLHBird ChihweiLHBird commented Apr 2, 2026

Resolves #23428

When a schema defines both enum and default but the default is not one of the enum values, generators can produce incorrect code (e.g., Python emits unquoted defaults causing NameError). This is technically valid JSON Schema but a common spec authoring mistake.

This PR adds a per-occurrence WARNING to validate --recommend that flags each schema where default is not in enum:

Warnings:
    - Schema has default value 'http' not in enum [udp]
    - Schema has default value 'http' not in enum [tcp]

Changes

  • RuleConfiguration.java -- New toggle enableDefaultNotInEnumRecommendation, controllable via system property openapi.generator.rule.default-not-in-enum
  • OpenApiEvaluator.java -- Per-occurrence check inside the validate() method, using the same pattern as unused schemas
  • ModelUtils.java -- New getAllSchemasInDocument() method that traverses the entire document (paths + components), not just components/schemas, so inline schemas in request bodies and responses are also covered
  • OpenApiEvaluatorTest.java -- 10 test cases: positive/negative, rule toggle, global recommendations toggle, multiple occurrences, integer enums, inline request body, inline response

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    No samples are affected -- this change only touches the validation pipeline, not templates or generators.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)

Summary by cubic

Adds a validation warning when a schema’s default is not one of its enum values. Runs under validate --recommend and covers inline path schemas and orphaned component schemas.

  • New Features
    • Per-occurrence WARNING in OpenApiEvaluator for enum/default mismatch.
    • New ModelUtils.getAllSchemasInDocument() traverses paths and components/schemas (inline + orphaned).
    • Enabled by default; toggle via RuleConfiguration or system property openapi.generator.rule.default-not-in-enum.
    • Tests in OpenApiEvaluatorTest for rule toggles, integer enums, multiple occurrences, and inline request/response schemas.

Written for commit f616c1f. Summary will update on new commits.

@ChihweiLHBird ChihweiLHBird marked this pull request as ready for review April 2, 2026 23:29
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ChihweiLHBird ChihweiLHBird force-pushed the zhiwei/validate-default-not-in-enum branch from 261db8e to 2bd53a9 Compare April 3, 2026 04:34
Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
@ChihweiLHBird ChihweiLHBird force-pushed the zhiwei/validate-default-not-in-enum branch from 2bd53a9 to f616c1f Compare April 3, 2026 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[REQ] Add validation warning when schema default value is not in enum

1 participant