From 259e5ec0baf24ef06b7ae9c1667d1dce23374542 Mon Sep 17 00:00:00 2001 From: Martin Schilling Date: Fri, 11 Oct 2019 13:36:56 +0200 Subject: [PATCH] Fixed bug where EnumType::asNullable() would reject null --- src/Type/EnumType.php | 13 ++++++++++ src/Type/NullableType.php | 21 +++++++++++----- tests/Type/EnumTypeTest.php | 50 +++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 tests/Type/EnumTypeTest.php diff --git a/src/Type/EnumType.php b/src/Type/EnumType.php index d47a136..6c70997 100644 --- a/src/Type/EnumType.php +++ b/src/Type/EnumType.php @@ -13,6 +13,7 @@ use EventEngine\JsonSchema\AnnotatedType; use EventEngine\JsonSchema\JsonSchema; +use EventEngine\JsonSchema\Type; class EnumType implements AnnotatedType { @@ -41,4 +42,16 @@ public function toArray(): array 'enum' => $this->entries, ], $this->annotations()); } + + public function asNullable(): Type + { + $cp = clone $this; + + $this->assertTypeCanBeConvertedToNullableType($cp); + + $cp->type = [$cp->type, JsonSchema::TYPE_NULL]; + $cp->entries[] = null; + + return $cp; + } } diff --git a/src/Type/NullableType.php b/src/Type/NullableType.php index 0360ebe..24983d4 100644 --- a/src/Type/NullableType.php +++ b/src/Type/NullableType.php @@ -20,16 +20,25 @@ public function asNullable(): Type { $cp = clone $this; + $this->assertTypeCanBeConvertedToNullableType($cp); + + $cp->type = [$cp->type, JsonSchema::TYPE_NULL]; + + return $cp; + } + + protected function assertTypeCanBeConvertedToNullableType(Type $cp): void + { if (! isset($cp->type)) { - throw new \RuntimeException('Type cannot be converted to nullable type. No json schema type set for ' . \get_class($this)); + throw new \RuntimeException( + 'Type cannot be converted to nullable type. No json schema type set for ' . \get_class($this) + ); } if (! \is_string($cp->type)) { - throw new \RuntimeException('Type cannot be converted to nullable type. JSON schema type is not a string'); + throw new \RuntimeException( + 'Type cannot be converted to nullable type. JSON schema type is not a string' + ); } - - $cp->type = [$cp->type, JsonSchema::TYPE_NULL]; - - return $cp; } } diff --git a/tests/Type/EnumTypeTest.php b/tests/Type/EnumTypeTest.php new file mode 100644 index 0000000..34f487c --- /dev/null +++ b/tests/Type/EnumTypeTest.php @@ -0,0 +1,50 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace EventEngineTest\JsonSchema\Type; + +use EventEngine\JsonSchema\Type\EnumType; +use EventEngineTest\JsonSchema\BasicTestCase; + +final class EnumTypeTest extends BasicTestCase +{ + /** + * @test + */ + public function it_creates_enum_type() + { + $enumType = new EnumType('a', 'b'); + + $this->assertEquals( + [ + 'type' => 'string', + 'enum' => ['a', 'b'], + ], + $enumType->toArray() + ); + } + + /** + * @test + */ + public function it_creates_nullable_enum_type() + { + $enumType = (new EnumType('a', 'b'))->asNullable(); + + $this->assertEquals( + [ + 'type' => ['string', 'null'], + 'enum' => ['a', 'b', null] + ], + $enumType->toArray() + ); + } +}