From bd40dbb9e04f713c35851f5bc40eadbea96c5d37 Mon Sep 17 00:00:00 2001 From: Erayd Date: Wed, 17 May 2017 04:08:16 +1200 Subject: [PATCH] [BUGFIX] Split "uri" format into "uri" & "uri-reference", fix meta-schema bug (#419) * Split "uri" format into "uri" and "uri-reference" * 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 --- .../Constraints/FormatConstraint.php | 7 +++++++ src/JsonSchema/SchemaStorage.php | 11 +++++++++++ tests/Constraints/FormatTest.php | 18 ++++++++++++------ tests/SchemaStorageTest.php | 13 +++++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/JsonSchema/Constraints/FormatConstraint.php b/src/JsonSchema/Constraints/FormatConstraint.php index c172847f..578cdb14 100644 --- a/src/JsonSchema/Constraints/FormatConstraint.php +++ b/src/JsonSchema/Constraints/FormatConstraint.php @@ -80,6 +80,13 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i = break; case 'uri': + if (null === filter_var($element, FILTER_VALIDATE_URL, FILTER_NULL_ON_FAILURE)) { + $this->addError($path, 'Invalid URL format', 'format', array('format' => $schema->format)); + } + break; + + case 'uriref': + case 'uri-reference': if (null === filter_var($element, FILTER_VALIDATE_URL, FILTER_NULL_ON_FAILURE)) { // FILTER_VALIDATE_URL does not conform to RFC-3986, and cannot handle relative URLs, but // the json-schema spec uses RFC-3986, so need a bit of hackery to properly validate them. diff --git a/src/JsonSchema/SchemaStorage.php b/src/JsonSchema/SchemaStorage.php index 3c17c8c4..9a1caeb4 100644 --- a/src/JsonSchema/SchemaStorage.php +++ b/src/JsonSchema/SchemaStorage.php @@ -58,6 +58,17 @@ public function addSchema($id, $schema = null) $schema = BaseConstraint::arrayToObjectRecursive($schema); } + // 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/Constraints/FormatTest.php b/tests/Constraints/FormatTest.php index ad7075d9..b035aafe 100644 --- a/tests/Constraints/FormatTest.php +++ b/tests/Constraints/FormatTest.php @@ -143,12 +143,12 @@ public function getValidFormats() array('555 320 1212', 'phone'), array('http://bluebox.org', 'uri'), - array('//bluebox.org', 'uri'), - array('/absolutePathReference/', 'uri'), - array('./relativePathReference/', 'uri'), - array('./relative:PathReference/', 'uri'), - array('relativePathReference/', 'uri'), - array('relative/Path:Reference/', 'uri'), + array('//bluebox.org', 'uri-reference'), + array('/absolutePathReference/', 'uri-reference'), + array('./relativePathReference/', 'uri-reference'), + array('./relative:PathReference/', 'uri-reference'), + array('relativePathReference/', 'uri-reference'), + array('relative/Path:Reference/', 'uri-reference'), array('info@something.edu', 'email'), @@ -200,6 +200,12 @@ public function getInvalidFormats() array('htt:/bluebox.org', 'uri'), array('.relative:path/reference/', 'uri'), array('', 'uri'), + array('//bluebox.org', 'uri'), + array('/absolutePathReference/', 'uri'), + array('./relativePathReference/', 'uri'), + array('./relative:PathReference/', 'uri'), + array('relativePathReference/', 'uri'), + array('relative/Path:Reference/', 'uri'), array('info@somewhere', 'email'), 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); + } }