Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ValueObject/Valid/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace Membrane\OpenAPIReader\ValueObject\Valid;

use Membrane\OpenAPIReader\ValueObject\Limit;
use Membrane\OpenAPIReader\ValueObject\Valid\Enum\Type;

interface Schema
{
/** @return Type[] */
public function getTypes(): array;
public function getRelevantMinimum(): ?Limit;
public function getRelevantMaximum(): ?Limit;
}
14 changes: 14 additions & 0 deletions src/ValueObject/Valid/V30/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ public function canBePrimitive(): bool
return false;
}

/** @return Type[] */
public function getTypes(): array
{
$result = isset($this->type) ?
[$this->type] :
Type::casesForVersion(OpenAPIVersion::Version_3_0);

if ($this->nullable) {
$result[] = Type::Null;
}

return $result;
}

public function getRelevantMaximum(): ?Limit
{
return isset($this->maximum) ?
Expand Down
64 changes: 64 additions & 0 deletions tests/ValueObject/Valid/V30/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ public function itGetsRelevantMinimum(?Limit $expected, Partial\Schema $schema):
self::assertEquals($expected, $sut->getRelevantMinimum());
}

/**
* @param Type[] $expected
*/
#[Test]
#[TestDox('It gets the types allowed, in a version agnostic format')]
#[DataProvider('provideSchemasToGetTypes')]
public function itGetsTypes(array $expected, Partial\Schema $schema): void
{
$sut = new Schema(new Identifier(''), $schema);

self::assertEqualsCanonicalizing($expected, $sut->getTypes());
}

public static function provideInvalidComplexSchemas(): Generator
{
$xOfs = [
Expand Down Expand Up @@ -451,4 +464,55 @@ public static function provideSchemasWithMin(): Generator
),
];
}

/**
* @return \Generator<array{ 0:Type[], 1:Partial\Schema }>
*/
public static function provideSchemasToGetTypes(): Generator
{
yield 'no type' => [
Type::casesForVersion(OpenAPIVersion::Version_3_0),
PartialHelper::createSchema(),
];

yield 'nullable' => [
[Type::Null, ...Type::casesForVersion(OpenAPIVersion::Version_3_0)],
PartialHelper::createSchema(nullable: true)
];

yield 'string' => [[Type::String], PartialHelper::createSchema(type: 'string')];
yield 'integer' => [[Type::Integer], PartialHelper::createSchema(type: 'integer')];
yield 'number' => [[Type::Number], PartialHelper::createSchema(type: 'number')];
yield 'boolean' => [[Type::Boolean], PartialHelper::createSchema(type: 'boolean')];
yield 'array' => [
[Type::Array],
PartialHelper::createSchema(type: 'array', items: PartialHelper::createSchema()),
];
yield 'object' => [[Type::Object], PartialHelper::createSchema(type: 'object')];

yield 'nullable string' => [
[Type::String, Type::Null],
PartialHelper::createSchema(type: 'string', nullable: true),
];
yield 'nullable integer' => [
[Type::Integer, Type::Null],
PartialHelper::createSchema(type: 'integer', nullable: true),
];
yield 'nullable number' => [
[Type::Number, Type::Null],
PartialHelper::createSchema(type: 'number', nullable: true),
];
yield 'nullable boolean' => [
[Type::Boolean, Type::Null],
PartialHelper::createSchema(type: 'boolean', nullable: true),
];
yield 'nullable array' => [
[Type::Array, Type::Null],
PartialHelper::createSchema(type: 'array', nullable: true, items: PartialHelper::createSchema()),
];
yield 'nullable object' => [
[Type::Object, Type::Null],
PartialHelper::createSchema(type: 'object', nullable: true),
];
}
}