diff --git a/src/JsonSchema/SchemaFactory.php b/src/JsonSchema/SchemaFactory.php index ac582f45be1..0c180d32738 100644 --- a/src/JsonSchema/SchemaFactory.php +++ b/src/JsonSchema/SchemaFactory.php @@ -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(); diff --git a/tests/JsonSchema/SchemaFactoryTest.php b/tests/JsonSchema/SchemaFactoryTest.php index ca7fbbf2af0..3a00bff878f 100644 --- a/tests/JsonSchema/SchemaFactoryTest.php +++ b/tests/JsonSchema/SchemaFactoryTest.php @@ -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); @@ -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