python-jsonschema / python-jsonschema/jsonschema

Behavior when attempting to *override* an official dialect is not well defined when recursing into subschemas containing the official dialect's $schema identifier

Open
#994 25 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dialects v2 Enhancement
Dominant language
Python
Stars
5k
Forks
671
Avg merge
1d 1h
Merged PRs (30d)
10

Description

This is related to part of https://github.com/python-jsonschema/jsonschema/issues/981#issuecomment-1219713160; namely, the broken validation of default values in asdf. However, I believe the issue itself originates in the evolve: https://github.com/python-jsonschema/jsonschema/blob/2f15a98f9243b77ff8f546e8b28cbbdabd7fef23/jsonschema/validators.py#L222-L238 method first introduced in version 4.10.0.

Essentially, we are creating a a new validator using the Draft4Validator, which contains an additional validators function (under the default keyword). See https://github.com/asdf-format/asdf/blob/bf954a3921df6b3e5f36c211c18257ac97d4f423/asdf/schema.py#L705-L750 in the asdf code. However, it appears that the additional default validator is being lost when attempting to validate something. The following should produce a ValidationError with the latest version of asdf:

from asdf import schema

s = {"type": "object", "properties": {"a": {"type": "integer", "default": "foo"}}}

schema.check_schema(s)

Indeed, if one has jsonschema 4.9.1 installed, one gets a ValidationError; however, if one has jsonschema 4.15.0 installed, one gets no validation error.

After carefully stepping the the code as it is running with jsonschema 4.15.0 I have found that at some point while iterating, the evolve method returns a validator class which does not have default in its VALIDATORS attribute when it should have one. Making it so that there is no validator available when it does try to validate default. This occurs when validator_for does not return DefaultValidator: https://github.com/python-jsonschema/jsonschema/blob/2f15a98f9243b77ff8f546e8b28cbbdabd7fef23/jsonschema/validators.py#L1143 Instead it returns the Draft4Validator which does not contain default in its VALIDATORS dictionary. Meaning the validator returned by evolve has now lost the default validator for further iterations.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in jsonschema/validators.py at evolve (around lines 222-238) and validator_for (around line 1143), then reproduce the behavior with the asdf schema.check_schema example. Trace recursive validation of the nested subschema and compare the validator's VALIDATORS mapping. Done means the expected ValidationError is restored and regression coverage captures the custom default validator behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
json, python
Domain
backend, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.