Skip to content

Commit 8b2bca6

Browse files
authored
Merge pull request #80 from wol-soft/issue79_CombinedRefAndObjectDefinition
Combined Ref and object definition
2 parents d3caa60 + 30807e3 commit 8b2bca6

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/Model/SchemaDefinition/JsonSchema.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ class JsonSchema
4646
*/
4747
public function __construct(string $file, array $json)
4848
{
49+
// wrap in an allOf to pass the processing to multiple handlers - ugly hack to be removed after rework
50+
if (isset($json['$ref']) && count(array_diff(array_intersect(array_keys($json), self::SCHEMA_SIGNATURE_RELEVANT_FIELDS), ['$ref', 'type']))) {
51+
$json = array_merge(
52+
array_diff_key($json, array_fill_keys(self::SCHEMA_SIGNATURE_RELEVANT_FIELDS, null)),
53+
[
54+
'allOf' => [
55+
['$ref' => $json['$ref']],
56+
array_intersect_key(
57+
$json,
58+
array_fill_keys(array_diff(self::SCHEMA_SIGNATURE_RELEVANT_FIELDS, ['$ref']), null),
59+
),
60+
],
61+
],
62+
);
63+
}
64+
4965
$this->json = $json;
5066
$this->file = $file;
5167
}

tests/Issues/Issue/Issue79Test.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPModelGenerator\Tests\Issues\Issue;
6+
7+
use PHPModelGenerator\Exception\ErrorRegistryException;
8+
use PHPModelGenerator\Model\GeneratorConfiguration;
9+
use PHPModelGenerator\Tests\Issues\AbstractIssueTest;
10+
11+
class Issue79Test extends AbstractIssueTest
12+
{
13+
public function testCombinedReferenceAndObjectDefinition(): void
14+
{
15+
$className = $this->generateClassFromFile('person.json');
16+
17+
$object = new $className(['name' => 'Hans', 'street' => 'A28']);
18+
19+
$this->assertSame('Hans', $object->getName());
20+
$this->assertSame('A28', $object->getStreet());
21+
$this->assertNull($object->getAge());
22+
$this->assertNull($object->getZip());
23+
}
24+
25+
/**
26+
* @dataProvider invalidInputDataProvider
27+
*/
28+
public function testCombinedReferenceAndObjectDefinitionWithInvalidDataThrowsAnException(array $data): void
29+
{
30+
$className = $this->generateClassFromFile(
31+
'person.json',
32+
(new GeneratorConfiguration())->setCollectErrors(true),
33+
);
34+
35+
$this->expectException(ErrorRegistryException::class);
36+
37+
new $className(['name' => 23, 'age' => 'A28']);
38+
}
39+
40+
public static function invalidInputDataProvider(): array
41+
{
42+
return [
43+
'empty input' => [[]],
44+
'empty reference' => [['name' => 'Hans']],
45+
'empty object' => [['street' => 'A28']],
46+
'invalid reference' => [['name' => 'Hans', 'street' => 'A28', 'zip' => 'ABC']],
47+
'invalid object' => [['name' => 'Hans', 'street' => 'A28', 'age' => 'ABC']],
48+
];
49+
}
50+
}

tests/Schema/Issues/79/person.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"definitions": {
3+
"location": {
4+
"type": "object",
5+
"properties": {
6+
"street": {
7+
"type": "string"
8+
},
9+
"zip": {
10+
"type": "integer"
11+
}
12+
},
13+
"required": [
14+
"street"
15+
]
16+
}
17+
},
18+
"$id": "Person",
19+
"type": "object",
20+
"properties": {
21+
"name": {
22+
"type": "string"
23+
},
24+
"age": {
25+
"type": "integer"
26+
}
27+
},
28+
"$ref": "#/definitions/location",
29+
"required": [
30+
"name"
31+
]
32+
}

0 commit comments

Comments
 (0)