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 committed Apr 23, 2020
1 parent f52a17e commit a2cfac2
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 @@ -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);
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 @@ -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,
Expand Down Expand Up @@ -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,
]),
],
]),
Expand Down Expand Up @@ -679,6 +684,7 @@ public function testNormalizeWithApiKeysEnabled(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -911,6 +917,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand All @@ -922,6 +929,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void
'gerard' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a gerard.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -1034,6 +1042,7 @@ public function testNormalizeWithOpenApiDefinitionName(): void
'type' => 'integer',
'description' => 'This is an id.',
'readOnly' => true,
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -1244,6 +1253,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand All @@ -1255,6 +1265,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void
'gerard' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a gerard.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -1467,6 +1478,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand All @@ -1478,6 +1490,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void
'gerard' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a gerard.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -1997,6 +2010,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand All @@ -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/.',
Expand All @@ -2026,6 +2041,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
'name' => new \ArrayObject([
'type' => 'string',
'description' => 'This is a name.',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -2170,6 +2186,7 @@ private function doTestNormalizeWithFilters($filterLocator): void
'name' => new \ArrayObject([
'description' => 'This is a name.',
'type' => 'string',
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -2344,6 +2361,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
'type' => 'array',
'description' => 'This is a name.',
'items' => ['$ref' => '#/components/schemas/Answer'],
'nullable' => true,
]),
],
]),
Expand All @@ -2356,6 +2374,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
'type' => 'array',
'description' => 'This is a name.',
'items' => ['$ref' => '#/components/schemas/Answer'],
'nullable' => true,
]),
],
]),
Expand Down Expand Up @@ -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,
]),
],
]),
Expand Down Expand Up @@ -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,
]),
],
]),
Expand Down Expand Up @@ -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,
]),
],
]),
Expand Down Expand Up @@ -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,
]),
],
]),
Expand Down Expand Up @@ -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,
]),
],
]),
Expand Down

0 comments on commit a2cfac2

Please sign in to comment.