Skip to content

Commit 70b1c4c

Browse files
authored
Merge pull request #7 from open-code-modeling/0.2.x-merge-up-into-0.3.x_5fd0a9322eed06.63718349
Merge release 0.2.1 into 0.3.x
2 parents da06569 + 09c339a commit 70b1c4c

File tree

7 files changed

+149
-19
lines changed

7 files changed

+149
-19
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5+
56
## 0.3.0 - TBD
67

78
### Added
@@ -24,6 +25,25 @@ All notable changes to this project will be documented in this file, in reverse
2425

2526
- Nothing.
2627

28+
## 0.2.1 - 2020-12-09
29+
30+
31+
-----
32+
33+
### Release Notes for [0.2.1](https://github.com/open-code-modeling/json-schema-to-php/milestone/5)
34+
35+
0.2.x bugfix release (patch)
36+
37+
### 0.2.1
38+
39+
- Total issues resolved: **1**
40+
- Total pull requests resolved: **0**
41+
- Total contributors: **1**
42+
43+
#### bug
44+
45+
- [6: Support enums without type](https://github.com/open-code-modeling/json-schema-to-php/issues/6) thanks to @sandrokeil
46+
2747
## 0.2.0 - 2020-12-04
2848

2949

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
"@analyse",
5252
"@test"
5353
],
54-
"cs": "php-cs-fixer fix src -v --diff --dry-run",
55-
"cs-fix": "php-cs-fixer fix src -v --diff",
54+
"cs": "php-cs-fixer fix -v --diff --dry-run",
55+
"cs-fix": "php-cs-fixer fix -v --diff",
5656
"test": "vendor/bin/phpunit",
5757
"analyse": "php vendor/bin/phpstan.phar analyse --no-interaction"
5858
},

src/Type/Type.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace OpenCodeModeling\JsonSchemaToPhp\Type;
1212

13+
use OpenCodeModeling\JsonSchemaToPhp\Exception\RuntimeException;
1314
use OpenCodeModeling\JsonSchemaToPhp\Shorthand\Shorthand;
1415

1516
final class Type
@@ -57,10 +58,38 @@ public static function fromDefinition(array $definition, ?string $name = null, a
5758
|| isset($definition['required']):
5859
$definition['type'] = ObjectType::type();
5960
break;
61+
case isset($definition['enum']):
62+
$enumType = '';
63+
64+
foreach ($definition['enum'] as $enumvalue) {
65+
$previousType = \gettype($enumvalue);
66+
67+
if ($enumType !== '' && $previousType !== $enumType) {
68+
throw new RuntimeException('Mixed enum types not supported');
69+
}
70+
$enumType = $previousType;
71+
}
72+
switch ($enumType) {
73+
case 'array':
74+
case 'string':
75+
case 'integer':
76+
case 'boolean':
77+
break;
78+
case 'float':
79+
case 'double':
80+
$enumType = 'number';
81+
break;
82+
default:
83+
throw new RuntimeException(
84+
\sprintf('The type "%s" is not supported in schema definition for "%s"', $enumType, $name)
85+
);
86+
}
87+
$definition['type'] = $enumType;
88+
break;
6089
case \count($definition) === 0:
6190
return new TypeSet(MixedType::fromDefinition($definition, $name));
6291
default:
63-
throw new \RuntimeException(\sprintf('The "type" is missing in schema definition for "%s"', $name));
92+
throw new RuntimeException(\sprintf('The "type" is missing in schema definition for "%s"', $name));
6493
}
6594
}
6695

@@ -113,14 +142,14 @@ public static function fromDefinition(array $definition, ?string $name = null, a
113142
$isNullable = true;
114143
break;
115144
default:
116-
throw new \RuntimeException(
145+
throw new RuntimeException(
117146
\sprintf('JSON schema type "%s" is not implemented', $definition['type'])
118147
);
119148
}
120149
}
121150

122151
if (\count($types) === 0) {
123-
throw new \RuntimeException('Could not determine type of JSON schema');
152+
throw new RuntimeException('Could not determine type of JSON schema');
124153
}
125154

126155
$typeSet = new TypeSet(...$types);

tests/Shorthand/ShorthandTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/json-schema-to-php for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/json-schema-to-php/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/json-schema-to-php/blob/master/LICENSE.md MIT License
7+
*/
8+
29
declare(strict_types=1);
310

