From 4f050db6665231650e97acf4f2d6fe46d3581d17 Mon Sep 17 00:00:00 2001 From: Erayd Date: Thu, 4 May 2017 09:43:03 +1200 Subject: [PATCH] Correct format for id & $ref in draft-03/04 meta-schemas See https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues/177#issuecomment-293051367 --- src/JsonSchema/SchemaStorage.php | 12 ++++++++++++ tests/SchemaStorageTest.php | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/JsonSchema/SchemaStorage.php b/src/JsonSchema/SchemaStorage.php index 3ee081e3..fe64d2f5 100644 --- a/src/JsonSchema/SchemaStorage.php +++ b/src/JsonSchema/SchemaStorage.php @@ -51,6 +51,18 @@ public function addSchema($id, $schema = null) // schemas do not have an associated URI when passed via Validator::validate(). $schema = $this->uriRetriever->retrieve($id); } + + // workaround for bug in draft-03 & draft-04 meta-schemas (id & $ref defined with incorrect format) + // see https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues/177#issuecomment-293051367 + if (is_object($schema) && property_exists($schema, 'id')) { + if ($schema->id == 'http://json-schema.org/draft-04/schema#') { + $schema->properties->id->format = 'uri-reference'; + } elseif ($schema->id == 'http://json-schema.org/draft-03/schema#') { + $schema->properties->id->format = 'uri-reference'; + $schema->properties->{'$ref'}->format = 'uri-reference'; + } + } + $objectIterator = new ObjectIterator($schema); foreach ($objectIterator as $toResolveSchema) { if (property_exists($toResolveSchema, '$ref') && is_string($toResolveSchema->{'$ref'})) { diff --git a/tests/SchemaStorageTest.php b/tests/SchemaStorageTest.php index 294f0f95..0e440ac8 100644 --- a/tests/SchemaStorageTest.php +++ b/tests/SchemaStorageTest.php @@ -289,4 +289,17 @@ public function testGetUriResolver() $s->addSchema('http://json-schema.org/draft-04/schema#'); $this->assertInstanceOf('\JsonSchema\Uri\UriResolver', $s->getUriResolver()); } + + public function testMetaSchemaFixes() + { + $s = new SchemaStorage(); + $s->addSchema('http://json-schema.org/draft-03/schema#'); + $s->addSchema('http://json-schema.org/draft-04/schema#'); + $draft_03 = $s->getSchema('http://json-schema.org/draft-03/schema#'); + $draft_04 = $s->getSchema('http://json-schema.org/draft-04/schema#'); + + $this->assertEquals('uri-reference', $draft_03->properties->id->format); + $this->assertEquals('uri-reference', $draft_03->properties->{'$ref'}->format); + $this->assertEquals('uri-reference', $draft_04->properties->id->format); + } }