|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Mcp\JsonSchema; |
| 15 | + |
| 16 | +use ApiPlatform\JsonSchema\Schema; |
| 17 | +use ApiPlatform\JsonSchema\SchemaFactoryInterface; |
| 18 | +use ApiPlatform\Metadata\Operation; |
| 19 | + |
| 20 | +/** |
| 21 | + * Wraps a SchemaFactoryInterface and flattens the resulting schema |
| 22 | + * into a MCP-compliant structure: no $ref, no allOf, no definitions. |
| 23 | + * |
| 24 | + * @experimental |
| 25 | + */ |
| 26 | +final class SchemaFactory implements SchemaFactoryInterface |
| 27 | +{ |
| 28 | + public function __construct( |
| 29 | + private readonly SchemaFactoryInterface $decorated, |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + public function buildSchema(string $className, string $format = 'json', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema |
| 34 | + { |
| 35 | + $schema = $this->decorated->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection); |
| 36 | + |
| 37 | + $definitions = []; |
| 38 | + foreach ($schema->getDefinitions() as $key => $definition) { |
| 39 | + $definitions[$key] = $definition instanceof \ArrayObject ? $definition->getArrayCopy() : (array) $definition; |
| 40 | + } |
| 41 | + |
| 42 | + $rootKey = $schema->getRootDefinitionKey(); |
| 43 | + if (null !== $rootKey) { |
| 44 | + $root = $definitions[$rootKey] ?? []; |
| 45 | + } else { |
| 46 | + // Collection schemas (and others) put allOf/type directly on the root |
| 47 | + $root = $schema->getArrayCopy(false); |
| 48 | + } |
| 49 | + |
| 50 | + $flat = self::resolveNode($root, $definitions); |
| 51 | + |
| 52 | + $flatSchema = new Schema(Schema::VERSION_JSON_SCHEMA); |
| 53 | + unset($flatSchema['$schema']); |
| 54 | + foreach ($flat as $key => $value) { |
| 55 | + $flatSchema[$key] = $value; |
| 56 | + } |
| 57 | + |
| 58 | + return $flatSchema; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Recursively resolve $ref, allOf, and nested structures into a flat schema node. |
| 63 | + * |
| 64 | + * @param array $resolving Tracks the current $ref resolution chain to detect circular references |
| 65 | + */ |
| 66 | + public static function resolveNode(array|\ArrayObject $node, array $definitions, array &$resolving = []): array |
| 67 | + { |
| 68 | + if ($node instanceof \ArrayObject) { |
| 69 | + $node = $node->getArrayCopy(); |
| 70 | + } |
| 71 | + |
| 72 | + if (isset($node['$ref'])) { |
| 73 | + $refKey = str_replace('#/definitions/', '', $node['$ref']); |
| 74 | + if (!isset($definitions[$refKey]) || isset($resolving[$refKey])) { |
| 75 | + return ['type' => 'object']; |
| 76 | + } |
| 77 | + $resolving[$refKey] = true; |
| 78 | + $resolved = self::resolveNode($definitions[$refKey], $definitions, $resolving); |
| 79 | + unset($resolving[$refKey]); |
| 80 | + |
| 81 | + return $resolved; |
| 82 | + } |
| 83 | + |
| 84 | + if (isset($node['allOf'])) { |
| 85 | + $merged = ['type' => 'object', 'properties' => []]; |
| 86 | + $requiredSets = []; |
| 87 | + foreach ($node['allOf'] as $entry) { |
| 88 | + $resolved = self::resolveNode($entry, $definitions, $resolving); |
| 89 | + if (isset($resolved['properties'])) { |
| 90 | + foreach ($resolved['properties'] as $k => $v) { |
| 91 | + $merged['properties'][$k] = $v; |
| 92 | + } |
| 93 | + } |
| 94 | + if (isset($resolved['required'])) { |
| 95 | + $requiredSets[] = $resolved['required']; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if ($requiredSets) { |
| 100 | + $merged['required'] = array_merge(...$requiredSets); |
| 101 | + } |
| 102 | + if ([] === $merged['properties']) { |
| 103 | + unset($merged['properties']); |
| 104 | + } |
| 105 | + if (isset($node['description'])) { |
| 106 | + $merged['description'] = $node['description']; |
| 107 | + } |
| 108 | + |
| 109 | + return self::resolveDeep($merged, $definitions, $resolving); |
| 110 | + } |
| 111 | + |
| 112 | + if (!isset($node['type'])) { |
| 113 | + $node['type'] = 'object'; |
| 114 | + } |
| 115 | + |
| 116 | + return self::resolveDeep($node, $definitions, $resolving); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Recursively resolve nested properties and array items. |
| 121 | + */ |
| 122 | + private static function resolveDeep(array $node, array $definitions, array &$resolving): array |
| 123 | + { |
| 124 | + if (isset($node['items'])) { |
| 125 | + $node['items'] = self::resolveNode( |
| 126 | + $node['items'] instanceof \ArrayObject ? $node['items']->getArrayCopy() : $node['items'], |
| 127 | + $definitions, |
| 128 | + $resolving, |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + if (isset($node['properties']) && \is_array($node['properties'])) { |
| 133 | + foreach ($node['properties'] as $propName => $propSchema) { |
| 134 | + $node['properties'][$propName] = self::resolveNode( |
| 135 | + $propSchema instanceof \ArrayObject ? $propSchema->getArrayCopy() : $propSchema, |
| 136 | + $definitions, |
| 137 | + $resolving, |
| 138 | + ); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return $node; |
| 143 | + } |
| 144 | +} |
0 commit comments