Skip to content

Commit 9d17089

Browse files
committed
Revert changes related to $serializerContext as we already have Schema::getVersion()
1 parent 25697ee commit 9d17089

File tree

4 files changed

+27
-72
lines changed

4 files changed

+27
-72
lines changed

src/JsonSchema/SchemaFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,19 @@ private function getMetadata(string $className, string $type = Schema::TYPE_OUTP
258258

259259
return [
260260
$resourceMetadata,
261-
$this->getSerializerContext($resourceMetadata, $type, $operationType, $operationName, $serializerContext),
261+
$serializerContext ?? $this->getSerializerContext($resourceMetadata, $type, $operationType, $operationName),
262262
$inputOrOutput['class'],
263263
];
264264
}
265265

266-
private function getSerializerContext(ResourceMetadata $resourceMetadata, string $type = Schema::TYPE_OUTPUT, ?string $operationType, ?string $operationName, ?array $previousSerializerContext): array
266+
private function getSerializerContext(ResourceMetadata $resourceMetadata, string $type = Schema::TYPE_OUTPUT, ?string $operationType, ?string $operationName): array
267267
{
268268
$attribute = Schema::TYPE_OUTPUT === $type ? 'normalization_context' : 'denormalization_context';
269269

270270
if (null === $operationType || null === $operationName) {
271-
return array_merge($resourceMetadata->getAttribute($attribute, []), (array) $previousSerializerContext);
271+
return $resourceMetadata->getAttribute($attribute, []);
272272
}
273273

274-
return array_merge($resourceMetadata->getTypedOperationAttribute($operationType, $operationName, $attribute, [], true), (array) $previousSerializerContext);
274+
return $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, $attribute, [], true);
275275
}
276276
}

src/JsonSchema/TypeFactory.php

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,6 @@ final class TypeFactory implements TypeFactoryInterface
2929
{
3030
use ResourceClassInfoTrait;
3131

32-
/**
33-
* This constant is to be provided as serializer context key to conditionally enable types to be generated in
34-
* a format that is compatible with OpenAPI specifications **PREVIOUS** to 3.0.
35-
*
36-
* Without this flag being set, the generated format will only be compatible with Swagger 3.0 or newer.
37-
*
38-
* Once support for OpenAPI < 3.0 is gone, this constant **WILL BE REMOVED**
39-
*
40-
* @internal Once support for OpenAPI < 3.0 is gone, this constant **WILL BE REMOVED** - do not rely on
41-
* it in downstream projects!
42-
*/
43-
public const CONTEXT_SERIALIZATION_FORMAT_OPENAPI_PRE_V3_0 = self::class.'::CONTEXT_SERIALIZATION_FORMAT_OPENAPI_PRE_V3_0';
44-
4532
/**
4633
* @var SchemaFactoryInterface|null
4734
*/
@@ -67,31 +54,19 @@ public function getType(Type $type, string $format = 'json', ?bool $readableLink
6754
$subType = $type->getCollectionValueType() ?? new Type($type->getBuiltinType(), false, $type->getClassName(), false);
6855

6956
if (null !== $keyType && Type::BUILTIN_TYPE_STRING === $keyType->getBuiltinType()) {
70-
return $this->addNullabilityToTypeDefinition(
71-
[
72-
'type' => 'object',
73-
'additionalProperties' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
74-
],
75-
$type,
76-
(array) $serializerContext
77-
);
57+
return $this->addNullabilityToTypeDefinition([
58+
'type' => 'object',
59+
'additionalProperties' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
60+
], $type, $schema);
7861
}
7962

80-
return $this->addNullabilityToTypeDefinition(
81-
[
82-
'type' => 'array',
83-
'items' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
84-
],
85-
$type,
86-
(array) $serializerContext
87-
);
63+
return $this->addNullabilityToTypeDefinition([
64+
'type' => 'array',
65+
'items' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
66+
], $type, $schema);
8867
}
8968

90-
return $this->addNullabilityToTypeDefinition(
91-
$this->makeBasicType($type, $format, $readableLink, $serializerContext, $schema),
92-
$type,
93-
(array) $serializerContext
94-
);
69+
return $this->addNullabilityToTypeDefinition($this->makeBasicType($type, $format, $readableLink, $serializerContext, $schema), $type, $schema);
9570
}
9671

