From 4893f8a9e1113f55f2550fe7f72c9991b36ea9ad Mon Sep 17 00:00:00 2001 From: Dylan Ballandras Date: Tue, 17 Dec 2019 09:52:58 +0100 Subject: [PATCH] [JSONSchema] Add nullable property description --- CHANGELOG.md | 1 + src/JsonSchema/SchemaFactory.php | 3 ++- tests/Swagger/Serializer/DocumentationNormalizerV3Test.php | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) 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, ]), ], ]),