Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e8d8219
feat(jsonschema): adapter over Symfony JsonSchema
soyuka Oct 6, 2026
49cc0a3
feat(jsonschema): JSON-LD through the generator
soyuka Oct 6, 2026
c460a12
feat(jsonschema): HAL, JSON:API and MCP through the generator
soyuka Oct 7, 2026
ed3f033
feat(symfony): opt-in json schema generator
soyuka Oct 7, 2026
3c65435
refactor(jsonschema): follow JsonSchema spike API
soyuka Oct 7, 2026
9404767
refactor(jsonschema): drop callable validation groups
soyuka Oct 8, 2026
799d197
refactor(jsonschema): follow JsonSchema extension namespaces
soyuka Oct 14, 2026
3967628
fix(jsonschema): keep declared group order in names
soyuka Oct 14, 2026
aa124e8
refactor(jsonschema): carry request state in configuration context
soyuka Oct 14, 2026
95790b1
refactor(jsonschema): build the generator once
soyuka Oct 14, 2026
737ad7c
feat(symfony): opt in json schema definition processors
soyuka Oct 14, 2026
d6b7db4
ci: run the json schema generator against symfony/json-schema
soyuka Oct 14, 2026
4154424
fix(jsonschema): follow empty mixed schema and object schemas
soyuka Oct 14, 2026
95dc57a
fix(jsonschema): follow symfony/json-schema 8.2 API
soyuka Dec 1, 2026
fa094a5
test(jsonschema): refresh goldens after the 5.0 fixes
soyuka Dec 1, 2026
e1034dc
fix(jsonschema): keep generated array shape properties
soyuka Dec 1, 2026
0a043fc
refactor(jsonschema): build root documentation once
soyuka Dec 1, 2026
d940c5d
fix(jsonschema): ignore validation group generators
soyuka Dec 1, 2026
b5db7f9
feat(jsonschema): describe discriminator maps on the generator path
soyuka Dec 1, 2026
5d5052e
fix(hydra): keep the default schema path unchanged
soyuka Dec 1, 2026
59ce060
chore: require symfony/json-schema for development
soyuka Dec 1, 2026
fa442b0
refactor(jsonschema): tidy the generator adapter
soyuka Dec 1, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,51 @@ jobs:
- name: Run PHPUnit tests
run: vendor/bin/phpunit

