|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Netgen\ApiPlatformExtras\ApiPlatform\JsonSchema; |
| 6 | + |
| 7 | +use ApiPlatform\JsonSchema\Schema; |
| 8 | +use ApiPlatform\JsonSchema\SchemaFactoryInterface; |
| 9 | +use ApiPlatform\JsonSchema\SchemaUriPrefixTrait; |
| 10 | +use ApiPlatform\Metadata\Operation; |
| 11 | +use ApiPlatform\Metadata\Patch; |
| 12 | +use ApiPlatform\Metadata\Post; |
| 13 | +use ApiPlatform\Metadata\Put; |
| 14 | +use ArrayObject; |
| 15 | + |
| 16 | +use function in_array; |
| 17 | +use function is_string; |
| 18 | +use function str_replace; |
| 19 | + |
| 20 | +final class SchemaFactoryDecorator implements SchemaFactoryInterface |
| 21 | +{ |
| 22 | + use SchemaUriPrefixTrait; |
| 23 | + |
| 24 | + private const array SCHEMA_LOGICAL_OPERATORS = ['anyOf', 'oneOf', 'allOf']; |
| 25 | + |
| 26 | + private const string JSONLD_INPUT_OBJECT_PROPERTY_NAME = '@id'; |
| 27 | + |
| 28 | + private const array JSONLD_INPUT_OBJECT_PROPERTY = [ |
| 29 | + 'type' => 'string', |
| 30 | + 'format' => 'iri-reference', |
| 31 | + 'example' => 'https://example.com/', |
| 32 | + ]; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + private SchemaFactoryInterface $decorated, |
| 36 | + ) {} |
| 37 | + |
| 38 | + /** @param array<mixed> $serializerContext */ |
| 39 | + 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 |
| 40 | + { |
| 41 | + $schema = $this->decorated->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection); |
| 42 | + $version = $schema->getVersion(); |
| 43 | + $schemaPrefix = $this->getSchemaUriPrefix($version); |
| 44 | + $currentReference = $schema['$ref'] ?? null; |
| 45 | + |
| 46 | + if ( |
| 47 | + is_string($currentReference) |
| 48 | + && $type === Schema::TYPE_INPUT |
| 49 | + && $operation instanceof Operation |
| 50 | + && in_array($operation::class, [Put::class, Post::class, Patch::class], true) |
| 51 | + ) { |
| 52 | + $this->ensureJsonldInputPropertyForInputSchemas($currentReference, $schemaPrefix, $schema->getDefinitions()); |
| 53 | + } |
| 54 | + |
| 55 | + return $schema; |
| 56 | + } |
| 57 | + |
| 58 | + /** @param ArrayObject<string, mixed> $definitions */ |
| 59 | + private function ensureJsonldInputPropertyForInputSchemas(string $reference, string $schemaPrefix, ArrayObject $definitions): void |
| 60 | + { |
| 61 | + $definitionName = str_replace($schemaPrefix, '', $reference); |
| 62 | + |
| 63 | + foreach ($definitions[$definitionName]['properties'] ?? [] as $property) { |
| 64 | + if (isset($property['type'])) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + if (isset($property['$ref'])) { |
| 69 | + $this->addJsonldInputProperty( |
| 70 | + $definitions, |
| 71 | + $schemaPrefix, |
| 72 | + $property['$ref'], |
| 73 | + ); |
| 74 | + |
| 75 | + break; |
| 76 | + } |
| 77 | + |
| 78 | + foreach (self::SCHEMA_LOGICAL_OPERATORS as $operator) { |
| 79 | + if (!isset($property[$operator])) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + foreach ($property[$operator] as $subschema) { |
| 84 | + if (!isset($subschema['$ref'])) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + $this->addJsonldInputProperty( |
| 89 | + $definitions, |
| 90 | + $schemaPrefix, |
| 91 | + $subschema['$ref'], |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** @param ArrayObject<string, mixed> $definitions */ |
| 99 | + private function addJsonldInputProperty( |
| 100 | + ArrayObject $definitions, |
| 101 | + string $schemaPrefix, |
| 102 | + string $ref, |
| 103 | + ): void { |
| 104 | + $definitionKey = str_replace($schemaPrefix, '', $ref); |
| 105 | + |
| 106 | + $definitions[$definitionKey]['properties'][self::JSONLD_INPUT_OBJECT_PROPERTY_NAME] |
| 107 | + ??= self::JSONLD_INPUT_OBJECT_PROPERTY; |
| 108 | + } |
| 109 | +} |
0 commit comments