python-jsonschema / jsonschema

An implementation of the JSON Schema specification for Python
https://python-jsonschema.readthedocs.io
MIT License
4.63k stars 582 forks source link

oneOf not fails if more than one child schema matches #792

Closed schaumb closed 3 years ago

schaumb commented 3 years ago

The issue comes from: https://github.com/jsongraph/json-graph-specification/issues/53

Schema: https://github.com/jsongraph/json-graph-specification/blob/ce5ed4007c61df23dd0835094a7b69364ab445d9/json-graph-schema_v2.json Test:

{"graph": {}}
Julian commented 3 years ago

Hi -- not sure I can parse the title here -- are you saying it does fail or doesn't fail? oneOf fails only when exactly one subschema matches. Is that not the behavior you're observing?

schaumb commented 3 years ago

Hi! It doesn't fail when more than one subschema matches.

Julian commented 3 years ago

Got it. Can you try to minimize your example? Which two schemas do you think are both matching?

Julian commented 3 years ago

I don't see the behavior you're mentioning, the instance fails here:

⊙  cat foo.py                                                                                                                                                                                                    julian@Airm
from jsonschema import validate
import requests
schema = requests.get("https://raw.githubusercontent.com/jsongraph/json-graph-specification/ce5ed4007c61df23dd0835094a7b69364ab445d9/json-graph-schema_v2.json").json()
instance = {"graph" : {}}
validate(instance=instance, schema=schema)

⊙  ~[jsonschema:python] foo.py                                                                                                                                                                                   julian@Airm
Traceback (most recent call last):
  File "/Users/julian/Desktop/foo.py", line 5, in <module>
    validate(instance=instance, schema=schema)
  File "/Users/julian/.dotfiles/.local/share/virtualenvs/jsonschema/lib/python3.9/site-packages/jsonschema/validators.py", line 934, in validate
    raise error
jsonschema.exceptions.ValidationError: {} is valid under each of {'type': 'object', 'additionalProperties': False, 'properties': {'id': {'type': 'string'}, 'label': {'type': 'string'}, 'directed': {'type': ['boolean'], 'default': True}, 'type': {'type': 'string'}, 'metadata': {'type': ['object']}, 'nodes': {'type': 'object', 'additionalProperties': {'$ref': '#/definitions/node'}}, 'hyperedges': {'type': ['array'], 'items': {'$ref': '#/definitions/directedhyperedge'}}}}, {'type': 'object', 'additionalProperties': False, 'properties': {'id': {'type': 'string'}, 'label': {'type': 'string'}, 'directed': {'type': ['boolean'], 'default': True}, 'type': {'type': 'string'}, 'metadata': {'type': ['object']}, 'nodes': {'type': 'object', 'additionalProperties': {'$ref': '#/definitions/node'}}, 'edges': {'type': ['array'], 'items': {'$ref': '#/definitions/edge'}}}}

Failed validating 'oneOf' in schema[0]['properties']['graph']:
    {'oneOf': [{'additionalProperties': False,
                'properties': {'directed': {'default': True,
                                            'type': ['boolean']},
                               'edges': {'items': {'$ref': '#/definitions/edge'},
                                         'type': ['array']},
                               'id': {'type': 'string'},
                               'label': {'type': 'string'},
                               'metadata': {'type': ['object']},
                               'nodes': {'additionalProperties': {'$ref': '#/definitions/node'},
                                         'type': 'object'},
                               'type': {'type': 'string'}},
                'type': 'object'},
               {'additionalProperties': False,
                'properties': {'directed': {'default': True,
                                            'type': ['boolean']},
                               'hyperedges': {'items': {'$ref': '#/definitions/directedhyperedge'},
                                              'type': ['array']},
                               'id': {'type': 'string'},
                               'label': {'type': 'string'},
                               'metadata': {'type': ['object']},
                               'nodes': {'additionalProperties': {'$ref': '#/definitions/node'},
                                         'type': 'object'},
                               'type': {'type': 'string'}},
                'type': 'object'},
               {'additionalProperties': False,
                'properties': {'directed': {'enum': [False],
                                            'type': ['boolean']},
                               'hyperedges': {'items': {'$ref': '#/definitions/undirectedhyperedge'},
                                              'type': ['array']},
                               'id': {'type': 'string'},
                               'label': {'type': 'string'},
                               'metadata': {'type': ['object']},
                               'nodes': {'additionalProperties': {'$ref': '#/definitions/node'},
                                         'type': 'object'},
                               'type': {'type': 'string'}},
                'required': ['directed'],
                'type': 'object'}]}

On instance['graph']:
    {}
schaumb commented 3 years ago

Oh I can see. Then no issue here :)