Skip to content

Commit

Permalink
Better nullable properties in exported OpenAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil authored and alanpoulain committed Mar 9, 2021
1 parent 4717bd5 commit 1287c70
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
29 changes: 29 additions & 0 deletions tests/Swagger/Serializer/DocumentationNormalizerV3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
]),
],
]),
Expand Down Expand Up @@ -706,6 +711,7 @@ public function testNormalizeWithApiKeysEnabled(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -945,6 +951,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand All @@ -956,6 +963,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void
'gerard' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a gerard.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -1073,6 +1081,7 @@ public function testNormalizeWithOpenApiDefinitionName(): void
'type' => 'integer',
'description' => 'This is an id.',
'readOnly' => true,
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -1290,6 +1299,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand All @@ -1301,6 +1311,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void
'gerard' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a gerard.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -1520,6 +1531,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand All @@ -1531,6 +1543,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void
'gerard' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a gerard.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -2072,6 +2085,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand All @@ -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/.',
Expand All @@ -2101,6 +2116,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -2250,6 +2266,7 @@ private function doTestNormalizeWithFilters($filterLocator): void
'name' => new \ArrayObject([
'description' => 'This is a name.',
'type' => 'string',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -2432,6 +2449,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
'type' => 'array',
'description' => 'This is a name.',
'items' => ['$ref' => '#/components/schemas/Answer'],
'nullable' => true,
]),
],
]),
Expand All @@ -2444,6 +2462,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
'type' => 'array',
'description' => 'This is a name.',
'items' => ['$ref' => '#/components/schemas/Answer'],
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -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,
]),
],
]),
Expand Down Expand Up @@ -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,
]),
],
]),
Expand Down Expand Up @@ -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,
]),
],
]),
Expand Down Expand Up @@ -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,
]),
],
]),
Expand Down Expand Up @@ -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,
]),
],
]),
Expand Down

0 comments on commit 1287c70

Please sign in to comment.