FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[JSON Schema] Manage correctly nullability by alanpoulain · Pull Request #3817 · api-platform/core · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .php  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
11 changes: 11 additions & 0 deletions src/JsonSchema/TypeFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type, ?
];
}

if ($schema && Schema::VERSION_JSON_SCHEMA === $schema->getVersion()) {
return array_merge(
$jsonSchema,
[
'type' => \is_array($jsonSchema['type'])
? array_merge($jsonSchema['type'], ['null'])
: [$jsonSchema['type'], 'null'],
]
);
}

return array_merge($jsonSchema, ['nullable' => true]);
}
}
121 changes: 118 additions & 3 deletions tests/JsonSchema/TypeFactoryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TypeFactoryTest extends TestCase
public function testGetType(array $schema, Type $type): void
{
$typeFactory = new TypeFactory();
$this->assertEquals($schema, $typeFactory->getType($type, 'json', null, null, new Schema()));
$this->assertEquals($schema, $typeFactory->getType($type, 'json', null, null, new Schema(Schema::VERSION_OPENAPI)));
}

public function typeProvider(): iterable
Expand Down Expand Up @@ -146,14 +146,129 @@ public function typeProvider(): iterable
];
}

/** @dataProvider openAPIV2typeProvider */
/**
* @dataProvider jsonSchemaTypeProvider
*/
public function testGetTypeWithJsonSchemaSyntax(array $schema, Type $type): void
{
$typeFactory = new TypeFactory();
$this->assertEquals($schema, $typeFactory->getType($type, 'json', null, null, new Schema(Schema::VERSION_JSON_SCHEMA)));
}

public function jsonSchemaTypeProvider(): iterable
{
yield [['type' => 'integer'], new Type(Type::BUILTIN_TYPE_INT)];
yield [['type' => ['integer', 'null']], new Type(Type::BUILTIN_TYPE_INT, true)];
yield [['type' => 'number'], new Type(Type::BUILTIN_TYPE_FLOAT)];
yield [['type' => ['number', 'null']], new Type(Type::BUILTIN_TYPE_FLOAT, true)];
yield [['type' => 'boolean'], new Type(Type::BUILTIN_TYPE_BOOL)];
yield [['type' => ['boolean', 'null']], new Type(Type::BUILTIN_TYPE_BOOL, true)];
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_STRING)];
yield [['type' => ['string', 'null']], new Type(Type::BUILTIN_TYPE_STRING, true)];
yield [['type' => 'string'], new Type(Type::BUILTIN_TYPE_OBJECT)];
yield [['type' => ['string', 'null']], new Type(Type::BUILTIN_TYPE_OBJECT, true)];
yield [['type' => 'string', 'format' => 'date-time'], new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTimeImmutable::class)];
yield [['type' => ['string', 'null'], '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', 'format' => 'iri-reference'], new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)];
yield [['type' => ['string', 'null'], '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' => [
['type' => ['array', 'null'], 'items' => ['type' => 'string']],
new Type(Type::BUILTIN_TYPE_STRING, true, null, true),
];

yield 'array can contain nullable values' => [
[
'type' => 'array',
'items' => [
'type' => ['string', 'null'],
],
],
new Type(Type::BUILTIN_TYPE_STRING, false, null, true, null, new Type(Type::BUILTIN_TYPE_STRING, true, null, false)),
];

yield 'map with string keys becomes an object' => [
['type' => 'object', 'additionalProperties' => ['type' => 'string']],
new Type(
Type::BUILTIN_TYPE_STRING,
false,
null,
true,
new Type(Type::BUILTIN_TYPE_STRING, false, null, false)
),
];

yield 'nullable map with string keys becomes a nullable object' => [
[
'type' => ['object', 'null'],
'additionalProperties' => ['type' => 'string'],
],
new Type(
Type::BUILTIN_TYPE_STRING,
true,
null,
true,
new Type(Type::BUILTIN_TYPE_STRING, false, null, false),
new Type(Type::BUILTIN_TYPE_STRING, false, null, false)
),
];

yield 'map value type will be considered' => [
['type' => 'object', 'additionalProperties' => ['type' => 'integer']],
new Type(
Type::BUILTIN_TYPE_ARRAY,
false,
null,
true,
new Type(Type::BUILTIN_TYPE_STRING, false, null, false),
new Type(Type::BUILTIN_TYPE_INT, false, null, false)
),
];

yield 'map value type nullability will be considered' => [
[
'type' => 'object',
'additionalProperties' => [
'type' => ['integer', 'null'],
],
],
new Type(
Type::BUILTIN_TYPE_ARRAY,
false,
null,
true,
new Type(Type::BUILTIN_TYPE_STRING, false, null, false),
new Type(Type::BUILTIN_TYPE_INT, true, null, false)
),
];

yield 'nullable map can contain nullable values' => [
[
'type' => ['object', 'null'],
'additionalProperties' => [
'type' => ['integer', 'null'],
],
],
new Type(
Type::BUILTIN_TYPE_ARRAY,
true,
null,
true,
new Type(Type::BUILTIN_TYPE_STRING, false, null, false),
new Type(Type::BUILTIN_TYPE_INT, true, null, false)
),
];
}

/** @dataProvider openAPIV2TypeProvider */
public function testGetTypeWithOpenAPIV2Syntax(array $schema, Type $type): void
{
$typeFactory = new TypeFactory();
$this->assertSame($schema, $typeFactory->getType($type, 'json', null, null, new Schema(Schema::VERSION_SWAGGER)));
}

public function openAPIV2typeProvider(): iterable
public function openAPIV2TypeProvider(): iterable
{
yield [['type' => 'integer'], new Type(Type::BUILTIN_TYPE_INT)];
yield [['type' => 'integer'], new Type(Type::BUILTIN_TYPE_INT, true)];
Expand Down

Back | FazBrowse Home | New Git URL