Skip to content

Commit 82ba090

Browse files
committed
Better nullable properties in exported OpenAPI
1 parent f52a17e commit 82ba090

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

src/JsonSchema/SchemaFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str
214214
$className = $valueType->getClassName();
215215
}
216216

217-
$valueSchema = $this->typeFactory->getType(new Type($builtinType, $type->isNullable(), $className, $isCollection), $format, $propertyMetadata->isReadableLink(), $serializerContext, $schema);
217+
$valueSchema = $this->typeFactory->getType(new Type($builtinType, !$propertyMetadata->isRequired() || $type->isNullable(), $className, $isCollection), $format, $propertyMetadata->isReadableLink(), $serializerContext, $schema);
218218
}
219219

220220
$propertySchema = new \ArrayObject($propertySchema + $valueSchema);

src/JsonSchema/TypeFactory.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,7 @@ private function getClassType(?string $className, string $format, ?bool $readabl
146146
*/
147147
private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type, ?Schema $schema): array
148148
{
149-
if ($schema && Schema::VERSION_SWAGGER === $schema->getVersion()) {
150-
return $jsonSchema;
151-
}
152-
153-
if (!$type->isNullable()) {
149+
if (($schema && Schema::VERSION_SWAGGER === $schema->getVersion()) || !$type->isNullable()) {
154150
return $jsonSchema;
155151
}
156152

tests/Swagger/Serializer/DocumentationNormalizerV3Test.php

+29
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,17 @@ private function doTestNormalize(OperationMethodResolverInterface $operationMeth
377377
'minLength' => 3,
378378
'maxLength' => 20,
379379
'pattern' => '^dummyPattern$',
380+
'nullable' => true,
380381
]),
381382
'name' => new \ArrayObject([
382383
'type' => 'string',
383384
'description' => 'This is a name.',
385+
'nullable' => true,
384386
]),
385387
'description' => new \ArrayObject([
386388
'type' => 'string',
387389
'description' => 'This is an initializable but not writable property.',
390+
'nullable' => true,
388391
]),
389392
'dummyDate' => new \ArrayObject([
390393
'nullable' => true,
@@ -545,10 +548,12 @@ private function doTestNormalizeWithNameConverter(bool $legacy = false): void
545548
'name' => new \ArrayObject([
546549
'type' => 'string',
547550
'description' => 'This is a name.',
551+
'nullable' => true,
548552
]),
549553
'name_converted' => new \ArrayObject([
550554
'type' => 'string',
551555
'description' => 'This is a converted name.',
556+
'nullable' => true,
552557
]),
553558
],
554559
]),
@@ -679,6 +684,7 @@ public function testNormalizeWithApiKeysEnabled(): void
679684
'name' => new \ArrayObject([
680685
'type' => 'string',
681686
'description' => 'This is a name.',
687+
'nullable' => true,
682688
]),
683689
],
684690
]),
@@ -911,6 +917,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void
911917
'name' => new \ArrayObject([
912918
'type' => 'string',
913919
'description' => 'This is a name.',
920+
'nullable' => true,
914921
]),
915922
],
916923
]),
@@ -922,6 +929,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void
922929
'gerard' => new \ArrayObject([
923930
'type' => 'string',
924931
'description' => 'This is a gerard.',
932+
'nullable' => true,
925933
]),
926934
],
927935
]),
@@ -1034,6 +1042,7 @@ public function testNormalizeWithOpenApiDefinitionName(): void
10341042
'type' => 'integer',
10351043
'description' => 'This is an id.',
10361044
'readOnly' => true,
1045+
'nullable' => true,
10371046
]),
10381047
],
10391048
]),
@@ -1244,6 +1253,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void
12441253
'name' => new \ArrayObject([
12451254
'type' => 'string',
12461255
'description' => 'This is a name.',
1256+
'nullable' => true,
12471257
]),
12481258
],
12491259
]),
@@ -1255,6 +1265,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void
12551265
'gerard' => new \ArrayObject([
12561266
'type' => 'string',
12571267
'description' => 'This is a gerard.',
1268+
'nullable' => true,
12581269
]),
12591270
],
12601271
]),
@@ -1467,6 +1478,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void
14671478
'name' => new \ArrayObject([
14681479
'type' => 'string',
14691480
'description' => 'This is a name.',
1481+
'nullable' => true,
14701482
]),
14711483
],
14721484
]),
@@ -1478,6 +1490,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void
14781490
'gerard' => new \ArrayObject([
14791491
'type' => 'string',
14801492
'description' => 'This is a gerard.',
1493+
'nullable' => true,
14811494
]),
14821495
],
14831496
]),
@@ -1997,6 +2010,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
19972010
'name' => new \ArrayObject([
19982011
'type' => 'string',
19992012
'description' => 'This is a name.',
2013+
'nullable' => true,
20002014
]),
20012015
],
20022016
]),
@@ -2008,6 +2022,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
20082022
'name' => new \ArrayObject([
20092023
'type' => 'string',
20102024
'description' => 'This is a name.',
2025+
'nullable' => true,
20112026
]),
20122027
'relatedDummy' => new \ArrayObject([
20132028
'description' => 'This is a related dummy \o/.',
@@ -2026,6 +2041,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
20262041
'name' => new \ArrayObject([
20272042
'type' => 'string',
20282043
'description' => 'This is a name.',
2044+
'nullable' => true,
20292045
]),
20302046
],
20312047
]),
@@ -2170,6 +2186,7 @@ private function doTestNormalizeWithFilters($filterLocator): void
21702186
'name' => new \ArrayObject([
21712187
'description' => 'This is a name.',
21722188
'type' => 'string',
2189+
'nullable' => true,
21732190
]),
21742191
],
21752192
]),
@@ -2344,6 +2361,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
23442361
'type' => 'array',
23452362
'description' => 'This is a name.',
23462363
'items' => ['$ref' => '#/components/schemas/Answer'],
2364+
'nullable' => true,
23472365
]),
23482366
],
23492367
]),
@@ -2356,6 +2374,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
23562374
'type' => 'array',
23572375
'description' => 'This is a name.',
23582376
'items' => ['$ref' => '#/components/schemas/Answer'],
2377+
'nullable' => true,
23592378
]),
23602379
],
23612380
]),
@@ -2463,12 +2482,14 @@ public function testNormalizeWithPropertyOpenApiContext(): void
24632482
'type' => 'integer',
24642483
'description' => 'This is an id.',
24652484
'readOnly' => true,
2485+
'nullable' => true,
24662486
]),
24672487
'name' => new \ArrayObject([
24682488
'type' => 'string',
24692489
'description' => 'This is a name.',
24702490
'enum' => ['one', 'two'],
24712491
'example' => 'one',
2492+
'nullable' => true,
24722493
]),
24732494
],
24742495
]),
@@ -2590,12 +2611,14 @@ public function testNormalizeWithPaginationClientEnabled(): void
25902611
'type' => 'integer',
25912612
'description' => 'This is an id.',
25922613
'readOnly' => true,
2614+
'nullable' => true,
25932615
]),
25942616
'name' => new \ArrayObject([
25952617
'type' => 'string',
25962618
'description' => 'This is a name.',
25972619
'enum' => ['one', 'two'],
25982620
'example' => 'one',
2621+
'nullable' => true,
25992622
]),
26002623
],
26012624
]),
@@ -2723,12 +2746,14 @@ public function testNormalizeWithPaginationCustomDefaultAndMaxItemsPerPage(): vo
27232746
'type' => 'integer',
27242747
'description' => 'This is an id.',
27252748
'readOnly' => true,
2749+
'nullable' => true,
27262750
]),
27272751
'name' => new \ArrayObject([
27282752
'type' => 'string',
27292753
'description' => 'This is a name.',
27302754
'enum' => ['one', 'two'],
27312755
'example' => 'one',
2756+
'nullable' => true,
27322757
]),
27332758
],
27342759
]),
@@ -2860,12 +2885,14 @@ public function testLegacyNormalizeWithPaginationCustomDefaultAndMaxItemsPerPage
28602885
'type' => 'integer',
28612886
'description' => 'This is an id.',
28622887
'readOnly' => true,
2888+
'nullable' => true,
28632889
]),
28642890
'name' => new \ArrayObject([
28652891
'type' => 'string',
28662892
'description' => 'This is a name.',
28672893
'enum' => ['one', 'two'],
28682894
'example' => 'one',
2895+
'nullable' => true,
28692896
]),
28702897
],
28712898
]),
@@ -3097,10 +3124,12 @@ private function doTestNormalizeWithCustomFormatsDefinedAtOperationLevel(Operati
30973124
'type' => 'integer',
30983125
'description' => 'This is an id.',
30993126
'readOnly' => true,
3127+
'nullable' => true,
31003128
]),
31013129
'name' => new \ArrayObject([
31023130
'type' => 'string',
31033131
'description' => 'This is a name.',
3132+
'nullable' => true,
31043133
]),
31053134
],
31063135
]),

0 commit comments

Comments
 (0)