Skip to content

Commit

Permalink
Correct format for id & $ref in draft-03/04 meta-schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
erayd committed May 3, 2017
1 parent 7e9b2ce commit 4f050db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/JsonSchema/SchemaStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'})) {
Expand Down
13 changes: 13 additions & 0 deletions tests/SchemaStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 4f050db

Please sign in to comment.