Skip to content

Commit 5968b14

Browse files
committed
Add support for array type $ref - Close #4
1 parent 13c1bf8 commit 5968b14

File tree

4 files changed

+87
-19
lines changed

4 files changed

+87
-19
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,23 @@
33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

55
## 0.1.0 - TBD
6+
7+
### Added
8+
9+
- Nothing.
10+
11+
### Changed
12+
13+
- Nothing.
14+
15+
### Deprecated
16+
17+
- Nothing.
18+
19+
### Removed
20+
21+
- Nothing.
22+
23+
### Fixed
24+
25+
- Nothing.

src/Type/ArrayType.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,38 @@ 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']) || isset($definitionValue['$ref'])) {
80-
$self->$key[] = Type::fromDefinition($definitionValue, '');
81-
82-
return;
83-
}
84-
foreach ($definitionValue as $propertyDefinition) {
85-
if (isset($propertyDefinition['$ref'])) {
86-
$ref = ReferenceType::fromDefinition($propertyDefinition, '');
79+
switch (true) {
80+
case isset($definitionValue['type']):
81+
$self->$key[] = Type::fromDefinition($definitionValue, '');
82+
break;
83+
case isset($definitionValue['$ref']):
84+
$ref = ReferenceType::fromDefinition($definitionValue, '');
8785

88-
if ($resolvedType = $resolveReference($propertyDefinition['$ref'])) {
89-
$ref->setResolvedType($resolveReference($propertyDefinition['$ref']));
86+
if ($resolvedType = $resolveReference($definitionValue['$ref'])) {
87+
$ref->setResolvedType($resolveReference($definitionValue['$ref']));
9088
}
91-
9289
$self->$key[] = new TypeSet($ref);
93-
} else {
94-
$self->$key[] = Type::fromDefinition(
95-
isset($propertyDefinition[0])
96-
? $definitionValue
97-
: $propertyDefinition,
98-
''
99-
);
100-
}
90+
break;
91+
default:
92+
foreach ($definitionValue as $propertyDefinition) {
93+
if (isset($propertyDefinition['$ref'])) {
94+
$ref = ReferenceType::fromDefinition($propertyDefinition, '');
95+
96+
if ($resolvedType = $resolveReference($propertyDefinition['$ref'])) {
97+
$ref->setResolvedType($resolveReference($propertyDefinition['$ref']));
98+
}
99+
100+
$self->$key[] = new TypeSet($ref);
101+
} else {
102+
$self->$key[] = Type::fromDefinition(
103+
isset($propertyDefinition[0])
104+
? $definitionValue
105+
: $propertyDefinition,
106+
''
107+
);
108+
}
109+
}
110+
break;
101111
}
102112
};
103113

tests/Type/ArrayTypeTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ public function it_supports_array_with_one_type(): void
6565
$this->assertItemOne($items[0]);
6666
}
6767

68+
/**
69+
* @test
70+
*/
71+
public function it_supports_array_with_one_type_ref(): void
72+
{
73+
$json = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_array_one_type_ref.json');
74+
$decodedJson = \json_decode($json, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
75+
76+
$typeSet = Type::fromDefinition($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+
$this->assertItemThree($items[0]);
89+
}
90+
6891
/**
6992
* @test
7093
*/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"type": "array",
3+
"items": {
4+
"$ref": "#/definitions/name"
5+
},
6+
"definitions": {
7+
"name": {
8+
"type": [
9+
"string",
10+
"null"
11+
],
12+
"minLength": 2
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)