Skip to content

Commit dc8830d

Browse files
author
Alexander Miertsch
authored
Merge pull request #5 from martin-schilling/master
Fixed bug where EnumType::asNullable() would reject null
2 parents 5024779 + 259e5ec commit dc8830d

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

src/Type/EnumType.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use EventEngine\JsonSchema\AnnotatedType;
1515
use EventEngine\JsonSchema\JsonSchema;
16+
use EventEngine\JsonSchema\Type;
1617

1718
class EnumType implements AnnotatedType
1819
{
@@ -41,4 +42,16 @@ public function toArray(): array
4142
'enum' => $this->entries,
4243
], $this->annotations());
4344
}
45+
46+
public function asNullable(): Type
47+
{
48+
$cp = clone $this;
49+
50+
$this->assertTypeCanBeConvertedToNullableType($cp);
51+
52+
$cp->type = [$cp->type, JsonSchema::TYPE_NULL];
53+
$cp->entries[] = null;
54+
55+
return $cp;
56+
}
4457
}

src/Type/NullableType.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,25 @@ public function asNullable(): Type
2020
{
2121
$cp = clone $this;
2222

23+
$this->assertTypeCanBeConvertedToNullableType($cp);
24+
25+
$cp->type = [$cp->type, JsonSchema::TYPE_NULL];
26+
27+
return $cp;
28+
}
29+
30+
protected function assertTypeCanBeConvertedToNullableType(Type $cp): void
31+
{
2332
if (! isset($cp->type)) {
24-
throw new \RuntimeException('Type cannot be converted to nullable type. No json schema type set for ' . \get_class($this));
33+
throw new \RuntimeException(
34+
'Type cannot be converted to nullable type. No json schema type set for ' . \get_class($this)
35+
);
2536
}
2637

2738
if (! \is_string($cp->type)) {
28-
throw new \RuntimeException('Type cannot be converted to nullable type. JSON schema type is not a string');
39+
throw new \RuntimeException(
40+
'Type cannot be converted to nullable type. JSON schema type is not a string'
41+
);
2942
}
30-
31-
$cp->type = [$cp->type, JsonSchema::TYPE_NULL];
32-
33-
return $cp;
3443
}
3544
}

tests/Type/EnumTypeTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* This file is part of the event-engine/php-json-schema.
4+
* (c) 2017-2019 prooph software GmbH <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace EventEngineTest\JsonSchema\Type;
13+
14+
use EventEngine\JsonSchema\Type\EnumType;
15+
use EventEngineTest\JsonSchema\BasicTestCase;
16+
17+
final class EnumTypeTest extends BasicTestCase
18+
{
19+
/**
20+
* @test
21+
*/
22+
public function it_creates_enum_type()
23+
{
24+
$enumType = new EnumType('a', 'b');
25+
26+
$this->assertEquals(
27+
[
28+
'type' => 'string',
29+
'enum' => ['a', 'b'],
30+
],
31+
$enumType->toArray()
32+
);
33+
}
34+
35+
/**
36+
* @test
37+
*/
38+
public function it_creates_nullable_enum_type()
39+
{
40+
$enumType = (new EnumType('a', 'b'))->asNullable();
41+
42+
$this->assertEquals(
43+
[
44+
'type' => ['string', 'null'],
45+
'enum' => ['a', 'b', null]
46+
],
47+
$enumType->toArray()
48+
);
49+
}
50+
}

0 commit comments

Comments
 (0)