Skip to content

Commit

Permalink
Better nullable properties in exported OpenAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil committed Apr 23, 2020
1 parent f52a17e commit 2fc14d6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str
$propertySchema['example'] = $propertySchema['default'];
}

if (!$swagger && !isset($propertySchema['nullable']) && !$propertyMetadata->isRequired()) {
$propertySchema['nullable'] = true;
}

$valueSchema = [];
if (null !== $type = $propertyMetadata->getType()) {
$isCollection = $type->isCollection();
Expand Down
3 changes: 2 additions & 1 deletion tests/JsonSchema/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testBuildSchemaForNonResourceClass(): void

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(NotAResource::class, 'foo', Argument::cetera())->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), null, true));
$propertyMetadataFactoryProphecy->create(NotAResource::class, 'bar', Argument::cetera())->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), null, true));
$propertyMetadataFactoryProphecy->create(NotAResource::class, 'bar', Argument::cetera())->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), null, true, true, true, true, false));

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->isResourceClass(NotAResource::class)->willReturn(false);
Expand All @@ -77,6 +77,7 @@ public function testBuildSchemaForNonResourceClass(): void
$this->assertArrayHasKey('bar', $definitions[$rootDefinitionKey]['properties']);
$this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['bar']);
$this->assertSame('integer', $definitions[$rootDefinitionKey]['properties']['bar']['type']);
$this->assertTrue($definitions[$rootDefinitionKey]['properties']['bar']['nullable']);
}

public function testBuildSchemaForOperationWithOverriddenSerializerGroups(): void
Expand Down
29 changes: 29 additions & 0 deletions tests/Swagger/Serializer/DocumentationNormalizerV3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,17 @@ private function doTestNormalize(OperationMethodResolverInterface $operationMeth
'minLength' => 3,
'maxLength' => 20,
'pattern' => '^dummyPattern$',
'nullable' => true,
]),
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
'description' => new \ArrayObject([
'type' => 'string',
'description' => 'This is an initializable but not writable property.',
'nullable' => true,
]),
'dummyDate' => new \ArrayObject([
'nullable' => true,
Expand Down Expand Up @@ -545,10 +548,12 @@ private function doTestNormalizeWithNameConverter(bool $legacy = false): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
'name_converted' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a converted name.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -679,6 +684,7 @@ public function testNormalizeWithApiKeysEnabled(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -911,6 +917,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand All @@ -922,6 +929,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void
'gerard' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a gerard.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -1034,6 +1042,7 @@ public function testNormalizeWithOpenApiDefinitionName(): void
'type' => 'integer',
'description' => 'This is an id.',
'readOnly' => true,
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -1244,6 +1253,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand All @@ -1255,6 +1265,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void
'gerard' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a gerard.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -1467,6 +1478,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand All @@ -1478,6 +1490,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void
'gerard' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a gerard.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -1997,6 +2010,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand All @@ -2008,6 +2022,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
'relatedDummy' => new \ArrayObject([
'description' => 'This is a related dummy \o/.',
Expand All @@ -2026,6 +2041,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -2170,6 +2186,7 @@ private function doTestNormalizeWithFilters($filterLocator): void
'name' => new \ArrayObject([
'description' => 'This is a name.',
'type' => 'string',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -2344,6 +2361,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
'type' => 'array',
'description' => 'This is a name.',
'items' => ['$ref' => '#/components/schemas/Answer'],
'nullable' => true,
]),
],
]),
Expand All @@ -2356,6 +2374,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
'type' => 'array',
'description' => 'This is a name.',
'items' => ['$ref' => '#/components/schemas/Answer'],
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -2463,12 +2482,14 @@ public function testNormalizeWithPropertyOpenApiContext(): void
'type' => 'integer',
'description' => 'This is an id.',
'readOnly' => true,
'nullable' => true,
]),
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'enum' => ['one', 'two'],
'example' => 'one',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -2590,12 +2611,14 @@ public function testNormalizeWithPaginationClientEnabled(): void
'type' => 'integer',
'description' => 'This is an id.',
'readOnly' => true,
'nullable' => true,
]),
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'enum' => ['one', 'two'],
'example' => 'one',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -2723,12 +2746,14 @@ public function testNormalizeWithPaginationCustomDefaultAndMaxItemsPerPage(): vo
'type' => 'integer',
'description' => 'This is an id.',
'readOnly' => true,
'nullable' => true,
]),
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'enum' => ['one', 'two'],
'example' => 'one',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -2860,12 +2885,14 @@ public function testLegacyNormalizeWithPaginationCustomDefaultAndMaxItemsPerPage
'type' => 'integer',
'description' => 'This is an id.',
'readOnly' => true,
'nullable' => true,
]),
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'enum' => ['one', 'two'],
'example' => 'one',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -3097,10 +3124,12 @@ private function doTestNormalizeWithCustomFormatsDefinedAtOperationLevel(Operati
'type' => 'integer',
'description' => 'This is an id.',
'readOnly' => true,
'nullable' => true,
]),
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand Down

0 comments on commit 2fc14d6

Please sign in to comment.