Skip to content

Commit da2324e

Browse files
authored
fix(jsonschema): move jsonMergePatch postfix to DefinitionNameFactory (#7510)
1 parent c08a0ed commit da2324e

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/JsonSchema/DefinitionNameFactory.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ final class DefinitionNameFactory implements DefinitionNameFactoryInterface
2222
use ResourceClassInfoTrait;
2323

2424
private const GLUE = '.';
25+
private const JSON_MERGE_PATCH_SCHEMA_POSTFIX = 'jsonMergePatch';
26+
2527
private array $prefixCache = [];
2628

2729
public function __construct(private ?array $distinctFormats = null)
@@ -50,8 +52,9 @@ public function create(string $className, string $format = 'json', ?string $inpu
5052
// TODO: remove in 5.0
5153
$v = $this->distinctFormats ? ($this->distinctFormats[$format] ?? false) : true;
5254

53-
if ('json' !== $format && $v) {
55+
if (!\in_array($format, ['json', 'merge-patch+json'], true) && $v) {
5456
// JSON is the default, and so isn't included in the definition name
57+
// JSON merge patch is postfixed at the end
5558
$prefix .= self::GLUE.$format;
5659
}
5760

@@ -67,6 +70,10 @@ public function create(string $className, string $format = 'json', ?string $inpu
6770
$name .= '_noid';
6871
}
6972

73+
if ('merge-patch+json' === $format) {
74+
$name .= self::GLUE.self::JSON_MERGE_PATCH_SCHEMA_POSTFIX;
75+
}
76+
7077
return $this->encodeDefinitionName($name);
7178
}
7279

src/JsonSchema/SchemaFactory.php

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

46-
private const JSON_MERGE_PATCH_SCHEMA_POSTFIX = '.jsonMergePatch';
47-
4846
private ?SchemaFactoryInterface $schemaFactory = null;
4947
// Edge case where the related resource is not readable (for example: NotExposed) but we have groups to read the whole related object
5048
public const OPENAPI_DEFINITION_NAME = 'openapi_definition_name';
@@ -83,7 +81,6 @@ public function buildSchema(string $className, string $format = 'json', string $
8381

8482
$validationGroups = $operation ? $this->getValidationGroups($operation) : [];
8583
$version = $schema->getVersion();
86-
$definitionName = $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
8784
$method = $operation instanceof HttpOperation ? $operation->getMethod() : 'GET';
8885
if (!$operation) {
8986
$method = Schema::TYPE_INPUT === $type ? 'POST' : 'GET';
@@ -95,9 +92,12 @@ public function buildSchema(string $className, string $format = 'json', string $
9592
}
9693

9794
$isJsonMergePatch = 'json' === $format && 'PATCH' === $method && Schema::TYPE_INPUT === $type;
98-
if ($isJsonMergePatch) {
99-
$definitionName .= self::JSON_MERGE_PATCH_SCHEMA_POSTFIX;
100-
}
95+
$definitionFormat = match (true) {
96+
default => $format,
97+
$isJsonMergePatch => 'merge-patch+json',
98+
};
99+
100+
$definitionName = $this->definitionNameFactory->create($className, $definitionFormat, $inputOrOutputClass, $operation, $serializerContext);
101101

102102
if (!isset($schema['$ref']) && !isset($schema['type'])) {
103103
$ref = $this->getSchemaUriPrefix($version).$definitionName;

0 commit comments

Comments
 (0)