Skip to content

fix nested oneOf resolution with different schema types (fixes #232)#233

Merged
cwacek merged 4 commits intocwacek:masterfrom
fmigneault:fix-nested-oneOf
May 15, 2023
Merged

fix nested oneOf resolution with different schema types (fixes #232)#233
cwacek merged 4 commits intocwacek:masterfrom
fmigneault:fix-nested-oneOf

Conversation

@fmigneault
Copy link
Copy Markdown
Contributor

@fmigneault fmigneault commented Jan 16, 2023

Allows a nested oneOf to refer to other schema types than object by considering the keyword expansion when needed.

Fixes

@fmigneault
Copy link
Copy Markdown
Contributor Author

@cwacek Is it possible to consider this PR? If something should be adjusted, let me know.

@felixonmars
Copy link
Copy Markdown

Thanks for the PR. I am cherry-picking this into Arch Linux packaging to get around the following test failure:

=================================== FAILURES ===================================
________________________________ test_validates ________________________________

nested_arrays = {'properties': {'foo': {'items': {'items': [{'type': 'number'}, {'type': 'number'}], 'type': 'array'}, 'type': 'array'}}, 'title': 'example'}
instance = {'foo': [[42, 44]]}

    def test_validates(nested_arrays, instance):
>       validate(instance, nested_arrays)

test/test_nested_arrays.py:31:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/lib/python3.10/site-packages/jsonschema/validators.py:1117: in validate
    cls.check_schema(schema)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'jsonschema.validators.Draft202012Validator'>
schema = {'properties': {'foo': {'items': {'items': [{'type': 'number'}, {'type': 'number'}], 'type': 'array'}, 'type': 'array'}}, 'title': 'example'}
format_checker = <FormatChecker checkers=['date', 'email', 'idn-email', 'ipv4', 'ipv6', 'regex', 'uuid']>

    @classmethod
    def check_schema(cls, schema, format_checker=_UNSET):
        Validator = validator_for(cls.META_SCHEMA, default=cls)
        if format_checker is _UNSET:
            format_checker = Validator.FORMAT_CHECKER
        validator = Validator(
            schema=cls.META_SCHEMA,
            format_checker=format_checker,
        )
        for error in validator.iter_errors(schema):
>           raise exceptions.SchemaError.create_from(error)
E           jsonschema.exceptions.SchemaError: [{'type': 'number'}, {'type': 'number'}] is not of type 'object', 'boolean'
E
E           Failed validating 'type' in metaschema['allOf'][1]['properties']['properties']['additionalProperties']['$dynamicRef']['allOf'][1]['properties']['items']['$dynamicRef']['allOf'][1]['properties']['items']['$dynamicRef']['allOf'][0]:
E               {'$defs': {'anchorString': {'pattern': '^[A-Za-z_][-A-Za-z0-9._]*$',
E                                           'type': 'string'},
E                          'uriReferenceString': {'format': 'uri-reference',
E                                                 'type': 'string'},
E                          'uriString': {'format': 'uri', 'type': 'string'}},
E                '$dynamicAnchor': 'meta',
E                '$id': 'https://json-schema.org/draft/2020-12/meta/core',
E                '$schema': 'https://json-schema.org/draft/2020-12/schema',
E                '$vocabulary': {'https://json-schema.org/draft/2020-12/vocab/core': True},
E                'properties': {'$anchor': {'$ref': '#/$defs/anchorString'},
E                               '$comment': {'type': 'string'},
E                               '$defs': {'additionalProperties': {'$dynamicRef': '#meta'},
E                                         'type': 'object'},
E                               '$dynamicAnchor': {'$ref': '#/$defs/anchorString'},
E                               '$dynamicRef': {'$ref': '#/$defs/uriReferenceString'},
E                               '$id': {'$comment': 'Non-empty fragments not allowed.',
E                                       '$ref': '#/$defs/uriReferenceString',
E                                       'pattern': '^[^#]*#?$'},
E                               '$ref': {'$ref': '#/$defs/uriReferenceString'},
E                               '$schema': {'$ref': '#/$defs/uriString'},
E                               '$vocabulary': {'additionalProperties': {'type': 'boolean'},
E                                               'propertyNames': {'$ref': '#/$defs/uriString'},
E                                               'type': 'object'}},
E                'title': 'Core vocabulary meta-schema',
E                'type': ['object', 'boolean']}
E
E           On schema['properties']['foo']['items']['items']:
E               [{'type': 'number'}, {'type': 'number'}]

/usr/lib/python3.10/site-packages/jsonschema/validators.py:231: SchemaError

@cwacek cwacek merged commit 17fb607 into cwacek:master May 15, 2023
cwacek added a commit that referenced this pull request May 15, 2023
@cwacek
Copy link
Copy Markdown
Owner

cwacek commented May 15, 2023

@felixonmars @fmigneault: apologies for the delay merging this in. I haven't had as much time as one would hope to maintain this repo. The updated package is released as 0.4.2

@fmigneault
Copy link
Copy Markdown
Contributor Author

@cwacek
No worries. Thanks.

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

Successfully merging this pull request may close these issues.

Invalid resolution of nested oneOf for schemas of differing types

3 participants