411
namespace OpenCodeModelingTest\JsonSchemaToPhp\Shorthand;
@@ -31,7 +38,7 @@ public function it_converts_enum_shorthand_to_json_schema_string(): void
3138
$schema = Shorthand::convertToJsonSchema(['test' => 'enum|available|blocked|bought']);
3239

3340
$this->assertEquals($this->jsonSchemaObject(
34-
['test' => ['enum' => ["available", "blocked", "bought"]]],
41+
['test' => ['enum' => ['available', 'blocked', 'bought']]],
3542
['test']
3643
), $schema);
3744
}
@@ -117,7 +124,7 @@ public function it_parses_shorthand_validation_and_adds_it_to_json_schema(): voi
117124
'test2' => 'number|minimum:0.5|maximum:10',
118125
'test3' => 'string|null|format:email',
119126
'test4' => 'boolean|default:false',
120-
'test5' => 'boolean|null|default:true'
127+
'test5' => 'boolean|null|default:true',
121128
]);
122129

123130
$this->assertEquals($this->jsonSchemaObject(
@@ -178,7 +185,7 @@ public function it_converts_shorthand_object_to_json_schema_object(): void
178185
'searchProfile?' => [
179186
'roomsMin' => 'number|null|minimum:0.5',
180187
'roomsMax' => 'number|null|minimum:0.5',
181-
]
188+
],
182189
]);
183190

184191
$this->assertEquals(
@@ -194,7 +201,7 @@ public function it_converts_shorthand_object_to_json_schema_object(): void
194201
'searchProfile' => $this->jsonSchemaObject([
195202
'roomsMin' => ['type' => ['number', 'null'], 'minimum' => 0.5],
196203
'roomsMax' => ['type' => ['number', 'null'], 'minimum' => 0.5],
197-
], ['roomsMin', 'roomsMax'])
204+
], ['roomsMin', 'roomsMax']),
198205
], ['name', 'email', 'address', 'tags'], 'Prospect'),
199206
$schema
200207
);
@@ -212,10 +219,10 @@ private function jsonSchemaObject(array $properties, array $required = [], strin
212219
'type' => 'object',
213220
'properties' => $properties,
214221
'additionalProperties' => false,
215-
'required' => $required
222+
'required' => $required,
216223
];
217224

