Skip to content

Commit

Permalink
Handle nullable typed arrays (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu authored Nov 15, 2021
1 parent e8594d3 commit 68c76ce
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/Processors/AugmentProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ public function __invoke(Analysis $analysis)
}
} elseif (preg_match('/@var\s+(?<type>[^\s]+)([ \t])?(?<description>.+)?$/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)) {
Expand Down
6 changes: 6 additions & 0 deletions tests/Fixtures/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class Customer
*/
public $bestFriend;

/**
* @OA\Property()
* @var Customer[]|null
*/
public $endorsedFriends;

/**
* for ContextTest
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/Processors/AugmentPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = [
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/Processors/AugmentSchemasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 68c76ce

Please sign in to comment.