Skip to content

Commit 1861371

Browse files
committed
fix: include $id in schema resolving as draft-06 and upwards use $id as keyword
1 parent 2f6cecc commit 1861371

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/JsonSchema/SchemaStorage.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ private function expandRefs(&$schema, ?string $parentId = null): void
106106
continue;
107107
}
108108

109+
$schemaId = $this->findSchemaIdInObject($schema);
109110
$childId = $parentId;
110-
if (property_exists($schema, 'id') && is_string($schema->id) && $childId !== $schema->id) {
111-
$childId = $this->uriResolver->resolve($schema->id, $childId);
111+
if (is_string($schemaId) && $childId !== $schemaId) {
112+
$childId = $this->uriResolver->resolve($schemaId, $childId);
112113
}
113114

114115
$this->expandRefs($member, $childId);
@@ -196,17 +197,30 @@ private function scanForSubschemas($schema, string $parentId): void
196197
continue;
197198
}
198199

199-
if (property_exists($potentialSubSchema, 'id') && is_string($potentialSubSchema->id) && property_exists($potentialSubSchema, 'type')) {
200+
$potentialSubSchemaId = $this->findSchemaIdInObject($potentialSubSchema);
201+
if (is_string($potentialSubSchemaId) && property_exists($potentialSubSchema, 'type')) {
200202
// Enum and const don't allow id as a keyword, see https://github.com/json-schema-org/JSON-Schema-Test-Suite/pull/471
201203
if (in_array($propertyName, ['enum', 'const'])) {
202204
continue;
203205
}
204206

205207
// Found sub schema
206-
$this->addSchema($this->uriResolver->resolve($potentialSubSchema->id, $parentId), $potentialSubSchema);
208+
$this->addSchema($this->uriResolver->resolve($potentialSubSchemaId, $parentId), $potentialSubSchema);
207209
}
208210

209211
$this->scanForSubschemas($potentialSubSchema, $parentId);
210212
}
211213
}
214+
215+
private function findSchemaIdInObject(object $schema): ?string
216+
{
217+
if (property_exists($schema, 'id') && is_string($schema->id)) {
218+
return $schema->id;
219+
}
220+
if (property_exists($schema, '$id') && is_string($schema->{'$id'})) {
221+
return $schema->{'$id'};
222+
}
223+
224+
return null;
225+
}
212226
}

src/JsonSchema/Validator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public function validate(&$value, $schema = null, ?int $checkMode = null): int
6161
if (LooseTypeCheck::propertyExists($schema, 'id')) {
6262
$schemaURI = LooseTypeCheck::propertyGet($schema, 'id');
6363
}
64+
if (LooseTypeCheck::propertyExists($schema, '$id')) {
65+
$schemaURI = LooseTypeCheck::propertyGet($schema, '$id');
66+
}
6467
$this->factory->getSchemaStorage()->addSchema($schemaURI, $schema);
6568

6669
$validator = $this->factory->createInstanceFor('schema');

0 commit comments

Comments
 (0)