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 ba308b4
Show file tree
Hide file tree
Showing 2 changed files with 6 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 (!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

0 comments on commit ba308b4

Please sign in to comment.