Skip to content

Commit abe0438

Browse files
authored
fix(jsonschema): make all required properties optional in PATCH operation with 'json' format (#7398)
1 parent 00fbdf7 commit abe0438

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/JsonSchema/SchemaFactory.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
4343
use ResourceMetadataTrait;
4444
use SchemaUriPrefixTrait;
4545

46+
private const JSON_MERGE_PATCH_SCHEMA_POSTFIX = '.jsonMergePatch';
47+
4648
private ?SchemaFactoryInterface $schemaFactory = null;
4749
// Edge case where the related resource is not readable (for example: NotExposed) but we have groups to read the whole related object
4850
public const OPENAPI_DEFINITION_NAME = 'openapi_definition_name';
@@ -92,6 +94,11 @@ public function buildSchema(string $className, string $format = 'json', string $
9294
return $schema;
9395
}
9496

97+
$isJsonMergePatch = 'json' === $format && 'PATCH' === $method && Schema::TYPE_INPUT === $type;
98+
if ($isJsonMergePatch) {
99+
$definitionName .= self::JSON_MERGE_PATCH_SCHEMA_POSTFIX;
100+
}
101+
95102
if (!isset($schema['$ref']) && !isset($schema['type'])) {
96103
$ref = $this->getSchemaUriPrefix($version).$definitionName;
97104
if ($forceCollection || ('POST' !== $method && $operation instanceof CollectionOperationInterface)) {
@@ -141,7 +148,7 @@ public function buildSchema(string $className, string $format = 'json', string $
141148
}
142149

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

tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ public function testExecuteWithJsonldTypeInput(): void
121121
$this->assertStringNotContainsString('@type', $result);
122122
}
123123

124+
public function testExecuteWithJsonMergePatchTypeInput(): void
125+
{
126+
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => '_api_/dummies/{id}{._format}_patch', '--format' => 'json', '--type' => 'input']);
127+
$result = $this->tester->getDisplay();
128+
$json = json_decode($result, associative: true);
129+
130+
$this->assertArrayNotHasKey('Dummy', $json['definitions']);
131+
$this->assertArrayHasKey('Dummy.jsonMergePatch', $json['definitions']);
132+
$this->assertArrayNotHasKey('required', $json['definitions']['Dummy.jsonMergePatch']);
133+
}
134+
124135
/**
125136
* Test issue #5998.
126137
*/

0 commit comments

Comments
 (0)