Skip to content

Commit c5d7076

Browse files
committed
Support array with one type - Close #2
1 parent 1859c22 commit c5d7076

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

src/Type/ArrayType.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static function fromDefinition(array $definition, ?string $name = null):
7676
};
7777

7878
$populateArrayType = static function (string $key, array $definitionValue) use ($resolveReference, $self) {
79-
if (isset($definitionValue['type'])) {
79+
if (isset($definitionValue['type']) || isset($definitionValue['$ref'])) {
8080
$self->$key[] = Type::fromDefinition($definitionValue, '');
8181

8282
return;
@@ -86,7 +86,11 @@ public static function fromDefinition(array $definition, ?string $name = null):
8686
$self->$key[] = Type::fromDefinition($propertyDefinition, '');
8787
} elseif (isset($propertyDefinition['$ref'])) {
8888
$ref = ReferenceType::fromDefinition($propertyDefinition, '');
89-
$ref->setResolvedType($resolveReference($propertyDefinition['$ref']));
89+
90+
if ($resolvedType = $resolveReference($propertyDefinition['$ref'])) {
91+
$ref->setResolvedType($resolveReference($propertyDefinition['$ref']));
92+
}
93+
9094
$self->$key[] = new TypeSet($ref);
9195
}
9296
}

src/Type/ObjectType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ public static function fromDefinition(array $definition, ?string $name = null):
8989
$self->properties[$propertyName] = Type::fromDefinition($propertyDefinition, $propertyName);
9090
} elseif (isset($propertyDefinition['$ref'])) {
9191
$ref = ReferenceType::fromDefinition($propertyDefinition, '');
92-
$ref->setResolvedType($resolveReference($propertyDefinition['$ref']));
92+
93+
if ($resolvedType = $resolveReference($propertyDefinition['$ref'])) {
94+
$ref->setResolvedType($resolvedType);
95+
}
96+
9397
$self->properties[$propertyName] = new TypeSet($ref);
9498
}
9599
}

tests/Type/ArrayTypeTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,56 @@ public function it_supports_array_type(): void
4242
$this->assertItemFour($items[3]);
4343
}
4444

45+
/**
46+
* @test
47+
*/
48+
public function it_supports_array_with_one_type(): void
49+
{
50+
$json = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_array_one_type.json');
51+
$decodedJson = \json_decode($json, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
52+
53+
$typeSet = Type::fromDefinition($decodedJson);
54+
55+
$this->assertCount(1, $typeSet);
56+
57+
/** @var ArrayType $type */
58+
$type = $typeSet->first();
59+
60+
$this->assertInstanceOf(ArrayType::class, $type);
61+
62+
$items = $type->items();
63+
$this->assertCount(1, $items);
64+
65+
$this->assertItemOne($items[0]);
66+
}
67+
68+
/**
69+
* @test
70+
*/
71+
public function it_supports_array_shorthand_with_no_ref(): void
72+
{
73+
$json = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_array_shorthand_no_ref.json');
74+
$decodedJson = \json_decode($json, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
75+
76+
$typeSet = Type::fromShorthand($decodedJson);
77+
78+
$this->assertCount(1, $typeSet);
79+
80+
/** @var ArrayType $type */
81+
$type = $typeSet->first();
82+
83+
$this->assertInstanceOf(ArrayType::class, $type);
84+
85+
$items = $type->items();
86+
$this->assertCount(1, $items);
87+
88+
/** @var ReferenceType $item */
89+
$item = $items[0]->first();
90+
91+
$this->assertInstanceOf(ReferenceType::class, $item);
92+
$this->assertNull($item->resolvedType());
93+
}
94+
4595
private function assertItemOne(TypeSet $itemOne): void
4696
{
4797
$this->assertCount(1, $itemOne);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "array",
3+
"items": {
4+
"type": "number"
5+
}
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"$items": "Name"
3+
}

0 commit comments

Comments
 (0)