Skip to content

Commit ba308b4

Browse files
committed
Better nullable properties in exported OpenAPI
1 parent f52a17e commit ba308b4

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/JsonSchema/SchemaFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str
203203
$propertySchema['example'] = $propertySchema['default'];
204204
}
205205

206+
if (!isset($propertySchema['nullable']) && !$propertyMetadata->isRequired()) {
207+
$propertySchema['nullable'] = true;
208+
}
209+
206210
$valueSchema = [];
207211
if (null !== $type = $propertyMetadata->getType()) {
208212
$isCollection = $type->isCollection();

tests/JsonSchema/SchemaFactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testBuildSchemaForNonResourceClass(): void
5555

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

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

8283
public function testBuildSchemaForOperationWithOverriddenSerializerGroups(): void

0 commit comments

Comments
 (0)