Skip to content

Commit a85ac59

Browse files
committed
fix: dont register schema or expand refs when found in enum or const
1 parent 55c7490 commit a85ac59

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/JsonSchema/SchemaStorage.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function addSchema(string $id, $schema = null): void
7070
}
7171
}
7272

73-
$this->addSubschemas($schema, $id);
73+
$this->scanForSubschemas($schema, $id);
7474

7575
// resolve references
7676
$this->expandRefs($schema, $id);
@@ -100,7 +100,12 @@ private function expandRefs(&$schema, ?string $parentId = null): void
100100
$schema->{'$ref'} = (string) $refPointer;
101101
}
102102

103-
foreach ($schema as &$member) {
103+
foreach ($schema as $propertyName => &$member) {
104+
if (in_array($propertyName, ['enum', 'const'])) {
105+
// Enum and const don't allow $ref as a keyword, see https://github.com/json-schema-org/JSON-Schema-Test-Suite/pull/445
106+
continue;
107+
}
108+
104109
$childId = $parentId;
105110
if (property_exists($schema, 'id') && is_string($schema->id) && $childId !== $schema->id) {
106111
$childId = $this->uriResolver->resolve($schema->id, $childId);
@@ -180,23 +185,28 @@ public function resolveRefSchema($refSchema, $resolveStack = [])
180185
/**
181186
* @param mixed $schema
182187
*/
183-
private function addSubschemas($schema, string $parentId): void
188+
private function scanForSubschemas($schema, string $parentId): void
184189
{
185190
if (!$schema instanceof \stdClass && !is_array($schema)) {
186191
return;
187192
}
188193

189-
foreach ($schema as $potentialSubSchema) {
194+
foreach ($schema as $propertyName => $potentialSubSchema) {
190195
if (!is_object($potentialSubSchema)) {
191196
continue;
192197
}
193198

194-
// Found sub schema
195199
if (property_exists($potentialSubSchema, 'id') && is_string($potentialSubSchema->id) && property_exists($potentialSubSchema, 'type')) {
196-
$this->addSchema($parentId . $potentialSubSchema->id, $potentialSubSchema);
200+
// Enum and const don't allow id as a keyword, see https://github.com/json-schema-org/JSON-Schema-Test-Suite/pull/471
201+
if (in_array($propertyName, ['enum', 'const'])) {
202+
continue;
203+
}
204+
205+
// Found sub schema
206+
$this->addSchema($this->uriResolver->resolve($potentialSubSchema->id, $parentId), $potentialSubSchema);
197207
}
198208

199-
$this->addSubschemas($potentialSubSchema, $parentId);
209+
$this->scanForSubschemas($potentialSubSchema, $parentId);
200210
}
201211
}
202212
}

0 commit comments

Comments
 (0)