From a2cfac2c0c5f556cc5b947dec5b5a29c7e99973d Mon Sep 17 00:00:00 2001 From: Baptiste Leduc Date: Thu, 23 Apr 2020 19:04:04 +0200 Subject: [PATCH] Better nullable properties in exported OpenAPI --- src/JsonSchema/SchemaFactory.php | 2 +- .../DocumentationNormalizerV3Test.php | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/JsonSchema/SchemaFactory.php b/src/JsonSchema/SchemaFactory.php index ac582f45be1..7b697d82d1d 100644 --- a/src/JsonSchema/SchemaFactory.php +++ b/src/JsonSchema/SchemaFactory.php @@ -214,7 +214,7 @@ 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 = $this->typeFactory->getType(new Type($builtinType, !$propertyMetadata->isRequired() || $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 846f638dafe..f59298b2bc3 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -377,14 +377,17 @@ private function doTestNormalize(OperationMethodResolverInterface $operationMeth 'minLength' => 3, 'maxLength' => 20, 'pattern' => '^dummyPattern$', + 'nullable' => true, ]), 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), 'description' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is an initializable but not writable property.', + 'nullable' => true, ]), 'dummyDate' => new \ArrayObject([ 'nullable' => true, @@ -545,10 +548,12 @@ private function doTestNormalizeWithNameConverter(bool $legacy = false): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), 'name_converted' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a converted name.', + 'nullable' => true, ]), ], ]), @@ -679,6 +684,7 @@ public function testNormalizeWithApiKeysEnabled(): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), ], ]), @@ -911,6 +917,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), ], ]), @@ -922,6 +929,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void 'gerard' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a gerard.', + 'nullable' => true, ]), ], ]), @@ -1034,6 +1042,7 @@ public function testNormalizeWithOpenApiDefinitionName(): void 'type' => 'integer', 'description' => 'This is an id.', 'readOnly' => true, + 'nullable' => true, ]), ], ]), @@ -1244,6 +1253,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), ], ]), @@ -1255,6 +1265,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void 'gerard' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a gerard.', + 'nullable' => true, ]), ], ]), @@ -1467,6 +1478,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), ], ]), @@ -1478,6 +1490,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void 'gerard' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a gerard.', + 'nullable' => true, ]), ], ]), @@ -1997,6 +2010,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), ], ]), @@ -2008,6 +2022,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), 'relatedDummy' => new \ArrayObject([ 'description' => 'This is a related dummy \o/.', @@ -2026,6 +2041,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), ], ]), @@ -2170,6 +2186,7 @@ private function doTestNormalizeWithFilters($filterLocator): void 'name' => new \ArrayObject([ 'description' => 'This is a name.', 'type' => 'string', + 'nullable' => true, ]), ], ]), @@ -2344,6 +2361,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt 'type' => 'array', 'description' => 'This is a name.', 'items' => ['$ref' => '#/components/schemas/Answer'], + 'nullable' => true, ]), ], ]), @@ -2356,6 +2374,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt 'type' => 'array', 'description' => 'This is a name.', 'items' => ['$ref' => '#/components/schemas/Answer'], + 'nullable' => true, ]), ], ]), @@ -2463,12 +2482,14 @@ public function testNormalizeWithPropertyOpenApiContext(): void 'type' => 'integer', 'description' => 'This is an id.', 'readOnly' => true, + 'nullable' => true, ]), 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', 'enum' => ['one', 'two'], 'example' => 'one', + 'nullable' => true, ]), ], ]), @@ -2590,12 +2611,14 @@ public function testNormalizeWithPaginationClientEnabled(): void 'type' => 'integer', 'description' => 'This is an id.', 'readOnly' => true, + 'nullable' => true, ]), 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', 'enum' => ['one', 'two'], 'example' => 'one', + 'nullable' => true, ]), ], ]), @@ -2723,12 +2746,14 @@ public function testNormalizeWithPaginationCustomDefaultAndMaxItemsPerPage(): vo 'type' => 'integer', 'description' => 'This is an id.', 'readOnly' => true, + 'nullable' => true, ]), 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', 'enum' => ['one', 'two'], 'example' => 'one', + 'nullable' => true, ]), ], ]), @@ -2860,12 +2885,14 @@ public function testLegacyNormalizeWithPaginationCustomDefaultAndMaxItemsPerPage 'type' => 'integer', 'description' => 'This is an id.', 'readOnly' => true, + 'nullable' => true, ]), 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', 'enum' => ['one', 'two'], 'example' => 'one', + 'nullable' => true, ]), ], ]), @@ -3097,10 +3124,12 @@ private function doTestNormalizeWithCustomFormatsDefinedAtOperationLevel(Operati 'type' => 'integer', 'description' => 'This is an id.', 'readOnly' => true, + 'nullable' => true, ]), 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), ], ]),