Skip to content

Commit

Permalink
fix(jsonschema): make all required properties optional in PATCH opera…
Browse files Browse the repository at this point in the history
…tion with 'json' format (#6394)
  • Loading branch information
ttskch authored Jun 28, 2024
1 parent 57f930c commit 842091d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
Expand All @@ -33,6 +34,9 @@
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
{
use ResourceMetadataTrait;

private const PATCH_SCHEMA_POSTFIX = '.patch';

private ?TypeFactoryInterface $typeFactory = null;
private ?SchemaFactoryInterface $schemaFactory = null;
// Edge case where the related resource is not readable (for example: NotExposed) but we have groups to read the whole related object
Expand Down Expand Up @@ -88,6 +92,12 @@ public function buildSchema(string $className, string $format = 'json', string $
return $schema;
}

$isJsonMergePatch = 'json' === $format && $operation instanceof Patch && Schema::TYPE_INPUT === $type;

if ($isJsonMergePatch) {
$definitionName .= self::PATCH_SCHEMA_POSTFIX;
}

if (!isset($schema['$ref']) && !isset($schema['type'])) {
$ref = Schema::VERSION_OPENAPI === $version ? '#/components/schemas/'.$definitionName : '#/definitions/'.$definitionName;
if ($forceCollection || ('POST' !== $method && $operation instanceof CollectionOperationInterface)) {
Expand Down Expand Up @@ -136,7 +146,7 @@ public function buildSchema(string $className, string $format = 'json', string $
}

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

Expand Down

0 comments on commit 842091d

Please sign in to comment.