From 68c76ce2bb43fb4603315fb973d4595711dcbfd3 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Mon, 15 Nov 2021 21:45:42 +0100 Subject: [PATCH] Handle nullable typed arrays (#998) --- src/Processors/AugmentProperties.php | 9 ++++++--- tests/Fixtures/Customer.php | 6 ++++++ tests/Processors/AugmentPropertiesTest.php | 10 ++++++++++ tests/Processors/AugmentSchemasTest.php | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Processors/AugmentProperties.php b/src/Processors/AugmentProperties.php index 2c112f8a7..72d74525a 100644 --- a/src/Processors/AugmentProperties.php +++ b/src/Processors/AugmentProperties.php @@ -83,9 +83,12 @@ public function __invoke(Analysis $analysis) } } elseif (preg_match('/@var\s+(?[^\s]+)([ \t])?(?.+)?$/im', $comment, $varMatches)) { if ($property->type === Generator::UNDEFINED) { - preg_match('/^([^\[]+)(.*$)/', trim($varMatches['type']), $typeMatches); - $isNullable = $this->isNullable($typeMatches[1]); - $type = $this->stripNull($typeMatches[1]); + $allTypes = trim($varMatches['type']); + $isNullable = $this->isNullable($allTypes); + $allTypes = $this->stripNull($allTypes); + preg_match('/^([^\[]+)(.*$)/', trim($allTypes), $typeMatches); + $type = $typeMatches[1]; + if (array_key_exists(strtolower($type), static::$types) === false) { $key = strtolower($context->fullyQualifiedName($type)); if ($property->ref === Generator::UNDEFINED && $typeMatches[2] === '' && array_key_exists($key, $refs)) { diff --git a/tests/Fixtures/Customer.php b/tests/Fixtures/Customer.php index 6646c2812..82dd5b395 100644 --- a/tests/Fixtures/Customer.php +++ b/tests/Fixtures/Customer.php @@ -82,6 +82,12 @@ class Customer */ public $bestFriend; + /** + * @OA\Property() + * @var Customer[]|null + */ + public $endorsedFriends; + /** * for ContextTest */ diff --git a/tests/Processors/AugmentPropertiesTest.php b/tests/Processors/AugmentPropertiesTest.php index d7ff2db06..d2ba9ac5e 100644 --- a/tests/Processors/AugmentPropertiesTest.php +++ b/tests/Processors/AugmentPropertiesTest.php @@ -35,6 +35,7 @@ public function testAugmentProperties() $submittedBy = $customer->properties[6]; $friends = $customer->properties[7]; $bestFriend = $customer->properties[8]; + $endorsedFriends = $customer->properties[9]; // Verify no values where defined in the annotation. $this->assertSame(Generator::UNDEFINED, $firstName->property); @@ -59,6 +60,10 @@ public function testAugmentProperties() $this->assertSame(Generator::UNDEFINED, $bestFriend->nullable); $this->assertSame(Generator::UNDEFINED, $bestFriend->allOf); + $this->assertSame(Generator::UNDEFINED, $endorsedFriends->property); + $this->assertSame(Generator::UNDEFINED, $endorsedFriends->nullable); + $this->assertSame(Generator::UNDEFINED, $endorsedFriends->allOf); + $analysis->process(new AugmentProperties()); $expectedValues = [ @@ -118,6 +123,11 @@ public function testAugmentProperties() $this->assertSame('bestFriend', $bestFriend->property); $this->assertTrue($bestFriend->nullable); $this->assertSame('#/components/schemas/Customer', $bestFriend->oneOf[0]->ref); + + $this->assertSame('endorsedFriends', $endorsedFriends->property); + $this->assertSame('array', $endorsedFriends->type); + $this->assertTrue($endorsedFriends->nullable); + $this->assertSame('#/components/schemas/Customer', $endorsedFriends->items->ref); } public function testTypedProperties() diff --git a/tests/Processors/AugmentSchemasTest.php b/tests/Processors/AugmentSchemasTest.php index 53ff423d3..94d5acf8d 100644 --- a/tests/Processors/AugmentSchemasTest.php +++ b/tests/Processors/AugmentSchemasTest.php @@ -26,7 +26,7 @@ public function testAugmentSchemas() $this->assertSame(Generator::UNDEFINED, $customer->properties, 'Sanity check. @OA\Property\'s not yet merged '); $analysis->process(new AugmentSchemas()); $this->assertSame('Customer', $customer->schema, '@OA\Schema()->schema based on classname'); - $this->assertCount(9, $customer->properties, '@OA\Property()s are merged into the @OA\Schema of the class'); + $this->assertCount(10, $customer->properties, '@OA\Property()s are merged into the @OA\Schema of the class'); } public function testAugmentSchemasForInterface()