Skip to content

Commit 30ce240

Browse files
committed
fix(jsonschema): make all required properties optional in PATCH operation with 'json' format
1 parent 40fd600 commit 30ce240

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/JsonSchema/SchemaFactory.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ApiPlatform\Metadata\CollectionOperationInterface;
1919
use ApiPlatform\Metadata\HttpOperation;
2020
use ApiPlatform\Metadata\Operation;
21+
use ApiPlatform\Metadata\Patch;
2122
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2223
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2324
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
@@ -33,6 +34,9 @@
3334
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
3435
{
3536
use ResourceMetadataTrait;
37+
38+
private const PATCH_SCHEMA_POSTFIX = '.patch';
39+
3640
private ?TypeFactoryInterface $typeFactory = null;
3741
private ?SchemaFactoryInterface $schemaFactory = null;
3842
// 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 $
8892
return $schema;
8993
}
9094

95+
$isJsonMergePatch = 'json' === $format && $operation instanceof Patch && Schema::TYPE_INPUT === $type;
96+
97+
if ($isJsonMergePatch) {
98+
$definitionName .= self::PATCH_SCHEMA_POSTFIX;
99+
}
100+
91101
if (!isset($schema['$ref']) && !isset($schema['type'])) {
92102
$ref = Schema::VERSION_OPENAPI === $version ? '#/components/schemas/'.$definitionName : '#/definitions/'.$definitionName;
93103
if ($forceCollection || ('POST' !== $method && $operation instanceof CollectionOperationInterface)) {
@@ -136,7 +146,7 @@ public function buildSchema(string $className, string $format = 'json', string $
136146
}
137147

138148
$normalizedPropertyName = $this->nameConverter ? $this->nameConverter->normalize($propertyName, $inputOrOutputClass, $format, $serializerContext) : $propertyName;
139-
if ($propertyMetadata->isRequired()) {
149+
if ($propertyMetadata->isRequired() && !$isJsonMergePatch) {
140150
$definition['required'][] = $normalizedPropertyName;
141151
}
142152

0 commit comments

Comments
 (0)