|
18 | 18 | use ApiPlatform\Metadata\CollectionOperationInterface;
|
19 | 19 | use ApiPlatform\Metadata\HttpOperation;
|
20 | 20 | use ApiPlatform\Metadata\Operation;
|
| 21 | +use ApiPlatform\Metadata\Patch; |
21 | 22 | use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
|
22 | 23 | use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
|
23 | 24 | use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
|
|
33 | 34 | final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
|
34 | 35 | {
|
35 | 36 | use ResourceMetadataTrait;
|
| 37 | + |
| 38 | + private const PATCH_SCHEMA_POSTFIX = '.patch'; |
| 39 | + |
36 | 40 | private ?TypeFactoryInterface $typeFactory = null;
|
37 | 41 | private ?SchemaFactoryInterface $schemaFactory = null;
|
38 | 42 | // Edge case where the related resource is not readable (for example: NotExposed) but we have groups to read the whole related object
|
@@ -88,6 +92,12 @@ public function buildSchema(string $className, string $format = 'json', string $
|
88 | 92 | return $schema;
|
89 | 93 | }
|
90 | 94 |
|
| 95 | + $isJsonMergePatch = 'json' === $format && $operation instanceof Patch && Schema::TYPE_INPUT === $type; |
| 96 | + |
| 97 | + if ($isJsonMergePatch) { |
| 98 | + $definitionName .= self::PATCH_SCHEMA_POSTFIX; |
| 99 | + } |
| 100 | + |
91 | 101 | if (!isset($schema['$ref']) && !isset($schema['type'])) {
|
92 | 102 | $ref = Schema::VERSION_OPENAPI === $version ? '#/components/schemas/'.$definitionName : '#/definitions/'.$definitionName;
|
93 | 103 | if ($forceCollection || ('POST' !== $method && $operation instanceof CollectionOperationInterface)) {
|
@@ -136,7 +146,7 @@ public function buildSchema(string $className, string $format = 'json', string $
|
136 | 146 | }
|
137 | 147 |
|
138 | 148 | $normalizedPropertyName = $this->nameConverter ? $this->nameConverter->normalize($propertyName, $inputOrOutputClass, $format, $serializerContext) : $propertyName;
|
139 |
| - if ($propertyMetadata->isRequired()) { |
| 149 | + if ($propertyMetadata->isRequired() && !$isJsonMergePatch) { |
140 | 150 | $definition['required'][] = $normalizedPropertyName;
|
141 | 151 | }
|
142 | 152 |
|
|
0 commit comments