diff --git a/CHANGELOG.md b/CHANGELOG.md index af023711589..b9d9324f443 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * GraphQL: Allow to format GraphQL errors based on exceptions (#3063) * GraphQL: Add page-based pagination (#3175) * OpenAPI: Add PHP default values to the documentation (#2386) +* OpenAPI: Add nullable schema components properties ## 2.5.3 diff --git a/src/JsonSchema/SchemaFactory.php b/src/JsonSchema/SchemaFactory.php index daf2e3aa014..069724e1afd 100644 --- a/src/JsonSchema/SchemaFactory.php +++ b/src/JsonSchema/SchemaFactory.php @@ -199,7 +199,8 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str $className = $valueType->getClassName(); } - $valueSchema = $this->typeFactory->getType(new Type($builtinType, $type->isNullable(), $className, $isCollection), $format, $propertyMetadata->isReadableLink(), $serializerContext, $schema); + $valueSchema = !$swagger && $type->isNullable() ? ['nullable' => true] : []; + $valueSchema += $this->typeFactory->getType(new Type($builtinType, $type->isNullable(), $className, $isCollection), $format, $propertyMetadata->isReadableLink(), $serializerContext, $schema); } $propertySchema = new \ArrayObject($propertySchema + $valueSchema); diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index d6250fb3b90..f7f2e0a6445 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -388,6 +388,7 @@ private function doTestNormalize(OperationMethodResolverInterface $operationMeth 'type' => 'string', 'description' => 'This is a \DateTimeInterface object.', 'format' => 'date-time', + 'nullable' => true, ]), ], ]), @@ -1998,6 +1999,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void 'relatedDummy' => new \ArrayObject([ 'description' => 'This is a related dummy \o/.', '$ref' => '#/components/schemas/'.$relatedDummyRef, + 'nullable' => true, ]), ], ]),