9772
private function makeBasicType(Type $type, string $format = 'json', ?bool $readableLink = null, ?array $serializerContext = null, Schema $schema = null): array
@@ -169,9 +144,9 @@ private function getClassType(?string $className, string $format, ?bool $readabl
169144
*
170145
* @return array<string, mixed>
171146
*/
172-
private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type, array $serializerContext): array
147+
private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type, ?Schema $schema): array
173148
{
174-
if (\array_key_exists(self::CONTEXT_SERIALIZATION_FORMAT_OPENAPI_PRE_V3_0, $serializerContext)) {
149+
if ($schema && Schema::VERSION_SWAGGER === $schema->getVersion()) {
175150
return $jsonSchema;
176151
}
177152

src/Swagger/Serializer/DocumentationNormalizer.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,6 @@ private function getJsonSchema(bool $v3, \ArrayObject $definitions, string $reso
594594
$schema = new Schema($v3 ? Schema::VERSION_OPENAPI : Schema::VERSION_SWAGGER);
595595
$schema->setDefinitions($definitions);
596596

597-
if (!$v3) {
598-
$serializerContext = array_merge([TypeFactory::CONTEXT_SERIALIZATION_FORMAT_OPENAPI_PRE_V3_0 => null], (array) $serializerContext);
599-
}
600-
601597
$this->jsonSchemaFactory->buildSchema($resourceClass, $format, $type, $operationType, $operationName, $schema, $serializerContext, $forceCollection);
602598

603599
return $schema;
@@ -724,14 +720,7 @@ private function getFiltersParameters(bool $v3, string $resourceClass, string $o
724720
'required' => $data['required'],
725721
];
726722

727-
$type = \in_array($data['type'], Type::$builtinTypes, true)
728-
? $this->jsonSchemaTypeFactory->getType(
729-
new Type($data['type'], false, null, $data['is_collection'] ?? false),
730-
'json',
731-
null,
732-
$v3 ? null : [TypeFactory::CONTEXT_SERIALIZATION_FORMAT_OPENAPI_PRE_V3_0 => null]
733-
)
734-
: ['type' => 'string'];
723+
$type = \in_array($data['type'], Type::$builtinTypes, true) ? $this->jsonSchemaTypeFactory->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false)) : ['type' => 'string'];
735724
$v3 ? $parameter['schema'] = $type : $parameter += $type;
736725

737726
if ($v3 && isset($data['schema'])) {

tests/JsonSchema/TypeFactoryTest.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TypeFactoryTest extends TestCase
2929
public function testGetType(array $schema, Type $type): void
3030
{
3131
$typeFactory = new TypeFactory();
32-
$this->assertEquals($schema, $typeFactory->getType($type));
32+
$this->assertEquals($schema, $typeFactory->getType($type, 'json', null, null, new Schema()));
3333
}
3434

3535
public function typeProvider(): iterable
@@ -47,8 +47,8 @@ public function typeProvider(): iterable
4747
yield [['type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTimeImmutable::class)];
4848
yield [['nullable' => true, 'type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, true, \DateTimeImmutable::class)];
4949
yield [['type' => 'string', 'format' => 'duration'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateInterval::class)];
50-
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)];
51-
yield [['nullable' => true, 'type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)];
50+
yield [['type' => 'string', 'format' => 'iri-reference'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)];
51+
yield [['nullable' => true, 'type' => 'string', 'format' => 'iri-reference'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)];
5252
yield [['type' => 'array', 'items' => ['type' => 'string']], new Type(Type::BUILTIN_TYPE_STRING, false, null, true)];
5353
yield 'array can be itself nullable' => [
5454
['nullable' => true, 'type' => 'array', 'items' => ['type' => 'string']],
@@ -147,7 +147,7 @@ public function typeProvider(): iterable
147147
public function testGetTypeWithOpenAPIV2Syntax(array $schema, Type $type): void
148148
{
149149
$typeFactory = new TypeFactory();
150-
$this->assertSame($schema, $typeFactory->getType($type, 'json', null, [TypeFactory::CONTEXT_SERIALIZATION_FORMAT_OPENAPI_PRE_V3_0 => null]));
150+
$this->assertSame($schema, $typeFactory->getType($type, 'json', null, null, new Schema(Schema::VERSION_SWAGGER)));
151151
}
152152

153153
public function openAPIV2typeProvider(): iterable
@@ -165,8 +165,8 @@ public function openAPIV2typeProvider(): iterable
165165
yield [['type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTimeImmutable::class)];
166166
yield [['type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, true, \DateTimeImmutable::class)];
167167
yield [['type' => 'string', 'format' => 'duration'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateInterval::class)];
168-
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)];
169-
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)];
168+
yield [['type' => 'string', 'format' => 'iri-reference'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)];
169+
yield [['type' => 'string', 'format' => 'iri-reference'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)];
170170
yield [['type' => 'array', 'items' => ['type' => 'string']], new Type(Type::BUILTIN_TYPE_STRING, false, null, true)];
171171
yield 'array can be itself nullable, but ignored in OpenAPI V2' => [
172172
['type' => 'array', 'items' => ['type' => 'string']],
@@ -284,20 +284,11 @@ public function testGetClassTypeWithNullability(): void
284284
$typeFactory = new TypeFactory();
285285
$typeFactory->setSchemaFactory($schemaFactory);
286286

287-
self::assertSame(
288-
[
289-
'nullable' => true,
290-
'anyOf' => [
291-
['$ref' => 'the-ref-name'],
292-
],
287+
self::assertSame([
288+
'nullable' => true,
289+
'anyOf' => [
290+
['$ref' => 'the-ref-name'],
293291
],
294-
$typeFactory->getType(
295-
new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class),
296-
'jsonld',
297-
true,
298-
['foo' => 'bar'],
299-
new Schema()
300-
)
301-
);
292+
], $typeFactory->getType(new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class), 'jsonld', true, ['foo' => 'bar'], new Schema()));
302293
}
303294
}

0 commit comments

Comments
 (0)