From 232378f3c222277f99db88e551dd4908b9b15d92 Mon Sep 17 00:00:00 2001 From: Erayd Date: Sun, 4 Mar 2018 15:41:52 +1300 Subject: [PATCH 01/14] Remove large array type-specific performance optimisation Fixes #441. The bug in #441 was caused by refactoring of the optimisation which introduced a type-checking error. Noting the performance impact is negligible for all cases other than large arrays of strings or numbers, and introduces significant cognitive complexity to a project that is extremely short of maintainers, removing it seems like the best course of action. The performance improvement provided by this optimisation was approximately 40%, however it also carried a number of other problematic bugs - if it were to be reintroduced at a later date with those bugs fixed (mainly the skipping of much of the validation logic for optimised items, even in cases where that logic might be necessary), it would not have such a significant impact. --- .../Constraints/CollectionConstraint.php | 56 +++++-------------- 1 file changed, 15 insertions(+), 41 deletions(-) diff --git a/src/JsonSchema/Constraints/CollectionConstraint.php b/src/JsonSchema/Constraints/CollectionConstraint.php index 49841a49..d1384b88 100644 --- a/src/JsonSchema/Constraints/CollectionConstraint.php +++ b/src/JsonSchema/Constraints/CollectionConstraint.php @@ -65,53 +65,27 @@ protected function validateItems(&$value, $schema = null, JsonPointer $path = nu { if (is_object($schema->items)) { // just one type definition for the whole array + foreach ($value as $k => &$v) { + $initErrors = $this->getErrors(); - if (isset($schema->items->type) - && ( - $schema->items->type == 'string' - || $schema->items->type == 'number' - || $schema->items->type == 'integer' - ) - && !isset($schema->additionalItems) - ) { - // performance optimization - $type = $schema->items->type; - $typeValidator = $this->factory->createInstanceFor('type'); - $validator = $this->factory->createInstanceFor($type === 'integer' ? 'number' : $type); - - foreach ($value as $k => &$v) { - $k_path = $this->incrementPath($path, $k); - $typeValidator->check($v, $schema->items, $k_path, $i); + // First check if its defined in "items" + $this->checkUndefined($v, $schema->items, $path, $k); - $validator->check($v, $schema->items, $k_path, $i); + // Recheck with "additionalItems" if the first test fails + if (count($initErrors) < count($this->getErrors()) && (isset($schema->additionalItems) && $schema->additionalItems !== false)) { + $secondErrors = $this->getErrors(); + $this->checkUndefined($v, $schema->additionalItems, $path, $k); } - unset($v); /* remove dangling reference to prevent any future bugs - * caused by accidentally using $v elsewhere */ - $this->addErrors($typeValidator->getErrors()); - $this->addErrors($validator->getErrors()); - } else { - foreach ($value as $k => &$v) { - $initErrors = $this->getErrors(); - - // First check if its defined in "items" - $this->checkUndefined($v, $schema->items, $path, $k); - - // Recheck with "additionalItems" if the first test fails - if (count($initErrors) < count($this->getErrors()) && (isset($schema->additionalItems) && $schema->additionalItems !== false)) { - $secondErrors = $this->getErrors(); - $this->checkUndefined($v, $schema->additionalItems, $path, $k); - } - // Reset errors if needed - if (isset($secondErrors) && count($secondErrors) < count($this->getErrors())) { - $this->errors = $secondErrors; - } elseif (isset($secondErrors) && count($secondErrors) === count($this->getErrors())) { - $this->errors = $initErrors; - } + // Reset errors if needed + if (isset($secondErrors) && count($secondErrors) < count($this->getErrors())) { + $this->errors = $secondErrors; + } elseif (isset($secondErrors) && count($secondErrors) === count($this->getErrors())) { + $this->errors = $initErrors; } - unset($v); /* remove dangling reference to prevent any future bugs - * caused by accidentally using $v elsewhere */ } + unset($v); /* remove dangling reference to prevent any future bugs + * caused by accidentally using $v elsewhere */ } else { // Defined item type definitions foreach ($value as $k => &$v) { From 836048e0992646bb1748013bc57c5470ab405a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Fri, 13 Apr 2018 12:16:47 +0200 Subject: [PATCH 02/14] Enhancement: Normalize composer.json --- composer.json | 65 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/composer.json b/composer.json index 6e72ab9b..0e195010 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,12 @@ { "name": "justinrainbow/json-schema", + "type": "library", "description": "A library to validate a json schema.", - "keywords": ["json", "schema"], + "keywords": [ + "json", + "schema" + ], "homepage": "https://github.com/justinrainbow/json-schema", - "type": "library", "license": "MIT", "authors": [ { @@ -23,43 +26,51 @@ "email": "seroscho@googlemail.com" } ], - "repositories": [{ - "type": "package", - "package": { - "name": "json-schema/JSON-Schema-Test-Suite", - "version": "1.2.0", - "source": { - "url": "https://github.com/json-schema/JSON-Schema-Test-Suite", - "type": "git", - "reference": "1.2.0" - } - } - }], "require": { "php": ">=5.3.3" }, "require-dev": { - "json-schema/JSON-Schema-Test-Suite": "1.2.0", "friendsofphp/php-cs-fixer": "^2.1", + "json-schema/JSON-Schema-Test-Suite": "1.2.0", "phpunit/phpunit": "^4.8.35" }, - "autoload": { - "psr-4": { "JsonSchema\\": "src/JsonSchema/" } - }, - "autoload-dev": { - "psr-4": { "JsonSchema\\Tests\\": "tests/" } - }, - "bin": ["bin/validate-json"], "extra": { "branch-alias": { "dev-master": "5.0.x-dev" } }, + "autoload": { + "psr-4": { + "JsonSchema\\": "src/JsonSchema/" + } + }, + "autoload-dev": { + "psr-4": { + "JsonSchema\\Tests\\": "tests/" + } + }, + "repositories": [ + { + "type": "package", + "package": { + "name": "json-schema/JSON-Schema-Test-Suite", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/json-schema/JSON-Schema-Test-Suite", + "reference": "1.2.0" + } + } + } + ], + "bin": [ + "bin/validate-json" + ], "scripts": { - "test" : "phpunit", - "testOnly" : "phpunit --colors --filter", - "coverage" : "phpunit --coverage-text", - "style-check" : "php-cs-fixer fix --dry-run --verbose --diff", - "style-fix" : "php-cs-fixer fix --verbose" + "coverage": "phpunit --coverage-text", + "style-check": "php-cs-fixer fix --dry-run --verbose --diff", + "style-fix": "php-cs-fixer fix --verbose", + "test": "phpunit", + "testOnly": "phpunit --colors --filter" } } From 96a7fe5d21e68efce3fdc3ea0d7586e236fd42e5 Mon Sep 17 00:00:00 2001 From: Peter Mead Date: Tue, 28 Nov 2017 16:37:34 +0000 Subject: [PATCH 03/14] Don't use PHP's fmod PHP's fmod has some quirks. The private fmod function wraps it to iron out some of those quirks but ultimately it's better to just not use it. --- src/JsonSchema/Constraints/NumberConstraint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonSchema/Constraints/NumberConstraint.php b/src/JsonSchema/Constraints/NumberConstraint.php index 5a809774..bbadbee8 100644 --- a/src/JsonSchema/Constraints/NumberConstraint.php +++ b/src/JsonSchema/Constraints/NumberConstraint.php @@ -70,7 +70,7 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i = private function fmod($number1, $number2) { $number1 = abs($number1); - $modulus = fmod($number1, $number2); + $modulus = ($number1 - round($number1 / $number2) * $number2); $precision = abs(0.0000000001); $diff = (float) ($modulus - $number2); From 6b2a5cdfe6058b505c3d876484f74a34f77e63d0 Mon Sep 17 00:00:00 2001 From: Peter Mead Date: Wed, 29 Nov 2017 15:58:40 +0000 Subject: [PATCH 04/14] Simplify the private fmod function Now that the private fmod function is not using PHP's fmod we can remove the code which was working around it's quirks. --- src/JsonSchema/Constraints/NumberConstraint.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/JsonSchema/Constraints/NumberConstraint.php b/src/JsonSchema/Constraints/NumberConstraint.php index bbadbee8..d4c31a46 100644 --- a/src/JsonSchema/Constraints/NumberConstraint.php +++ b/src/JsonSchema/Constraints/NumberConstraint.php @@ -69,18 +69,13 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i = private function fmod($number1, $number2) { - $number1 = abs($number1); $modulus = ($number1 - round($number1 / $number2) * $number2); - $precision = abs(0.0000000001); - $diff = (float) ($modulus - $number2); + $precision = 0.0000000001; - if (-$precision < $diff && $diff < $precision) { + if (-$precision < $modulus && $modulus < $precision) { return 0.0; } - $decimals1 = mb_strpos($number1, '.') ? mb_strlen($number1) - mb_strpos($number1, '.') - 1 : 0; - $decimals2 = mb_strpos($number2, '.') ? mb_strlen($number2) - mb_strpos($number2, '.') - 1 : 0; - - return (float) round($modulus, max($decimals1, $decimals2)); + return $modulus; } } From 23d7b337943bed21afb675798a028c1dee74375b Mon Sep 17 00:00:00 2001 From: David Porter Date: Mon, 11 Jun 2018 15:09:57 -0500 Subject: [PATCH 05/14] chore(travis): add hhvm to allow_failures --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index ebf29d24..3b4f5fc1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,6 +23,7 @@ matrix: dist: trusty allow_failures: - php: 'nightly' + - php: hhvm before_install: - if [[ "$WITH_COVERAGE" != "true" && "$TRAVIS_PHP_VERSION" != "hhvm" && "$TRAVIS_PHP_VERSION" != "nightly" && "$TRAVIS_PHP_VERSION" != "7.1" ]]; then phpenv config-rm xdebug.ini; fi From a91222b1436052e2161c51e347a220173c56d66e Mon Sep 17 00:00:00 2001 From: Friedemann Schmuhl Date: Mon, 13 Aug 2018 08:16:50 +0200 Subject: [PATCH 06/14] php-doc fix in bin/validate-json --- bin/validate-json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/validate-json b/bin/validate-json index 421ebcde..aeb818f0 100755 --- a/bin/validate-json +++ b/bin/validate-json @@ -129,7 +129,7 @@ function parseHeaderValue($headerValue) /** * Send a string to the output stream, but only if --quiet is not enabled * - * @param $str A string output + * @param $str string A string output */ function output($str) { global $arOptions; From 5dd32e9f9a29be9b672d6d81207024f43e46cf13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Wed, 17 Oct 2018 09:49:21 +0200 Subject: [PATCH 07/14] Fix: Pin friendsofphp/php-cs-fixer to minor version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0e195010..20e85fc3 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "php": ">=5.3.3" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.1", + "friendsofphp/php-cs-fixer": "~2.2.20", "json-schema/JSON-Schema-Test-Suite": "1.2.0", "phpunit/phpunit": "^4.8.35" }, From 8daf5eecc9c810eed590d9bc5222017a68f0d394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Wed, 17 Oct 2018 09:51:50 +0200 Subject: [PATCH 08/14] Fix: Remove unknown fixers --- .php_cs.dist | 3 --- 1 file changed, 3 deletions(-) diff --git a/.php_cs.dist b/.php_cs.dist index c4e84b4e..0b5e2b2c 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -11,11 +11,9 @@ $config '@PSR2' => true, '@Symfony' => true, // additionally - 'align_multiline_comment' => array('comment_type' => 'phpdocs_like'), 'array_syntax' => array('syntax' => 'long'), 'binary_operator_spaces' => false, 'concat_space' => array('spacing' => 'one'), - 'increment_style' => false, 'no_useless_else' => true, 'no_useless_return' => true, 'ordered_imports' => true, @@ -25,7 +23,6 @@ $config 'pre_increment' => false, 'simplified_null_return' => false, 'trailing_comma_in_multiline_array' => false, - 'yoda_style' => null, )) ->setFinder($finder) ; From d9dc83fbc014d3fa60738497b2e544ebf1a6bf41 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Tue, 6 Nov 2018 11:43:36 +1100 Subject: [PATCH 09/14] feat: Add blacklisting of endpoints with invalid media types - Fixes justinrainbow/json-schema#543 and justinrainbow/json-schema#540 --- src/JsonSchema/Uri/UriRetriever.php | 25 ++++++++++++++++++++++--- tests/Uri/UriRetrieverTest.php | 24 +++++++++++++++++++++++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/JsonSchema/Uri/UriRetriever.php b/src/JsonSchema/Uri/UriRetriever.php index 65452788..085926b8 100644 --- a/src/JsonSchema/Uri/UriRetriever.php +++ b/src/JsonSchema/Uri/UriRetriever.php @@ -32,6 +32,14 @@ class UriRetriever implements BaseUriRetrieverInterface '|^https?://json-schema.org/draft-(0[34])/schema#?|' => 'package://dist/schema/json-schema-draft-$1.json' ); + /** + * @var array A blacklist for media type ceheck exclusion + */ + protected $mediaTypeBlacklist = array( + 'http://json-schema.org/', + 'https://json-schema.org/' + ); + /** * @var null|UriRetrieverInterface */ @@ -44,6 +52,16 @@ class UriRetriever implements BaseUriRetrieverInterface */ private $schemaCache = array(); + /** + * Adds an endpoint to the media type validation blacklist + * + * @param string $endpoint + */ + public function addBlacklistedEndpoint($endpoint) + { + $this->mediaTypeBlacklist[] = $endpoint; + } + /** * Guarantee the correct media type was encountered * @@ -65,9 +83,10 @@ public function confirmMediaType($uriRetriever, $uri) return; } - if (substr($uri, 0, 23) == 'http://json-schema.org/') { - //HACK; they deliver broken content types - return true; + for ($i = 0, $iMax = count($this->mediaTypeBlacklist); $i < $iMax; $i++) { + if (stripos($uri, $this->mediaTypeBlacklist[$i]) === 0) { + return true; + } } throw new InvalidSchemaMediaTypeException(sprintf('Media type %s expected', Validator::SCHEMA_MEDIA_TYPE)); diff --git a/tests/Uri/UriRetrieverTest.php b/tests/Uri/UriRetrieverTest.php index c95fc923..2d0fb250 100644 --- a/tests/Uri/UriRetrieverTest.php +++ b/tests/Uri/UriRetrieverTest.php @@ -330,7 +330,7 @@ public function testRetrieveSchemaFromPackage() $this->assertEquals('454f423bd7edddf0bc77af4130ed9161', md5(json_encode($schema))); } - public function testJsonSchemaOrgMediaTypeHack() + public function testJsonSchemaOrgMediaTypeBlacklistDefault() { $mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); @@ -339,6 +339,28 @@ public function testJsonSchemaOrgMediaTypeHack() $this->assertTrue($retriever->confirmMediaType($mock, 'http://json-schema.org/')); } + /** + * @expectedException \JsonSchema\Exception\InvalidSchemaMediaTypeException + */ + public function testJsonSchemaOrgMediaTypeBlacklistUnknown() + { + $mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); + $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); + $retriever = new UriRetriever(); + + $retriever->confirmMediaType($mock, 'http://iglucentral.com'); + } + + public function testJsonSchemaOrgMediaTypeBlacklistAdded() + { + $mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); + $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); + $retriever = new UriRetriever(); + $retriever->addBlacklistedEndpoint('http://iglucentral.com'); + + $retriever->confirmMediaType($mock, 'http://iglucentral.com'); + } + public function testSchemaCache() { $retriever = new UriRetriever(); From dfbdda3ac62fa4ecc76c18fad2523a1ea9632db7 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Tue, 6 Nov 2018 11:55:46 +1100 Subject: [PATCH 10/14] fix: spelling mistake --- src/JsonSchema/Uri/UriRetriever.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonSchema/Uri/UriRetriever.php b/src/JsonSchema/Uri/UriRetriever.php index 085926b8..12375959 100644 --- a/src/JsonSchema/Uri/UriRetriever.php +++ b/src/JsonSchema/Uri/UriRetriever.php @@ -33,7 +33,7 @@ class UriRetriever implements BaseUriRetrieverInterface ); /** - * @var array A blacklist for media type ceheck exclusion + * @var array A blacklist for media type check exclusion */ protected $mediaTypeBlacklist = array( 'http://json-schema.org/', From 9fac26045d20c3314a15f0a3813bca7a081c107a Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Tue, 6 Nov 2018 12:03:39 +1100 Subject: [PATCH 11/14] fix: use example.com as example domains --- tests/Uri/UriRetrieverTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Uri/UriRetrieverTest.php b/tests/Uri/UriRetrieverTest.php index 2d0fb250..598a97d8 100644 --- a/tests/Uri/UriRetrieverTest.php +++ b/tests/Uri/UriRetrieverTest.php @@ -348,7 +348,7 @@ public function testJsonSchemaOrgMediaTypeBlacklistUnknown() $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); $retriever = new UriRetriever(); - $retriever->confirmMediaType($mock, 'http://iglucentral.com'); + $retriever->confirmMediaType($mock, 'http://example.com'); } public function testJsonSchemaOrgMediaTypeBlacklistAdded() @@ -356,9 +356,9 @@ public function testJsonSchemaOrgMediaTypeBlacklistAdded() $mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); $retriever = new UriRetriever(); - $retriever->addBlacklistedEndpoint('http://iglucentral.com'); + $retriever->addBlacklistedEndpoint('http://example.com'); - $retriever->confirmMediaType($mock, 'http://iglucentral.com'); + $retriever->confirmMediaType($mock, 'http://example.com'); } public function testSchemaCache() From bc14a6f75c3409a49796db395dee0a938d5a93a3 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Tue, 6 Nov 2018 13:35:30 +1100 Subject: [PATCH 12/14] fix: Adjustments based on code review - Change stripos to strpos to match case-insensitive - Rename $mediaTypeBlacklist to $allowedInvalidContentTypeEndpoints - Refactor for to foreach --- src/JsonSchema/Uri/UriRetriever.php | 14 +++++++------- tests/Uri/UriRetrieverTest.php | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/JsonSchema/Uri/UriRetriever.php b/src/JsonSchema/Uri/UriRetriever.php index 12375959..41147a2a 100644 --- a/src/JsonSchema/Uri/UriRetriever.php +++ b/src/JsonSchema/Uri/UriRetriever.php @@ -33,9 +33,9 @@ class UriRetriever implements BaseUriRetrieverInterface ); /** - * @var array A blacklist for media type check exclusion + * @var array A list of endpoints for media type check exclusion */ - protected $mediaTypeBlacklist = array( + protected $allowedInvalidContentTypeEndpoints = array( 'http://json-schema.org/', 'https://json-schema.org/' ); @@ -53,13 +53,13 @@ class UriRetriever implements BaseUriRetrieverInterface private $schemaCache = array(); /** - * Adds an endpoint to the media type validation blacklist + * Adds an endpoint to the media type validation exclusion list * * @param string $endpoint */ - public function addBlacklistedEndpoint($endpoint) + public function addInvalidContentTypeEndpoint($endpoint) { - $this->mediaTypeBlacklist[] = $endpoint; + $this->allowedInvalidContentTypeEndpoints[] = $endpoint; } /** @@ -83,8 +83,8 @@ public function confirmMediaType($uriRetriever, $uri) return; } - for ($i = 0, $iMax = count($this->mediaTypeBlacklist); $i < $iMax; $i++) { - if (stripos($uri, $this->mediaTypeBlacklist[$i]) === 0) { + foreach ($this->allowedInvalidContentTypeEndpoints as $endpoint) { + if (strpos($uri, $endpoint) === 0) { return true; } } diff --git a/tests/Uri/UriRetrieverTest.php b/tests/Uri/UriRetrieverTest.php index 598a97d8..f6ee67b7 100644 --- a/tests/Uri/UriRetrieverTest.php +++ b/tests/Uri/UriRetrieverTest.php @@ -356,7 +356,7 @@ public function testJsonSchemaOrgMediaTypeBlacklistAdded() $mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); $retriever = new UriRetriever(); - $retriever->addBlacklistedEndpoint('http://example.com'); + $retriever->addInvalidContentTypeEndpoint('http://example.com'); $retriever->confirmMediaType($mock, 'http://example.com'); } From 1da6614ebb0ce8f6c96eb7c3f9c3c18ab06e4fc2 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Tue, 6 Nov 2018 13:53:17 +1100 Subject: [PATCH 13/14] fix: Cleanup test names --- tests/Uri/UriRetrieverTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Uri/UriRetrieverTest.php b/tests/Uri/UriRetrieverTest.php index f6ee67b7..fb05cc18 100644 --- a/tests/Uri/UriRetrieverTest.php +++ b/tests/Uri/UriRetrieverTest.php @@ -330,19 +330,20 @@ public function testRetrieveSchemaFromPackage() $this->assertEquals('454f423bd7edddf0bc77af4130ed9161', md5(json_encode($schema))); } - public function testJsonSchemaOrgMediaTypeBlacklistDefault() + public function testInvalidContentTypeEndpointsDefault() { $mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); $retriever = new UriRetriever(); $this->assertTrue($retriever->confirmMediaType($mock, 'http://json-schema.org/')); + $this->assertTrue($retriever->confirmMediaType($mock, 'https://json-schema.org/')); } /** * @expectedException \JsonSchema\Exception\InvalidSchemaMediaTypeException */ - public function testJsonSchemaOrgMediaTypeBlacklistUnknown() + public function testInvalidContentTypeEndpointsUnknown() { $mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); @@ -351,7 +352,7 @@ public function testJsonSchemaOrgMediaTypeBlacklistUnknown() $retriever->confirmMediaType($mock, 'http://example.com'); } - public function testJsonSchemaOrgMediaTypeBlacklistAdded() + public function testInvalidContentTypeEndpointsAdded() { $mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType')); $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); From 86dfb042c660b5ebc2ebad16472e921f151c6a29 Mon Sep 17 00:00:00 2001 From: Pavel Kacha Date: Wed, 14 Nov 2018 13:48:13 +0100 Subject: [PATCH 14/14] Fix min/maxProperties check for invalid array input --- .../Constraints/TypeCheck/StrictTypeCheck.php | 4 ++++ tests/Constraints/MinMaxPropertiesTest.php | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/JsonSchema/Constraints/TypeCheck/StrictTypeCheck.php b/src/JsonSchema/Constraints/TypeCheck/StrictTypeCheck.php index a6303a7a..59fa12be 100644 --- a/src/JsonSchema/Constraints/TypeCheck/StrictTypeCheck.php +++ b/src/JsonSchema/Constraints/TypeCheck/StrictTypeCheck.php @@ -31,6 +31,10 @@ public static function propertyExists($value, $property) public static function propertyCount($value) { + if (!is_object($value)) { + return 0; + } + return count(get_object_vars($value)); } } diff --git a/tests/Constraints/MinMaxPropertiesTest.php b/tests/Constraints/MinMaxPropertiesTest.php index 2063122c..90774b91 100644 --- a/tests/Constraints/MinMaxPropertiesTest.php +++ b/tests/Constraints/MinMaxPropertiesTest.php @@ -123,6 +123,16 @@ public function getInvalidTests() } }' ), + array( + '{ + "value": [] + }', + '{ + "properties": { + "value": {"minProperties": 1,"maxProperties": 2} + } + }' + ), ); } }