phpunit-json-schema-generator:
name: PHPUnit (PHP ${{ matrix.php }}) (Symfony JsonSchema generator)
runs-on: ubuntu-latest
timeout-minutes: 20
strategy:
matrix:
php:
- '8.5'
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- name: Setup PHP
uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # 2.37.2
with:
php-version: ${{ matrix.php }}
tools: pecl, composer:2.9.8
extensions: intl, bcmath, curl, openssl, mbstring
coverage: none
ini-values: memory_limit=-1
- name: Get composer cache directory
id: composercache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-composer-json-schema-generator-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-json-schema-generator-
- name: Update project dependencies
run: |
composer global require "soyuka/pmu:$PMU_VERSION"
composer global config allow-plugins.soyuka/pmu true --no-interaction
composer global link .
- name: Remove cache
run: rm -Rf tests/Fixtures/app/var/cache/*
- name: Run JSON Schema tests (generator enabled)
env:
JSON_SCHEMA_GENERATOR: '1'
run: |
vendor/bin/phpunit tests/Functional/JsonSchema/JsonSchemaTest.php
vendor/bin/phpunit tests/Functional/JsonSchema/JsonLdJsonSchemaTest.php
vendor/bin/phpunit tests/Functional/OpenApiTest.php
vendor/bin/phpunit tests/Functional/DiscriminatorMapOpenApiTest.php

phpunit-symfony-lowest:
name: PHPUnit (PHP ${{ matrix.php }}) (Symfony lowest)
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
"symfony/framework-bundle": "^7.4 || ^8.0",
"symfony/http-client": "^7.4 || ^8.0",
"symfony/intl": "^7.4 || ^8.0",
"symfony/json-schema": "^8.2",
"symfony/json-streamer": "^7.4 || ^8.0",
"symfony/maker-bundle": "^1.24",
"symfony/mcp-bundle": "^0.15",
Expand Down Expand Up @@ -211,6 +212,7 @@
"symfony/uid": "To support Symfony UUID/ULID identifiers.",
"symfony/messenger": "To support messenger integration.",
"symfony/web-profiler-bundle": "To use the data collector.",
"symfony/json-schema": "To generate JSON Schemas with the Symfony JsonSchema component (opt-in, api_platform.json_schema.generator).",
"symfony/json-streamer": "To use the JSON Streamer component.",
"webonyx/graphql-php": "To support GraphQL."
},
Expand Down
55 changes: 55 additions & 0 deletions src/Hydra/JsonSchema/ItemDefinitionProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Hydra\JsonSchema;

use ApiPlatform\JsonSchema\Generator\ApiPlatformDefinitionPolicy;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
use Symfony\Component\JsonSchema\Configuration;
use Symfony\Component\JsonSchema\DefinitionParent;
use Symfony\Component\JsonSchema\DefinitionProcessor\DefinitionProcessorInterface;
use Symfony\Component\JsonSchema\SchemaDocument;
use Symfony\Component\TypeInfo\Type\GenericType;
use Symfony\Component\TypeInfo\Type\ObjectType;

final class ItemDefinitionProcessor implements DefinitionProcessorInterface
{
use ResourceClassInfoTrait;

public function __construct(
ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory,
private readonly ApiPlatformDefinitionPolicy $definitionPolicy,
) {
$this->resourceMetadataFactory = $resourceMetadataFactory;
}

public function process(array $definition, ObjectType|GenericType $type, Configuration $config, SchemaDocument $document, ?DefinitionParent $parent): array
{
$class = ($type instanceof GenericType ? $type->getWrappedType() : $type)->getClassName();

if (null === $parent || 'jsonld' !== $config->format || !$this->isResourceClass($class)) {
return $definition;
}

$baseName = $this->definitionPolicy->resolveGenId($parent) ? SchemaFactory::ITEM_BASE_SCHEMA_NAME : SchemaFactory::ITEM_WITHOUT_ID_BASE_SCHEMA_NAME;
$item = ['allOf' => [['$ref' => $config->dialect->refPath.$baseName], $definition]];

if (isset($definition['description'])) {
$item['description'] = $definition['description'];
unset($item['allOf'][1]['description']);
}

return $item;
}
}
22 changes: 20 additions & 2 deletions src/Hydra/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
use ResourceMetadataTrait;
use SchemaUriPrefixTrait;

private const ITEM_BASE_SCHEMA_NAME = 'HydraItemBaseSchema';
private const ITEM_WITHOUT_ID_BASE_SCHEMA_NAME = 'HydraItemBaseSchemaWithoutId';
public const ITEM_BASE_SCHEMA_NAME = 'HydraItemBaseSchema';
public const ITEM_WITHOUT_ID_BASE_SCHEMA_NAME = 'HydraItemBaseSchemaWithoutId';
private const COLLECTION_BASE_SCHEMA_NAME_NO_PAGINATION = 'HydraCollectionBaseSchemaNoPagination';
private const COLLECTION_BASE_SCHEMA_NAME = 'HydraCollectionBaseSchema';

Expand Down Expand Up @@ -89,6 +89,7 @@ public function __construct(
private readonly array $defaultContext = [],
private ?DefinitionNameFactoryInterface $definitionNameFactory = null,
?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null,
private readonly bool $addReferencedItemBaseSchemas = false,
) {
if (!$definitionNameFactory) {
$this->definitionNameFactory = new DefinitionNameFactory();
Expand Down Expand Up @@ -131,6 +132,9 @@ public function buildSchema(string $className, string $format = 'jsonld', string
$definitions = $schema->getDefinitions();
$prefix = $this->getSchemaUriPrefix($schema->getVersion());
$collectionKey = $schema->getItemsDefinitionKey();
if ($this->addReferencedItemBaseSchemas) {
$this->addReferencedItemBaseSchemas($definitions, $prefix);
}

if (!$collectionKey) {
$definitionName = $schema->getRootDefinitionKey() ?? $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
Expand Down Expand Up @@ -268,6 +272,20 @@ public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
}
}

private function addReferencedItemBaseSchemas(\ArrayObject $definitions, string $prefix): void
{
$references = [];
foreach ($definitions as $definition) {
$references[$definition['allOf'][0]['$ref'] ?? ''] = true;
}

foreach ([self::ITEM_BASE_SCHEMA_NAME => self::ITEM_BASE_SCHEMA_WITH_ID, self::ITEM_WITHOUT_ID_BASE_SCHEMA_NAME => self::ITEM_BASE_SCHEMA_WITHOUT_ID] as $name => $baseSchema) {
if (isset($references[$prefix.$name]) && !isset($definitions[$name])) {
$definitions[$name] = $baseSchema;
}
}
}

private function decorateItemDefinition(string $definitionName, \ArrayObject $definitions, string $prefix, string $type, ?array $serializerContext): void
{
if (!isset($definitions[$definitionName])) {
Expand Down
5 changes: 4 additions & 1 deletion src/Hydra/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"api-platform/documentation": "^5.0",
"api-platform/metadata": "^5.1",
"api-platform/jsonld": "^5.1",
"api-platform/json-schema": "^5.0",
"api-platform/json-schema": "^5.1",
"api-platform/serializer": "^5.0",
"symfony/web-link": "^7.4 || ^8.0",
"symfony/type-info": "^7.4 || ^8.0"
Expand All @@ -42,6 +42,9 @@
"phpspec/prophecy-phpunit": "^2.2",
"phpunit/phpunit": "^11.5 || ^12.2 || ^13.0"
},
"suggest": {
"symfony/json-schema": "To generate JSON Schemas with the Symfony JsonSchema component (opt-in, api_platform.json_schema.generator)."
},
"autoload": {
"psr-4": {
"ApiPlatform\\Hydra\\": ""
Expand Down
Loading
Loading