Skip to content

feat(jsonschema): generate schemas with symfony/json-schema - #8683

Open
soyuka wants to merge 22 commits into
api-platform:mainfrom
soyuka:feat/json-schema-kernel
Open

soyuka wants to merge 22 commits into
api-platform:mainfrom
soyuka:feat/json-schema-kernel

Conversation

@soyuka

@soyuka soyuka commented Oct 14, 2026

Copy link
Copy Markdown
Member
Q A
Branch? main
Tickets ∅
License MIT
Doc PR todo

Follows symfony/symfony#66685, the JsonSchema component got merged in 8.2 and it was seeded from our json-schema engine. This PR lets API Platform generate its schemas through Symfony\Component\JsonSchema\SchemaGenerator instead of our own SchemaFactory, without Operation leaking into the generation.

It's opt-in for 5.1:

api_platform:
    json_schema:
        generator: true

symfony/json-schema needs PHP 8.4 so it's only suggested, the old factory stays the default and enabling the option without the component throws a LogicException.

How it plugs in:

  • GeneratorSchemaFactory replaces the base api_platform.json_schema.schema_factory, the Hydra/HAL/JSON:API/MCP decorators stay on top.
  • The generator is built once, per-call state (schema type, format, partial, version, property options, declared groups...) travels in Configuration::$context.
  • Our extensions: resource as IRI resolver, ApiProperty provider/enricher, partial update and nested operation definition processors, Hydra item wrapping as a definition processor. Names come from ApiPlatformDefinitionPolicy so they don't change (declared group order is kept, .partial, .jsonMergePatch, _noid).
  • App definition processors join our generator only with #[AsJsonSchemaDefinitionProcessor], FrameworkBundle 8.2 autoconfigures the bare Symfony interfaces for its own generator ([FrameworkBundle][JsonSchema] Add JsonSchemaBundle symfony/symfony#66929) and we don't want those running on resources.

Tests: GoldenSchemaTest pins 46 cases (json, jsonld, jsonhal, jsonapi, mcp, input/output, partial, merge-patch, dialects...) and asserts the old buildSchema() and the new generate() produce the same document. A new phpunit-json-schema-generator CI job installs symfony/json-schema:8.2.x-dev and runs the goldens plus the JSON Schema / OpenAPI functional tests with the generator on.

Draft until these 5.0 fixes found along the way merge up, the goldens skip around them: #8643, #8644, #8652, #8653. Laravel wiring is a follow-up.

@soyuka soyuka added this to the 5.1 milestone Nov 24, 2026
@soyuka
soyuka force-pushed the feat/json-schema-kernel branch from 3842104 to c9ed87d Compare December 1, 2026 14:04
@soyuka
soyuka marked this pull request as ready for review December 1, 2026 14:04
soyuka added 22 commits December 1, 2026 16:42
Work in progress for symfony/symfony#64279. Golden tests compare
buildSchema() with generate() on the spiked component
(json-schema-component @ 1e167b63d5f), which has to be autoloaded
locally for now.
Nested resources get the Hydra item wrapper and their operation
description from definition processors, the Hydra decorator keeps
the root envelope. jsonld_nested_output_dto stays skipped until the
5.0 nested output DTO fix is merged up.
api_platform.json_schema.generator (off by default) swaps the base
schema factory for the Symfony JsonSchema generator. Definition
processors are autoconfigured, the Hydra item processor is tagged.
Resolvers, policies and processors receive the object type instead of the class name. CI requires the released component.
api-platform#8643, api-platform#8652 and api-platform#8653 are merged up, so their cases run on both paths again. The JSON:API and HAL goldens follow the schema fixes merged on main.
The property metadata describes array shapes with properties and required keys holding unresolved class references, which overrode the generated ones.
A service id or a callable resolves its groups at validation time, so the generator path describes the Default group like the base path, for the property metadata and for the definition name.
Symfony's discriminator support inlines an anyOf without a named definition nor an OpenAPI discriminator, so the generator path keeps it off and ports the base path behaviour in a definition processor: oneOf over the subtypes, an own branch for mapped parents, type property enums across nested maps, gated by resource polymorphism. Subtypes described at the root are named like nested definitions, as on the base path.
Hydra item base schemas referenced by nested definitions are only added when the generator is enabled, the collection description is back to main, and the generator-only item definition processor is registered with the generator for JSON-LD.
The component supports PHP 8.2, so every job installs it and runs the goldens on both paths; the generator job only keeps its generator-mode step. Hydra needs api-platform/json-schema 5.1 for the definition policy.
Keyed keyword lists instead of array_flip per property, a method instead of a closure per property, shared format and definition name constants, and a test cache directory per schema generator mode.
@soyuka
soyuka force-pushed the feat/json-schema-kernel branch from c9ed87d to fa442b0 Compare December 1, 2026 15:52
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