Skip to content

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

Merged
schmunk42 merged 11 commits into
masterfrom
feature/drop-invalid-schema-defaults
Nov 26, 2026
Merged

schmunk42 merged 11 commits into
masterfrom
feature/drop-invalid-schema-defaults

Conversation

@eluhr

@eluhr eluhr commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

What

InvalidDefaultSchemaDecorator decorates api_platform.json_schema.schema_factory and removes a default from the emitted JSON/OpenAPI schemas when the schema it sits in rejects it (pattern, minLength, maxLength, enum). API Platform derives default from the PHP property initializer, so private string $slug = ''; next to a slug #[Assert\Regex] yields default: "" that its own pattern rejects, and form clients show a validation error before anyone typed.

Switched by schema_default_cleanup.enabled, on by default. Released as 0.7.0 (minor), because the emitted schema changes for every consumer.

Review follow-ups in this PR

The branch was brought up to date with master by a merge commit (no rebase); the original commit is unchanged. On top of it:

  • The cleanup descends into items (single schema and tuple form), prefixItems and additionalProperties, and checks the default of every visited sub-schema, allOf/anyOf/oneOf branches and the definition root included.
  • A pattern that already contains an escaped \/ is no longer escaped twice (it did not compile and silently kept its default).
  • default: null is recognised and judged by enum only: it is removed when an enum exists that does not contain null.
  • $ in a pattern matches only at the very end (ECMA reading), not before a trailing newline.
  • Each definition is cleaned once per build instead of on every buildSchema() call (was quadratic in the number of definitions). Cleaned definition objects are remembered in a WeakMap per definitions object; replaced definitions and plain-array definitions are still cleaned.
  • A default next to a $ref is kept, because the reference is not resolved.
  • Tests build the schema in the shape API Platform delivers (ArrayObject definitions and properties, the stub writes into the schema it is handed); the "PHP cannot compile the pattern" test uses [^].
  • The comment in config/services_schema_default_cleanup.yaml describes the real decoration order.
  • README documents the option; CHANGELOG has the dated 0.7.0 section under ### Changed.

What consumers must do

Check forms that rely on a default that is now removed, among them default: null on nullable enum properties whose enum does not contain null, or switch the cleanup off:

dmstr_api_platform_utils:
    schema_default_cleanup:
        enabled: false

Known limits

  • $ref is not resolved; a default next to a $ref is not checked.
  • A pattern PHP cannot compile keeps its default.
  • ECMA-262 regular expressions are not fully translated to PCRE.
  • Server-side validation is unchanged; only the emitted schema changes.

Tests

85 tests, 213 assertions on PHP 8.4. Every fix has a test that fails without it.

🤖 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
schmunk42 and others added 10 commits November 26, 2026 08:59
Resolve the conflicts in CHANGELOG.md and ApiPlatformUtilsExtension.php
by keeping both sides: relation_labels from master and
schema_default_cleanup from this branch.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…rs [*]

Definitions and properties are ArrayObjects and the stubbed factory writes
into the schema it is handed, like the real one. The uncompilable-pattern
test uses [^], which ECMA accepts and PCRE rejects.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
No behaviour change: properties children are still checked and descended
into, allOf/anyOf/oneOf branches are still only descended into.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The cleanup descends into items (single schema and tuple form),
prefixItems and additionalProperties, and checks the default of every
visited sub-schema, combinator branches and the definition root included.
A default next to a $ref is kept, because the reference is not resolved.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A pattern that already contained \/ was escaped twice, no longer
compiled, and silently kept its invalid default.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
isset() skipped every null default. Presence is now tested with
array_key_exists/offsetExists; a null default is judged by enum only.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
PCRE lets $ match before a trailing newline, ECMA-262 does not. The D
modifier makes the check follow the ECMA reading.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Every buildSchema() call walked all definitions collected so far, which
is quadratic over one OpenAPI build. Cleaned definition objects are now
remembered in a WeakMap per definitions object; replaced definitions and
plain-array definitions are still cleaned.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…up [*]

A higher decoration priority sits further inside: the cleanup (5) wraps
the relation field decorator (10) and is wrapped by the Hydra, HAL and
JSON:API schema factories (0).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ngelog [*]

README: option, default, checked keywords, descent and known limits.
CHANGELOG: the entry moves to Changed and names what consumers must do.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@schmunk42 schmunk42 changed the title fix(openapi): drop property defaults their own schema rejects feat(openapi): drop schema defaults their own schema rejects Nov 26, 2026
@schmunk42
schmunk42 merged commit 1bc2d3c into master Nov 26, 2026
2 checks passed
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.

2 participants