Skip to content

fix(openapi): drop property defaults their own schema rejects - #13

Open
eluhr wants to merge 1 commit into
masterfrom
feature/drop-invalid-schema-defaults
Open

eluhr wants to merge 1 commit into
masterfrom
feature/drop-invalid-schema-defaults

Conversation

@eluhr

@eluhr eluhr commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Problem

API Platform derives a property's schema default from its PHP initializer. A common entity shape

#[Assert\Regex(pattern: self::SLUG_PATTERN)]
private string $slug = '';

therefore yields a schema that contradicts itself:

"slug": { "type": "string", "pattern": "^([a-z0-9]+(?:-[a-z0-9]+)*)$", "default": "" }

Form clients (the Vue admin UI with Jedison) start create forms from the schema defaults and report a validation error on a form nobody has touched yet. On one za7 stack this affected 9 properties across 7 resources (slug, key, name fields of the menu, content, MCP-endpoint and neuron-ai bundles).

Change

New InvalidDefaultSchemaDecorator (decorates api_platform.json_schema.schema_factory, priority 5) removes a default when the property schema itself rejects it:

  • pattern (ECMA pattern compiled as PCRE with /u; a pattern PHP cannot compile keeps its default rather than guessing)
  • minLength / maxLength for string defaults
  • enum

It walks properties, nested object properties and allOf/anyOf/oneOf branches, so the .jsonld read schemas (properties inside allOf[1]) are covered too. Valid defaults are untouched; server-side validation is unchanged.

On by default, since it only removes contradictions; schema_default_cleanup.enabled: false switches it off.

Verification

  • 7 new tests (decorator + DI wiring), full suite green: 64 tests.
  • Checked against the OpenAPI document of a live za7 stack: 0 self-violating defaults left, the other 135 defaults unchanged (e.g. title: "", status: "draft").

🤖 Generated with Claude Code

API Platform derives `default` from the PHP initializer, so
`private string $slug = '';` next to a slug Assert\Regex yields
`default: ""` that the property's own `pattern` rejects. Form clients
start from that default and show a validation error before anyone typed.

InvalidDefaultSchemaDecorator removes such defaults (pattern, minLength,
maxLength, enum), walking properties, nested objects and allOf/anyOf/oneOf.
On by default, switch off with schema_default_cleanup.enabled: false.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eluhr
eluhr requested a review from schmunk42 October 9, 2026 22:13
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.

1 participant