From 1287c704aaff68053c0dae77c50f81f4bfd51ede Mon Sep 17 00:00:00 2001 From: Baptiste Leduc Date: Thu, 23 Apr 2020 19:04:04 +0200 Subject: [PATCH 1/2] 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 a1ab3610e0b..03cb915c82d 100644 --- a/src/JsonSchema/SchemaFactory.php +++ b/src/JsonSchema/SchemaFactory.php @@ -228,7 +228,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); } if (\array_key_exists('type', $propertySchema) && \array_key_exists('$ref', $valueSchema)) { diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 3ea56e07583..e31cea0c18b 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -394,14 +394,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, @@ -567,10 +570,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, ]), ], ]), @@ -706,6 +711,7 @@ public function testNormalizeWithApiKeysEnabled(): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), ], ]), @@ -945,6 +951,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), ], ]), @@ -956,6 +963,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void 'gerard' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a gerard.', + 'nullable' => true, ]), ], ]), @@ -1073,6 +1081,7 @@ public function testNormalizeWithOpenApiDefinitionName(): void 'type' => 'integer', 'description' => 'This is an id.', 'readOnly' => true, + 'nullable' => true, ]), ], ]), @@ -1290,6 +1299,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), ], ]), @@ -1301,6 +1311,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void 'gerard' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a gerard.', + 'nullable' => true, ]), ], ]), @@ -1520,6 +1531,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), ], ]), @@ -1531,6 +1543,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void 'gerard' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a gerard.', + 'nullable' => true, ]), ], ]), @@ -2072,6 +2085,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), ], ]), @@ -2083,6 +2097,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/.', @@ -2101,6 +2116,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void 'name' => new \ArrayObject([ 'type' => 'string', 'description' => 'This is a name.', + 'nullable' => true, ]), ], ]), @@ -2250,6 +2266,7 @@ private function doTestNormalizeWithFilters($filterLocator): void 'name' => new \ArrayObject([ 'description' => 'This is a name.', 'type' => 'string', + 'nullable' => true, ]), ], ]), @@ -2432,6 +2449,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt 'type' => 'array', 'description' => 'This is a name.', 'items' => ['$ref' => '#/components/schemas/Answer'], + 'nullable' => true, ]), ], ]), @@ -2444,6 +2462,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt 'type' => 'array', 'description' => 'This is a name.', 'items' => ['$ref' => '#/components/schemas/Answer'], + 'nullable' => true, ]), ], ]), @@ -2556,12 +2575,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, ]), ], ]), @@ -2688,12 +2709,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, ]), ], ]), @@ -2826,12 +2849,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, ]), ], ]), @@ -2968,12 +2993,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, ]), ], ]), @@ -3212,10 +3239,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, ]), ], ]), From 84e09da935e1535e8aa35ece3aeab74fd33d7e5c Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Thu, 4 Mar 2021 17:15:02 +0100 Subject: [PATCH 2/2] chore: add changelog entry --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e304f6de76d..9b5c29eeac6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,9 @@ ## 2.7.0 -* **BC**: Change `api_platform.listener.request.add_format` priority from 7 to 28 to execute it before firewall (priority 8) (#3599) -* **BC**: Use `@final` annotation in ORM filters (#4109) +* **BC** Change `api_platform.listener.request.add_format` priority from 7 to 28 to execute it before firewall (priority 8) (#3599) +* Doctrine: **BC** Use `@final` annotation in ORM filters (#4109) +* OpenAPI: **BC** Better nullable handling in JSON Schema / OpenAPI (#3518) * Allow defining `exception_to_status` per operation (#3519) * Doctrine: Better exception to find which resource is linked to an exception (#3965) * Doctrine: Allow mixed type value for date filter (notice if invalid) (#3870)