Skip to content

Commit dcb6e10

Browse files
authored
Merge pull request #558 from erayd/backport-528
Backports for 5.2.8
2 parents 8560d43 + 86dfb04 commit dcb6e10

File tree

10 files changed

+119
-85
lines changed

10 files changed

+119
-85
lines changed

.php_cs.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ $config
1111
'@PSR2' => true,
1212
'@Symfony' => true,
1313
// additionally
14-
'align_multiline_comment' => array('comment_type' => 'phpdocs_like'),
1514
'array_syntax' => array('syntax' => 'long'),
1615
'binary_operator_spaces' => false,
1716
'concat_space' => array('spacing' => 'one'),
18-
'increment_style' => false,
1917
'no_useless_else' => true,
2018
'no_useless_return' => true,
2119
'ordered_imports' => true,
@@ -25,7 +23,6 @@ $config
2523
'pre_increment' => false,
2624
'simplified_null_return' => false,
2725
'trailing_comma_in_multiline_array' => false,
28-
'yoda_style' => null,
2926
))
3027
->setFinder($finder)
3128
;

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ matrix:
2323
dist: trusty
2424
allow_failures:
2525
- php: 'nightly'
26+
- php: hhvm
2627

2728
before_install:
2829
- if [[ "$WITH_COVERAGE" != "true" && "$TRAVIS_PHP_VERSION" != "hhvm" && "$TRAVIS_PHP_VERSION" != "nightly" && "$TRAVIS_PHP_VERSION" != "7.1" ]]; then phpenv config-rm xdebug.ini; fi

bin/validate-json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function parseHeaderValue($headerValue)
129129
/**
130130
* Send a string to the output stream, but only if --quiet is not enabled
131131
*
132-
* @param $str A string output
132+
* @param $str string A string output
133133
*/
134134
function output($str) {
135135
global $arOptions;

composer.json

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
{
22
"name": "justinrainbow/json-schema",
3+
"type": "library",
34
"description": "A library to validate a json schema.",
4-
"keywords": ["json", "schema"],
5+
"keywords": [
6+
"json",
7+
"schema"
8+
],
59
"homepage": "https://github.com/justinrainbow/json-schema",
6-
"type": "library",
710
"license": "MIT",
811
"authors": [
912
{
@@ -23,43 +26,51 @@
2326
"email": "[email protected]"
2427
}
2528
],
26-
"repositories": [{
27-
"type": "package",
28-
"package": {
29-
"name": "json-schema/JSON-Schema-Test-Suite",
30-
"version": "1.2.0",
31-
"source": {
32-
"url": "https://github.com/json-schema/JSON-Schema-Test-Suite",
33-
"type": "git",
34-
"reference": "1.2.0"
35-
}
36-
}
37-
}],
3829
"require": {
3930
"php": ">=5.3.3"
4031
},
4132
"require-dev": {
33+
"friendsofphp/php-cs-fixer": "~2.2.20",
4234
"json-schema/JSON-Schema-Test-Suite": "1.2.0",
43-
"friendsofphp/php-cs-fixer": "^2.1",
4435
"phpunit/phpunit": "^4.8.35"
4536
},
46-
"autoload": {
47-
"psr-4": { "JsonSchema\\": "src/JsonSchema/" }
48-
},
49-
"autoload-dev": {
50-
"psr-4": { "JsonSchema\\Tests\\": "tests/" }
51-
},
52-
"bin": ["bin/validate-json"],
5337
"extra": {
5438
"branch-alias": {
5539
"dev-master": "5.0.x-dev"
5640
}
5741
},
42+
"autoload": {
43+
"psr-4": {
44+
"JsonSchema\\": "src/JsonSchema/"
45+
}
46+
},
47+
"autoload-dev": {
48+
"psr-4": {
49+
"JsonSchema\\Tests\\": "tests/"
50+
}
51+
},
52+
"repositories": [
53+
{
54+
"type": "package",
55+
"package": {
56+
"name": "json-schema/JSON-Schema-Test-Suite",
57+
"version": "1.2.0",
58+
"source": {
59+
"type": "git",
60+
"url": "https://github.com/json-schema/JSON-Schema-Test-Suite",
61+
"reference": "1.2.0"
62+
}
63+
}
64+
}
65+
],
66+
"bin": [
67+
"bin/validate-json"
68+
],
5869
"scripts": {
59-
"test" : "phpunit",
60-
"testOnly" : "phpunit --colors --filter",
61-
"coverage" : "phpunit --coverage-text",
62-
"style-check" : "php-cs-fixer fix --dry-run --verbose --diff",
63-
"style-fix" : "php-cs-fixer fix --verbose"
70+
"coverage": "phpunit --coverage-text",
71+
"style-check": "php-cs-fixer fix --dry-run --verbose --diff",
72+
"style-fix": "php-cs-fixer fix --verbose",
73+
"test": "phpunit",
74+
"testOnly": "phpunit --colors --filter"
6475
}
6576
}

src/JsonSchema/Constraints/CollectionConstraint.php

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -65,53 +65,27 @@ protected function validateItems(&$value, $schema = null, JsonPointer $path = nu
6565
{
6666
if (is_object($schema->items)) {
6767
// just one type definition for the whole array
68+
foreach ($value as $k => &$v) {
69+
$initErrors = $this->getErrors();
6870

69-
if (isset($schema->items->type)
70-
&& (
71-
$schema->items->type == 'string'
72-
|| $schema->items->type == 'number'
73-
|| $schema->items->type == 'integer'
74-
)
75-
&& !isset($schema->additionalItems)
76-
) {
77-
// performance optimization
78-
$type = $schema->items->type;
79-
$typeValidator = $this->factory->createInstanceFor('type');
80-
$validator = $this->factory->createInstanceFor($type === 'integer' ? 'number' : $type);
81-
82-
foreach ($value as $k => &$v) {
83-
$k_path = $this->incrementPath($path, $k);
84-
$typeValidator->check($v, $schema->items, $k_path, $i);
71+
// First check if its defined in "items"
72+
$this->checkUndefined($v, $schema->items, $path, $k);
8573

86-
$validator->check($v, $schema->items, $k_path, $i);
74+
// Recheck with "additionalItems" if the first test fails
75+
if (count($initErrors) < count($this->getErrors()) && (isset($schema->additionalItems) && $schema->additionalItems !== false)) {
76+
$secondErrors = $this->getErrors();
77+
$this->checkUndefined($v, $schema->additionalItems, $path, $k);
8778
}
88-
unset($v); /* remove dangling reference to prevent any future bugs
89-
* caused by accidentally using $v elsewhere */
90-
$this->addErrors($typeValidator->getErrors());
91-
$this->addErrors($validator->getErrors());
92-
} else {
93-
foreach ($value as $k => &$v) {
94-
$initErrors = $this->getErrors();
95-
96-
// First check if its defined in "items"
97-
$this->checkUndefined($v, $schema->items, $path, $k);
98-
99-
// Recheck with "additionalItems" if the first test fails
100-
if (count($initErrors) < count($this->getErrors()) && (isset($schema->additionalItems) && $schema->additionalItems !== false)) {
101-
$secondErrors = $this->getErrors();
102-
$this->checkUndefined($v, $schema->additionalItems, $path, $k);
103-
}
10479

105-
// Reset errors if needed
106-
if (isset($secondErrors) && count($secondErrors) < count($this->getErrors())) {
107-
$this->errors = $secondErrors;
108-
} elseif (isset($secondErrors) && count($secondErrors) === count($this->getErrors())) {
109-
$this->errors = $initErrors;
110-
}
80+
// Reset errors if needed
81+
if (isset($secondErrors) && count($secondErrors) < count($this->getErrors())) {
82+
$this->errors = $secondErrors;
83+
} elseif (isset($secondErrors) && count($secondErrors) === count($this->getErrors())) {
84+
$this->errors = $initErrors;
11185
}
112-
unset($v); /* remove dangling reference to prevent any future bugs
113-
* caused by accidentally using $v elsewhere */
11486
}
87+
unset($v); /* remove dangling reference to prevent any future bugs
88+
* caused by accidentally using $v elsewhere */
11589
} else {
11690
// Defined item type definitions
11791
foreach ($value as $k => &$v) {

src/JsonSchema/Constraints/NumberConstraint.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,13 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
6969

7070
private function fmod($number1, $number2)
7171
{
72-
$number1 = abs($number1);
73-
$modulus = fmod($number1, $number2);
74-
$precision = abs(0.0000000001);
75-
$diff = (float) ($modulus - $number2);
72+
$modulus = ($number1 - round($number1 / $number2) * $number2);
73+
$precision = 0.0000000001;
7674

77-
if (-$precision < $diff && $diff < $precision) {
75+
if (-$precision < $modulus && $modulus < $precision) {
7876
return 0.0;
7977
}
8078

81-
$decimals1 = mb_strpos($number1, '.') ? mb_strlen($number1) - mb_strpos($number1, '.') - 1 : 0;
82-
$decimals2 = mb_strpos($number2, '.') ? mb_strlen($number2) - mb_strpos($number2, '.') - 1 : 0;
83-
84-
return (float) round($modulus, max($decimals1, $decimals2));
79+
return $modulus;
8580
}
8681
}

src/JsonSchema/Constraints/TypeCheck/StrictTypeCheck.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public static function propertyExists($value, $property)
3131

3232
public static function propertyCount($value)
3333
{
34+
if (!is_object($value)) {
35+
return 0;
36+
}
37+
3438
return count(get_object_vars($value));
3539
}
3640
}

src/JsonSchema/Uri/UriRetriever.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ class UriRetriever implements BaseUriRetrieverInterface
3232
'|^https?://json-schema.org/draft-(0[34])/schema#?|' => 'package://dist/schema/json-schema-draft-$1.json'
3333
);
3434

35+
/**
36+
* @var array A list of endpoints for media type check exclusion
37+
*/
38+
protected $allowedInvalidContentTypeEndpoints = array(
39+
'http://json-schema.org/',
40+
'https://json-schema.org/'
41+
);
42+
3543
/**
3644
* @var null|UriRetrieverInterface
3745
*/
@@ -44,6 +52,16 @@ class UriRetriever implements BaseUriRetrieverInterface
4452
*/
4553
private $schemaCache = array();
4654

55+
/**
56+
* Adds an endpoint to the media type validation exclusion list
57+
*
58+
* @param string $endpoint
59+
*/
60+
public function addInvalidContentTypeEndpoint($endpoint)
61+
{
62+
$this->allowedInvalidContentTypeEndpoints[] = $endpoint;
63+
}
64+
4765
/**
4866
* Guarantee the correct media type was encountered
4967
*
@@ -65,9 +83,10 @@ public function confirmMediaType($uriRetriever, $uri)
6583
return;
6684
}
6785

68-
if (substr($uri, 0, 23) == 'http://json-schema.org/') {
69-
//HACK; they deliver broken content types
70-
return true;
86+
foreach ($this->allowedInvalidContentTypeEndpoints as $endpoint) {
87+
if (strpos($uri, $endpoint) === 0) {
88+
return true;
89+
}
7190
}
7291

7392
throw new InvalidSchemaMediaTypeException(sprintf('Media type %s expected', Validator::SCHEMA_MEDIA_TYPE));

tests/Constraints/MinMaxPropertiesTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ public function getInvalidTests()
123123
}
124124
}'
125125
),
126+
array(
127+
'{
128+
"value": []
129+
}',
130+
'{
131+
"properties": {
132+
"value": {"minProperties": 1,"maxProperties": 2}
133+
}
134+
}'
135+
),
126136
);
127137
}
128138
}

tests/Uri/UriRetrieverTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,36 @@ public function testRetrieveSchemaFromPackage()
330330
$this->assertEquals('454f423bd7edddf0bc77af4130ed9161', md5(json_encode($schema)));
331331
}
332332

333-
public function testJsonSchemaOrgMediaTypeHack()
333+
public function testInvalidContentTypeEndpointsDefault()
334334
{
335335
$mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType'));
336336
$mock->method('getContentType')->willReturn('Application/X-Fake-Type');
337337
$retriever = new UriRetriever();
338338

339339
$this->assertTrue($retriever->confirmMediaType($mock, 'http://json-schema.org/'));
340+
$this->assertTrue($retriever->confirmMediaType($mock, 'https://json-schema.org/'));
341+
}
342+
343+
/**
344+
* @expectedException \JsonSchema\Exception\InvalidSchemaMediaTypeException
345+
*/
346+
public function testInvalidContentTypeEndpointsUnknown()
347+
{
348+
$mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType'));
349+
$mock->method('getContentType')->willReturn('Application/X-Fake-Type');
350+
$retriever = new UriRetriever();
351+
352+
$retriever->confirmMediaType($mock, 'http://example.com');
353+
}
354+
355+
public function testInvalidContentTypeEndpointsAdded()
356+
{
357+
$mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType'));
358+
$mock->method('getContentType')->willReturn('Application/X-Fake-Type');
359+
$retriever = new UriRetriever();
360+
$retriever->addInvalidContentTypeEndpoint('http://example.com');
361+
362+
$retriever->confirmMediaType($mock, 'http://example.com');
340363
}
341364

342365
public function testSchemaCache()

0 commit comments

Comments
 (0)