Skip to content

Wrong absolute_path with anyOf nested in allOf #312

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

Closed
estan opened this issue Dec 6, 2016 · 3 comments
Closed

Wrong absolute_path with anyOf nested in allOf #312

estan opened this issue Dec 6, 2016 · 3 comments

Comments

@estan
Copy link

estan commented Dec 6, 2016

I was working on a schema and noticed that the absolute_path in the ValidationException was not quite right when working with anyOf nested inside allOf.

Here's a small test case:

test.json

{
    "foo": {
        "baz": "wrong"
    }
}

schema1.json

{
    "$schema": "http://json-schema.org/draft-04/schema",

    "type": "object",
    "properties": {
        "foo": {
            "allOf": [
                {
                    "type": "object",
                    "properties": {
                        "bar": { "type": "integer" }
                    }
                },
                {
                    "type": "object",
                    "properties": {
                        "baz": { "type": "integer" }
                    }
                }
            ]
        }
    }
}

schema2.json

{
    "$schema": "http://json-schema.org/draft-04/schema",

    "type": "object",
    "properties": {
        "foo": {
            "allOf": [
                {
                    "type": "object",
                    "properties": {
                        "bar": { "type": "integer" }
                    }
                },
                {
                    "anyOf": [
                        {
                            "type": "object",
                            "properties": {
                                "baz": { "type": "integer" }
                            }
                        }
                    ]
                }
            ]
        }
    }
}

And finally:

test.py

from jsonschema import validate, ValidationError
from json import load

instance = load(open('test.json'))

schema1 = load(open('schema1.json'))
schema2 = load(open('schema2.json'))

for schema in [schema1, schema2]:
    try:
        validate(instance, schema)
    except ValidationError as e:
        print(e.absolute_path)

Output from python3 test.py

deque(['foo', 'baz'])
deque(['foo'])

Expected output

deque(['foo', 'baz'])
deque(['foo', 'baz'])

In both cases, the offending element in the instance is the same, so I would expect the absolute_path to be the same.

Would be great to have this fixed, as I'm using an allOf { anyOf { ... } } construct at quite a high level in my schema, and the absolute_path really helps when presenting an error to the user. As it is, the path is collapsed like this, making the error message not very helpful :/

@estan
Copy link
Author

estan commented Dec 6, 2016

I found this problem with the 2.5.1 Ubuntu package, but I've tested with Git master and it's still there.

@estan
Copy link
Author

estan commented Dec 7, 2016

Sorry, I should have read the error handling docs more carefully. I realize now that this is actually expected behavior, since the default heuristic considers anyOf and oneOf weak validation errors. If I instead use the jsonschema.exceptions.best_match helper on iter_errors(), it'll give me what I want. Very nice with all this flexibility!

Closing this now. Sorry for the noise.

@estan estan closed this as completed Dec 7, 2016
@Julian
Copy link
Member

Julian commented Dec 7, 2016

Glad you figured it out thanks for following up.

Julian added a commit that referenced this issue Nov 29, 2019
0f344a69 Merge pull request #313 from leadpony/issue309
46c44747 Replace the control escape \\a with \\t
1ffe03e5 Merge pull request #312 from gregsdennis/master
de004798 better descripttions
eea7f249 arrays have characters too
7c02d06d added unevaluatedProperties test file; resolves #310
1899a5aa Merge pull request #308 from aznan2/master
4a5010b3 Update the version list.
37569b13 issue #307 - made test compatible with draft4
e3087307 issue #307 - removed issue reference from description
e13d3275 issue #307 - removed pound sign from description
a3b9f723 issue #307 - test that oneOf handles missing optional property

git-subtree-dir: json
git-subtree-split: 0f344a698f6657441adf4ebf4ceeacd596683422
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

2 participants