Skip to content

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

@ChihweiLHBird

Description

@ChihweiLHBird
Description

When a schema has default not in enum, multiple generators produce incorrect code (e.g., Python emits unquoted defaults causing NameError). This is technically valid JSON Schema but a common spec authoring mistake.

validate --recommend should warn per occurrence, e.g.:

Warnings:
    - Schema has default value 'http' not in enum [udp]
openapi-generator version

master (7.22.0-SNAPSHOT)

OpenAPI declaration file content or url
openapi: "3.0.1"
info:
  title: Test
  version: "1.0"
paths: {}
components:
  schemas:
    Config:
      type: object
      properties:
        protocol:
          type: string
          enum: [udp, tcp]
          default: http
Command line used for generation
java -jar openapi-generator-cli.jar validate -i spec.yaml --recommend
Steps to reproduce
  1. Create a spec with a schema that has default not in enum (as above)
  2. Run validate --recommend
  3. No warning is produced about the mismatch
  4. Generate code (e.g., Python) — the generated default value is incorrect
Related issues/PRs

These are all downstream symptoms of the same root cause: specs with default ∉ enum that the validator doesn't catch.

Suggest a fix/enhancement

Add a validation rule in OpenApiEvaluator.validate() that iterates all schemas via a new ModelUtils.getAllSchemasInDocument() method (covering both component and inline path schemas) and warns when schema.getDefault() ∉ schema.getEnum(). Use the per-occurrence pattern (like unused schemas) so each occurrence gets its own warning message with the specific default value and enum list.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions