Skip to content

Commit

Permalink
Revert changes related to $serializerContext as we already have Schem…
Browse files Browse the repository at this point in the history
…a::getVersion()
  • Loading branch information
teohhanhui committed Feb 27, 2020
1 parent 25697ee commit 9d17089
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 72 deletions.
8 changes: 4 additions & 4 deletions src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,19 @@ private function getMetadata(string $className, string $type = Schema::TYPE_OUTP

return [
$resourceMetadata,
$this->getSerializerContext($resourceMetadata, $type, $operationType, $operationName, $serializerContext),
$serializerContext ?? $this->getSerializerContext($resourceMetadata, $type, $operationType, $operationName),
$inputOrOutput['class'],
];
}

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

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

return array_merge($resourceMetadata->getTypedOperationAttribute($operationType, $operationName, $attribute, [], true), (array) $previousSerializerContext);
return $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, $attribute, [], true);
}
}
47 changes: 11 additions & 36 deletions src/JsonSchema/TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,6 @@ final class TypeFactory implements TypeFactoryInterface
{
use ResourceClassInfoTrait;

/**
* This constant is to be provided as serializer context key to conditionally enable types to be generated in
* a format that is compatible with OpenAPI specifications **PREVIOUS** to 3.0.
*
* Without this flag being set, the generated format will only be compatible with Swagger 3.0 or newer.
*
* Once support for OpenAPI < 3.0 is gone, this constant **WILL BE REMOVED**
*
* @internal Once support for OpenAPI < 3.0 is gone, this constant **WILL BE REMOVED** - do not rely on
* it in downstream projects!
*/
public const CONTEXT_SERIALIZATION_FORMAT_OPENAPI_PRE_V3_0 = self::class.'::CONTEXT_SERIALIZATION_FORMAT_OPENAPI_PRE_V3_0';

/**
* @var SchemaFactoryInterface|null
*/
Expand All @@ -67,31 +54,19 @@ public function getType(Type $type, string $format = 'json', ?bool $readableLink
$subType = $type->getCollectionValueType() ?? new Type($type->getBuiltinType(), false, $type->getClassName(), false);

if (null !== $keyType && Type::BUILTIN_TYPE_STRING === $keyType->getBuiltinType()) {
return $this->addNullabilityToTypeDefinition(
[
'type' => 'object',
'additionalProperties' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
],
$type,
(array) $serializerContext
);
return $this->addNullabilityToTypeDefinition([
'type' => 'object',
'additionalProperties' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
], $type, $schema);
}

return $this->addNullabilityToTypeDefinition(
[
'type' => 'array',
'items' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
],
$type,
(array) $serializerContext
);
return $this->addNullabilityToTypeDefinition([
'type' => 'array',
'items' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
], $type, $schema);
}

return $this->addNullabilityToTypeDefinition(
$this->makeBasicType($type, $format, $readableLink, $serializerContext, $schema),
$type,
(array) $serializerContext
);
return $this->addNullabilityToTypeDefinition($this->makeBasicType($type, $format, $readableLink, $serializerContext, $schema), $type, $schema);
}

private function makeBasicType(Type $type, string $format = 'json', ?bool $readableLink = null, ?array $serializerContext = null, Schema $schema = null): array
Expand Down Expand Up @@ -169,9 +144,9 @@ private function getClassType(?string $className, string $format, ?bool $readabl
*
* @return array<string, mixed>
*/
private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type, array $serializerContext): array
private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type, ?Schema $schema): array
{
if (\array_key_exists(self::CONTEXT_SERIALIZATION_FORMAT_OPENAPI_PRE_V3_0, $serializerContext)) {
if ($schema && Schema::VERSION_SWAGGER === $schema->getVersion()) {
return $jsonSchema;
}

Expand Down
13 changes: 1 addition & 12 deletions src/Swagger/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,10 +594,6 @@ private function getJsonSchema(bool $v3, \ArrayObject $definitions, string $reso
$schema = new Schema($v3 ? Schema::VERSION_OPENAPI : Schema::VERSION_SWAGGER);
$schema->setDefinitions($definitions);

if (!$v3) {
$serializerContext = array_merge([TypeFactory::CONTEXT_SERIALIZATION_FORMAT_OPENAPI_PRE_V3_0 => null], (array) $serializerContext);
}

$this->jsonSchemaFactory->buildSchema($resourceClass, $format, $type, $operationType, $operationName, $schema, $serializerContext, $forceCollection);

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

$type = \in_array($data['type'], Type::$builtinTypes, true)
? $this->jsonSchemaTypeFactory->getType(
new Type($data['type'], false, null, $data['is_collection'] ?? false),
'json',
null,
$v3 ? null : [TypeFactory::CONTEXT_SERIALIZATION_FORMAT_OPENAPI_PRE_V3_0 => null]
)
: ['type' => 'string'];
$type = \in_array($data['type'], Type::$builtinTypes, true) ? $this->jsonSchemaTypeFactory->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false)) : ['type' => 'string'];
$v3 ? $parameter['schema'] = $type : $parameter += $type;

if ($v3 && isset($data['schema'])) {
Expand Down
31 changes: 11 additions & 20 deletions tests/JsonSchema/TypeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TypeFactoryTest extends TestCase
public function testGetType(array $schema, Type $type): void
{
$typeFactory = new TypeFactory();
$this->assertEquals($schema, $typeFactory->getType($type));
$this->assertEquals($schema, $typeFactory->getType($type, 'json', null, null, new Schema()));
}

public function typeProvider(): iterable
Expand All @@ -47,8 +47,8 @@ public function typeProvider(): iterable
yield [['type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTimeImmutable::class)];
yield [['nullable' => true, 'type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, true, \DateTimeImmutable::class)];
yield [['type' => 'string', 'format' => 'duration'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateInterval::class)];
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)];
yield [['nullable' => true, 'type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)];
yield [['type' => 'string', 'format' => 'iri-reference'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)];
yield [['nullable' => true, 'type' => 'string', 'format' => 'iri-reference'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)];
yield [['type' => 'array', 'items' => ['type' => 'string']], new Type(Type::BUILTIN_TYPE_STRING, false, null, true)];
yield 'array can be itself nullable' => [
['nullable' => true, 'type' => 'array', 'items' => ['type' => 'string']],
Expand Down Expand Up @@ -147,7 +147,7 @@ public function typeProvider(): iterable
public function testGetTypeWithOpenAPIV2Syntax(array $schema, Type $type): void
{
$typeFactory = new TypeFactory();
$this->assertSame($schema, $typeFactory->getType($type, 'json', null, [TypeFactory::CONTEXT_SERIALIZATION_FORMAT_OPENAPI_PRE_V3_0 => null]));
$this->assertSame($schema, $typeFactory->getType($type, 'json', null, null, new Schema(Schema::VERSION_SWAGGER)));
}

public function openAPIV2typeProvider(): iterable
Expand All @@ -165,8 +165,8 @@ public function openAPIV2typeProvider(): iterable
yield [['type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTimeImmutable::class)];
yield [['type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, true, \DateTimeImmutable::class)];
yield [['type' => 'string', 'format' => 'duration'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateInterval::class)];
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)];
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)];
yield [['type' => 'string', 'format' => 'iri-reference'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)];
yield [['type' => 'string', 'format' => 'iri-reference'], new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class)];
yield [['type' => 'array', 'items' => ['type' => 'string']], new Type(Type::BUILTIN_TYPE_STRING, false, null, true)];
yield 'array can be itself nullable, but ignored in OpenAPI V2' => [
['type' => 'array', 'items' => ['type' => 'string']],
Expand Down Expand Up @@ -284,20 +284,11 @@ public function testGetClassTypeWithNullability(): void
$typeFactory = new TypeFactory();
$typeFactory->setSchemaFactory($schemaFactory);

self::assertSame(
[
'nullable' => true,
'anyOf' => [
['$ref' => 'the-ref-name'],
],
self::assertSame([
'nullable' => true,
'anyOf' => [
['$ref' => 'the-ref-name'],
],
$typeFactory->getType(
new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class),
'jsonld',
true,
['foo' => 'bar'],
new Schema()
)
);
], $typeFactory->getType(new Type(Type::BUILTIN_TYPE_OBJECT, true, Dummy::class), 'jsonld', true, ['foo' => 'bar'], new Schema()));
}
}

0 comments on commit 9d17089

Please sign in to comment.