Skip to content

Resolve $id and $ref inside schema arrays - #235

Open
chinmayajha wants to merge 1 commit into
horejsek:masterfrom
chinmayajha:fix-ref-in-schema-arrays
Open

chinmayajha wants to merge 1 commit into
horejsek:masterfrom
chinmayajha:fix-ref-in-schema-arrays

Conversation

@chinmayajha

Copy link
Copy Markdown

RefResolver.walk only went into dict values, so subschemas in allOf, anyOf, oneOf and items arrays were never walked. An $id there was never registered, and a relative $ref there was not joined with the base URI of its parent $id:

import fastjsonschema

schema = {
    '$schema': 'http://json-schema.org/draft-07/schema',
    'allOf': [
        {'$ref': 'http://example.com/ref/if'},
        {'if': {'$id': 'http://example.com/ref/if', 'type': 'integer'}},
    ],
}
fastjsonschema.validate(schema, 12)
# tries to download http://example.com/ref/if

The walk now follows the keywords that hold subschemas and skips the data keywords (const, default, enum, examples). Before, a $ref inside a const or default value was rewritten to an absolute URI, so the const no longer matched its own value. _expand_refs in the generator skips the same keywords, so a $ref inside data is not resolved when building error messages.

With this, ref.json from the test suite passes for draft 06 and 07 and id.json passes for draft 04, 06 and 07, so I removed them from the ignore lists. I also added a test in tests/test_integration.py for a $ref inside const under a nested $id, which fails on master. The full suite passes and pylint is unchanged. CHANGELOG has entries under UNRELEASED.

RefResolver.walk only descended into dict values, so subschemas in
allOf, anyOf, oneOf and items arrays were never walked: an $id there
was not registered, and a relative $ref there was not joined with the
base URI of its parent $id. It also walked into const and default
values, rewriting a $ref there and registering an $id from data.

Walk the keywords that hold subschemas and skip the data keywords.
ref.json passes for draft 06 and 07, and id.json for draft 04, 06
and 07.
@chinmayajha

Copy link
Copy Markdown
Author

@horejsek could you approve the workflow runs and review this when you have a moment?

This branch has not been deployed

No deployments
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.

1 participant