Skip to content

Commit

Permalink
Add support for array type $ref - Close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrokeil committed Nov 20, 2020
1 parent 13c1bf8 commit 5968b14
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 19 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,23 @@
All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 0.1.0 - TBD

### Added

- Nothing.

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.
48 changes: 29 additions & 19 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,38 @@ public static function fromDefinition(array $definition, ?string $name = null):
};

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

return;
}
foreach ($definitionValue as $propertyDefinition) {
if (isset($propertyDefinition['$ref'])) {
$ref = ReferenceType::fromDefinition($propertyDefinition, '');
switch (true) {
case isset($definitionValue['type']):
$self->$key[] = Type::fromDefinition($definitionValue, '');
break;
case isset($definitionValue['$ref']):
$ref = ReferenceType::fromDefinition($definitionValue, '');

if ($resolvedType = $resolveReference($propertyDefinition['$ref'])) {
$ref->setResolvedType($resolveReference($propertyDefinition['$ref']));
if ($resolvedType = $resolveReference($definitionValue['$ref'])) {
$ref->setResolvedType($resolveReference($definitionValue['$ref']));
}

$self->$key[] = new TypeSet($ref);
} else {
$self->$key[] = Type::fromDefinition(
isset($propertyDefinition[0])
? $definitionValue
: $propertyDefinition,
''
);
}
break;
default:
foreach ($definitionValue as $propertyDefinition) {
if (isset($propertyDefinition['$ref'])) {
$ref = ReferenceType::fromDefinition($propertyDefinition, '');

if ($resolvedType = $resolveReference($propertyDefinition['$ref'])) {
$ref->setResolvedType($resolveReference($propertyDefinition['$ref']));
}

$self->$key[] = new TypeSet($ref);
} else {
$self->$key[] = Type::fromDefinition(
isset($propertyDefinition[0])
? $definitionValue
: $propertyDefinition,
''
);
}
}
break;
}
};

Expand Down
23 changes: 23 additions & 0 deletions tests/Type/ArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ public function it_supports_array_with_one_type(): void
$this->assertItemOne($items[0]);
}

/**
* @test
*/
public function it_supports_array_with_one_type_ref(): void
{
$json = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_array_one_type_ref.json');
$decodedJson = \json_decode($json, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);

$typeSet = Type::fromDefinition($decodedJson);

$this->assertCount(1, $typeSet);

/** @var ArrayType $type */
$type = $typeSet->first();

$this->assertInstanceOf(ArrayType::class, $type);

$items = $type->items();
$this->assertCount(1, $items);

$this->assertItemThree($items[0]);
}

/**
* @test
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/Type/_files/schema_with_array_one_type_ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "array",
"items": {
"$ref": "#/definitions/name"
},
"definitions": {
"name": {
"type": [
"string",
"null"
],
"minLength": 2
}
}
}

0 comments on commit 5968b14

Please sign in to comment.