diff --git a/src/ValueObject/Valid/Schema.php b/src/ValueObject/Valid/Schema.php index 4aff3f8..e4d89fd 100644 --- a/src/ValueObject/Valid/Schema.php +++ b/src/ValueObject/Valid/Schema.php @@ -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; } diff --git a/src/ValueObject/Valid/V30/Schema.php b/src/ValueObject/Valid/V30/Schema.php index 9f60a75..09a05ba 100644 --- a/src/ValueObject/Valid/V30/Schema.php +++ b/src/ValueObject/Valid/V30/Schema.php @@ -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) ? diff --git a/tests/ValueObject/Valid/V30/SchemaTest.php b/tests/ValueObject/Valid/V30/SchemaTest.php index 04d027a..b097732 100644 --- a/tests/ValueObject/Valid/V30/SchemaTest.php +++ b/tests/ValueObject/Valid/V30/SchemaTest.php @@ -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 = [ @@ -451,4 +464,55 @@ public static function provideSchemasWithMin(): Generator ), ]; } + + /** + * @return \Generator + */ + 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), + ]; + } }