-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
[REQ] Add validation warning when schema default value is not in enum #23428
Description
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: httpCommand line used for generation
java -jar openapi-generator-cli.jar validate -i spec.yaml --recommend
Steps to reproduce
- Create a spec with a schema that has
defaultnot inenum(as above) - Run
validate --recommend - No warning is produced about the mismatch
- Generate code (e.g., Python) — the generated default value is incorrect
Related issues/PRs
- [BUG] Invalid default enum value with Python generator #20397 — [BUG] Invalid default enum value with Python generator
- [BUG][RUBY] Integer default enum value references constant that does not exist #17218 — [BUG][RUBY] Integer default enum value references constant that does not exist
- [BUG][KOTLIN-CLIENT] OAS 3.x: Default enum value rendered without stating the enum name as query param #21437 — [BUG][KOTLIN-CLIENT] Default enum value rendered without stating the enum name
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.