Skip to content

Commit 0483f1c

Browse files
committed
Allow references to objects which provide an ID without trailing hash symbol
1 parent 8020d27 commit 0483f1c

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

src/Model/SchemaDefinition/SchemaDefinitionDictionary.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ public function setUpDefinitionDictionary(
7070
protected function fetchDefinitionsById(array $propertyData, SchemaProcessor $schemaProcessor, Schema $schema): void
7171
{
7272
if (isset($propertyData['$id'])) {
73-
$this->addDefinition($propertyData['$id'], new SchemaDefinition($propertyData, $schemaProcessor, $schema));
73+
$this->addDefinition(
74+
strpos($propertyData['$id'], '#') === 0 ? $propertyData['$id'] : "#{$propertyData['$id']}",
75+
new SchemaDefinition($propertyData, $schemaProcessor, $schema)
76+
);
7477
}
7578

7679
foreach ($propertyData as $item) {

src/Utils/ClassNameGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ public function getClassName(
2323
$className = sprintf(
2424
$isMergeClass ? '%s_Merged_%s' : '%s_%s',
2525
$currentClassName,
26-
ucfirst($schema['$id'] ?? ($propertyName . ($currentClassName ? uniqid() : '')))
26+
ucfirst(
27+
$schema['$id']
28+
? str_replace('#', '', $schema['$id'])
29+
: ($propertyName . ($currentClassName ? uniqid() : ''))
30+
)
2731
);
2832

2933
return ucfirst(preg_replace('/\W/', '', trim($className, '_')));

tests/Objects/ReferencePropertyTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ public function testNotProvidedOptionalReferenceObjectPropertyIsValid(string $re
8181
$this->assertNull($object->getPerson());
8282
}
8383

84+
public function testIdWithoutHashSymbolIsResolved(): void
85+
{
86+
$className = $this->generateClassFromFile('IdWithoutHashSymbolReference.json');
87+
88+
$object = new $className([]);
89+
$this->assertNull($object->getPerson());
90+
}
91+
8492
/**
8593
* @dataProvider validReferenceObjectInputProvider
8694
*
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"definitions": {
3+
"person": {
4+
"$id": "person",
5+
"type": "object",
6+
"properties": {
7+
"name": {
8+
"type": "string"
9+
},
10+
"age": {
11+
"type": "integer"
12+
}
13+
}
14+
}
15+
},
16+
"type": "object",
17+
"properties": {
18+
"person": {
19+
"$ref": "#person"
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)