Skip to content

Commit c241986

Browse files
Fix enum/const property name handling in SchemaStorage
Add conditional check to only skip enum/const when they are schema keywords, not property/definition names. This fixes the issue where definitions named 'enum' or 'const' were being skipped during ref expansion. Co-authored-by: DannyvdSluijs <618940+DannyvdSluijs@users.noreply.github.com>
1 parent 5f291dc commit c241986

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

src/JsonSchema/SchemaStorage.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ private function expandRefs(&$schema, ?string $parentId = null): void
101101
}
102102

103103
foreach ($schema as $propertyName => &$member) {
104-
if (in_array($propertyName, ['enum', 'const'])) {
104+
// Only skip enum/const if we're in an actual schema object (has validation keywords),
105+
// not in a container object like definitions where 'enum' might be a property/definition name
106+
if (in_array($propertyName, ['enum', 'const']) && $this->isSchemaObject($schema)) {
105107
// Enum and const don't allow $ref as a keyword, see https://github.com/json-schema-org/JSON-Schema-Test-Suite/pull/445
106108
continue;
107109
}
@@ -233,4 +235,30 @@ private function findSchemaIdInObject(object $schema): ?string
233235

234236
return null;
235237
}
238+
239+
/**
240+
* Check if an object appears to be a JSON Schema object (with validation keywords)
241+
* vs a pure container object (like definitions, properties containers)
242+
*/
243+
private function isSchemaObject(object $schema): bool
244+
{
245+
// Common JSON Schema keywords that indicate this is a schema, not a container
246+
$schemaKeywords = [
247+
'type', 'properties', 'items', 'additionalProperties', 'additionalItems',
248+
'required', 'allOf', 'anyOf', 'oneOf', 'not', 'format',
249+
'minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum',
250+
'minLength', 'maxLength', 'pattern', 'minItems', 'maxItems',
251+
'uniqueItems', 'minProperties', 'maxProperties', 'multipleOf',
252+
'$ref', '$schema', 'id', '$id', 'title', 'description',
253+
'default', 'examples', 'patternProperties', 'dependencies'
254+
];
255+
256+
foreach ($schemaKeywords as $keyword) {
257+
if (property_exists($schema, $keyword)) {
258+
return true;
259+
}
260+
}
261+
262+
return false;
263+
}
236264
}

tests/SchemaStorageTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,45 @@ public function testNoDoubleResolve(): void
305305
$schemas['test/schema']->{'$ref'}
306306
);
307307
}
308+
309+
/**
310+
* Test that definitions named 'enum' or 'const' are properly resolved
311+
* Regression test for: https://github.com/jsonrainbow/json-schema/issues/XXX
312+
*/
313+
public function testDefinitionNamedEnumIsResolved(): void
314+
{
315+
// Schema with a definition named 'enum' that contains a $ref
316+
$schema = (object) [
317+
'id' => 'http://example.com/schema.json',
318+
'definitions' => (object) [
319+
'enum' => (object) [
320+
'$ref' => 'http://json-schema.org/draft-04/schema#/properties/enum'
321+
],
322+
'const' => (object) [
323+
'$ref' => 'http://json-schema.org/draft-04/schema#/properties/const'
324+
]
325+
]
326+
];
327+
328+
$uriRetriever = $this->prophesize(\JsonSchema\UriRetrieverInterface::class);
329+
$uriRetriever->retrieve('http://example.com/schema.json')
330+
->willReturn($schema)
331+
->shouldBeCalled();
332+
333+
$schemaStorage = new SchemaStorage($uriRetriever->reveal());
334+
$schemaStorage->addSchema('http://example.com/schema.json');
335+
336+
// Verify the refs in the 'enum' and 'const' definitions were expanded
337+
$storedSchema = $schemaStorage->getSchema('http://example.com/schema.json');
338+
339+
// The $ref should have been expanded to include the full URI
340+
$this->assertStringContainsString(
341+
'http://json-schema.org/draft-04/schema#',
342+
$storedSchema->definitions->enum->{'$ref'}
343+
);
344+
$this->assertStringContainsString(
345+
'http://json-schema.org/draft-04/schema#',
346+
$storedSchema->definitions->const->{'$ref'}
347+
);
348+
}
308349
}

0 commit comments

Comments
 (0)