Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anyOf Warning #441

Closed
allentsai opened this issue Jul 14, 2017 · 5 comments
Closed

anyOf Warning #441

allentsai opened this issue Jul 14, 2017 · 5 comments

Comments

@allentsai
Copy link

Hello, I would first like to thank you all for the hard work you've put into this project, and add the disclaimer that this could very well be user error, but my schema validates with other validators, and previous versions, but I get a warning with v5.2.1.

Stripped down to what is causing the error:
Base schema:

{
	"$schema": "http://json-schema.org/draft-04/schema#",
	"type": "object",
	"properties": {
		"location": {
			"anyOf": [
				{
					"$ref": "./postalList.json"
				},
				{
					"$ref": "./area.json"
				}
			]
		}
	},
	"additionalProperties": false,
	"required": [
		"location"
	]
}

postalList.json:

{
	"$schema": "http://json-schema.org/draft-04/schema#",
	"type": "object",
	"properties": {
		"countryLimit": {
			"type": "string"
		},
		"type": {
			"enum": ["postalList"]
		},
		"data": {
			"type": "array",
			"items": {
				"type": "string",
				"minLength": 2
			},
			"minItems": 1,
			"uniqueItems": true
		}
	},
	"additionalItems": false
}

area.json:

{
	"$schema": "http://json-schema.org/draft-04/schema#",
	"type": "object",
	"properties": {
		"type": {
			"enum": ["area"]
		},
		"data": {
			"type": "array",
			"items": {
				"$ref": "./areas.json"
			},
			"minItems":1,
			"uniqueItems": true
		}
	},
	"additionalProperties": false
}

areas.json:

{
	"$schema": "http://json-schema.org/draft-04/schema#",
	"type": "object",
	"properties": {
		"id": {
			"type": "number",
			"minimum": 0
		},
		"name": {
			"type": "string",
			"minLength": 1
		},
		"abbr": {
			"type": "string",
			"pattern": "[A-Z]+",
			"minLength": 1
		}
	},
	"required": ["id", "name", "abbr"]
}

Test Code:

$validator = new \JsonSchema\Validator();
$value = json_encode(
    ["location" => ["type" => "area", "data" => [["id" => 1, "name" => "area name", "abbr" => "AB"]]]]
);

$result = $validator->validate(json_decode($value), (object)['$ref' => 'file://' . realpath('./test.json')]);

This gives me the follow error repeated twice:

PHP Warning:  mb_detect_encoding() expects parameter 1 to be string, object given in /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/StringConstraint.php on line 54$
PHP Stack trace:$
PHP   1. {main}() /usr/local/src/placelocal/public_html/test.php:0$
PHP   2. JsonSchema\\Validator->validate() /usr/local/src/placelocal/public_html/test.php:105$
PHP   3. JsonSchema\\Constraints\\SchemaConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Validator.php:63$
PHP   4. JsonSchema\\Constraints\\Constraint->checkUndefined() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/SchemaConstraint.php:93$
PHP   5. JsonSchema\\Constraints\\UndefinedConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:121$
PHP   6. JsonSchema\\Constraints\\UndefinedConstraint->validateTypes() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:51$
PHP   7. JsonSchema\\Constraints\\Constraint->checkObject() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:81$
PHP   8. JsonSchema\\Constraints\\ObjectConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:88$
PHP   9. JsonSchema\\Constraints\\ObjectConstraint->validateProperties() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/ObjectConstraint.php:47$
PHP  10. JsonSchema\\Constraints\\Constraint->checkUndefined() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/ObjectConstraint.php:145$
PHP  11. JsonSchema\\Constraints\\UndefinedConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:121$
PHP  12. JsonSchema\\Constraints\\UndefinedConstraint->validateOfProperties() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:48$
PHP  13. JsonSchema\\Constraints\\Constraint->checkUndefined() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:319$
PHP  14. JsonSchema\\Constraints\\UndefinedConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:121$
PHP  15. JsonSchema\\Constraints\\UndefinedConstraint->validateTypes() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:51$
PHP  16. JsonSchema\\Constraints\\Constraint->checkObject() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:81$
PHP  17. JsonSchema\\Constraints\\ObjectConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:88$
PHP  18. JsonSchema\\Constraints\\ObjectConstraint->validateProperties() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/ObjectConstraint.php:47$
PHP  19. JsonSchema\\Constraints\\Constraint->checkUndefined() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/ObjectConstraint.php:145$
PHP  20. JsonSchema\\Constraints\\UndefinedConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:121$
PHP  21. JsonSchema\\Constraints\\UndefinedConstraint->validateTypes() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:51$
PHP  22. JsonSchema\\Constraints\\Constraint->checkArray() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:66$
PHP  23. JsonSchema\\Constraints\\CollectionConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:69$
PHP  24. JsonSchema\\Constraints\\CollectionConstraint->validateItems() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/CollectionConstraint.php:52$
PHP  25. JsonSchema\\Constraints\\StringConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/CollectionConstraint.php:86$
PHP  26. JsonSchema\\Constraints\\StringConstraint->strlen() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/StringConstraint.php:35$
PHP  27. mb_detect_encoding() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/StringConstraint.php:54$