218-
if($title) {
225+
if ($title) {
219226
$obj['title'] = $title;
220227
}
221228

tests/Type/ArrayTypeTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
22

3+
/**
4+
* @see https://github.com/open-code-modeling/json-schema-to-php for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/json-schema-to-php/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/json-schema-to-php/blob/master/LICENSE.md MIT License
7+
*/
8+
39
declare(strict_types=1);
410

511
namespace OpenCodeModelingTest\JsonSchemaToPhp\Type;
@@ -20,7 +26,7 @@ final class ArrayTypeTest extends TestCase
2026
*/
2127
public function it_supports_array_type(): void
2228
{
23-
$json = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_array.json');
29+
$json = \file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_array.json');
2430
$decodedJson = \json_decode($json, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
2531

2632
$typeSet = Type::fromDefinition($decodedJson);
@@ -48,7 +54,7 @@ public function it_supports_array_type(): void
4854
*/
4955
public function it_supports_array_with_one_type(): void
5056
{
51-
$json = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_array_one_type.json');
57+
$json = \file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_array_one_type.json');
5258
$decodedJson = \json_decode($json, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
5359

5460
$typeSet = Type::fromDefinition($decodedJson);
@@ -71,7 +77,7 @@ public function it_supports_array_with_one_type(): void
7177
*/
7278
public function it_supports_array_with_one_type_ref(): void
7379
{
74-
$json = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_array_one_type_ref.json');
80+
$json = \file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_array_one_type_ref.json');
7581
$decodedJson = \json_decode($json, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
7682

7783
$typeSet = Type::fromDefinition($decodedJson);
@@ -102,7 +108,7 @@ public function it_supports_array_with_one_type_ref(): void
102108
*/
103109
public function it_supports_array_shorthand_with_no_ref(): void
104110
{
105-
$json = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_array_shorthand_no_ref.json');
111+
$json = \file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_array_shorthand_no_ref.json');
106112
$decodedJson = \json_decode($json, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
107113

108114
$typeSet = Type::fromShorthand($decodedJson);
@@ -191,7 +197,5 @@ private function assertItemFour(TypeSet $itemFour): void
191197
$itemFourType = $itemFour->first();
192198
$this->assertInstanceOf(StringType::class, $itemFourType);
193199
$this->assertCount(4, $itemFourType->enum());
194-
195200
}
196-
197201
}

tests/Type/ObjectTypeTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
22

3+
/**
4+
* @see https://github.com/open-code-modeling/json-schema-to-php for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/json-schema-to-php/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/json-schema-to-php/blob/master/LICENSE.md MIT License
7+
*/
8+
39
declare(strict_types=1);
410

511
namespace OpenCodeModelingTest\JsonSchemaToPhp\Type;
@@ -18,7 +24,7 @@ final class ObjectTypeTest extends TestCase
1824
*/
1925
public function it_supports_object_type(): void
2026
{
21-
$json = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_object.json');
27+
$json = \file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_object.json');
2228
$decodedJson = \json_decode($json, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
2329

2430
$typeSet = Type::fromDefinition($decodedJson);
@@ -79,7 +85,7 @@ public function it_supports_object_type(): void
7985
*/
8086
public function it_supports_definition_of_objects(): void
8187
{
82-
$json = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_objects.json');
88+
$json = \file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'schema_with_objects.json');
8389
$decodedJson = \json_decode($json, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
8490

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

tests/Type/TypeTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/json-schema-to-php for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/json-schema-to-php/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/json-schema-to-php/blob/master/LICENSE.md MIT License
7+
*/
8+
29
declare(strict_types=1);
310

411
namespace OpenCodeModelingTest\JsonSchemaToPhp\Type;
512

13+
use OpenCodeModeling\JsonSchemaToPhp\Type\IntegerType;
14+
use OpenCodeModeling\JsonSchemaToPhp\Type\NumberType;
615
use OpenCodeModeling\JsonSchemaToPhp\Type\ObjectType;
16+
use OpenCodeModeling\JsonSchemaToPhp\Type\StringType;
717
use OpenCodeModeling\JsonSchemaToPhp\Type\Type;
818
use PHPUnit\Framework\TestCase;
919

@@ -27,4 +37,58 @@ public function it_supports_shorthand_definition(): void
2737
$this->assertEquals('Person', $type->name());
2838
$this->assertEquals('Person', $type->title());
2939
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function it_supports_string_enums_without_type(): void
45+
{
46+
$typeSet = Type::fromShorthand(['contentLanguage' => 'enum|de-DE|en-US'], 'Content');
47+
48+
$this->assertCount(1, $typeSet);
49+
50+
/** @var ObjectType $type */
51+
$type = $typeSet->first();
52+
53+
$this->assertInstanceOf(ObjectType::class, $type);
54+
$this->assertArrayHasKey('contentLanguage', $type->properties());
55+
56+
$contentLanguage = $type->properties()['contentLanguage'];
57+
/** @var StringType $contentLanguageType */
58+
$contentLanguageType = $contentLanguage->first();
59+
$this->assertInstanceOf(StringType::class, $contentLanguageType);
60+
$this->assertSame(['de-DE', 'en-US'], $contentLanguageType->enum());
61+
}
62+
63+
/**
64+
* @test
65+
*/
66+
public function it_supports_int_enums_without_type(): void
67+
{
68+
$typeSet = Type::fromDefinition(['enum' => [10, 20]], 'Content');
69+
70+
$this->assertCount(1, $typeSet);
71+
72+
/** @var IntegerType $type */
73+
$type = $typeSet->first();
74+
75+
$this->assertInstanceOf(IntegerType::class, $type);
76+
$this->assertSame([10, 20], $type->enum());
77+
}
78+
79+
/**
80+
* @test
81+
*/
82+
public function it_supports_float_enums_without_type(): void
83+
{
84+
$typeSet = Type::fromDefinition(['enum' => [10.10, 20.20]], 'Content');
85+
86+
$this->assertCount(1, $typeSet);
87+
88+
/** @var NumberType $type */
89+
$type = $typeSet->first();
90+
91+
$this->assertInstanceOf(NumberType::class, $type);
92+
$this->assertSame([10.10, 20.20], $type->enum());
93+
}
3094
}

0 commit comments

Comments
 (0)