Seems like anyOf behavior is being too greedy when evaluating the included schemas. Note that if you swap the order of postalList.json and area.json, the warning no longer appears.

@allentsai allentsai changed the title anyOf Exception anyOf Warning Jul 14, 2017
@erayd
Copy link
Contributor

erayd commented Jul 23, 2017

Thanks for reporting this. I saw it the day you posted it, and just wanted to let you know that I haven't lost sight of it - I've just been insanely busy with other things (i.e. work that pays the bills), and haven't yet had time to look into the problem in any detail.

I will investigate and resolve this, but it might take me another couple of weeks to get to sorry. If someone else feels like sorting it out before then, feel free - pull requests against 6.0.0-dev are welcome :-).

@allentsai
Copy link
Author

Definitely understandable, people like you who make time to contribute to projects in addition to work are admirable! Thank you for looking at this.

@eldering
Copy link

eldering commented Mar 3, 2018

I encountered, what I think is, the same error. A simpler example:
The schema is

{
	"$schema": "http://json-schema.org/draft-04/schema#",
	"title": "Test validation of additionalProperties defined inside oneOf",

	"type": "object",
	"properties": {
		"op": { "enum": [ "a", "b" ] },
		"oneOf": [
			{
				"op": { "enum": [ "a" ] },
				"data": {}
			},
			{
				"op": { "enum": [ "b" ] },
				"data": {}
			}
		]
	},
	"additionalProperties": false
}

and the following JSON data

{"op":"a","data":{}}

fails to validate on this with error [] The property data is not defined and the definition does not allow additional properties.
This is with validate-json from version 5.2.6.

@erayd
Copy link
Contributor

erayd commented Mar 3, 2018

@eldering Your example has nothing to do with the bug reported in this issue, and is actually user error - your schema does doesn't mean what you think it means. Your schema does not define the property data at all. It defines the properties op and oneOf, and then tries to use data and op as keywords, and as those aren't valid keywords, they are ignored.

The error message you're receiving is correct, as intended, and matches how the spec says this should work.

@allentsai - what you have reported is definitely a bug; I'd just lost sight of this one sorry. I will fix it.

erayd added a commit to erayd/json-schema that referenced this issue Mar 4, 2018
Fixes jsonrainbow#441.

The bug in jsonrainbow#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.
@erayd
Copy link
Contributor

erayd commented Mar 4, 2018

@allentsai This is fixed in #498. I will backport this change for v5.2.8 once the PR is merged.

erayd added a commit to erayd/json-schema that referenced this issue Jan 10, 2019
Fixes jsonrainbow#441.

The bug in jsonrainbow#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants