Skip to content
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

Wrong behaviour for enum keyword #1208

Merged
merged 2 commits into from
Jan 16, 2024
Merged

Conversation

otto-ifak
Copy link
Contributor

@otto-ifak otto-ifak commented Jan 8, 2024

The following schema accepts the instance although it should be rejected:

from jsonschema import validate
schema = {
    "enum": [
        [False]
    ],
    "$schema": "https://json-schema.org/draft/2020-12/schema"
}
instance = [0]
validate(instance, schema)  # erroneously accepts the instance

I guess, this is caused by the following line:

elif instance not in enums:

([0] in [[False]] is true, so [0] not in [[False]] is false)


📚 Documentation preview 📚: https://python-jsonschema--1208.org.readthedocs.build/en/1208/

@otto-ifak
Copy link
Contributor Author

There is currently no test for this PR, maybe you could give me a hint where to put it?
Also, I am not sure if this change has a performance impact.

@Julian
Copy link
Member

Julian commented Jan 9, 2024

Thanks for the PR! I haven't looked carefully at it yet, but as for:

There is currently no test for this PR, maybe you could give me a hint where to put it?

The first step here would be a PR to the official JSON Schema test suite, specifically in the enum.json test file for each draft.

Once that's merged, we can bring the update in here, and then should be able to consider the PR.

Can you have a look there and let me know if you run into any trouble?

@Julian Julian added Waiting for Author A PR waiting for author changes or issue waiting on reporter feedback Needs Test Upstream Issues that need to have a test added to https://github.com/json-schema-org/JSON-Schema-Test-Suite labels Jan 12, 2024
@otto-ifak
Copy link
Contributor Author

Thanks for your reply. I have created the PR:

json-schema-org/JSON-Schema-Test-Suite#712

@gregsdennis
Copy link

I'm not sure how implicit casting works in python, but is it possible that [0] and [False] are the same thing in python?

(I get that they're not the same in JSON.)

@otto-ifak
Copy link
Contributor Author

@gregsdennis yes, that is right:
In Python 0 == False holds, so that [0] == [False] holds and so does [0] in [[False]] hold, which is the cause for this issue.

@Julian Julian removed Needs Test Upstream Issues that need to have a test added to https://github.com/json-schema-org/JSON-Schema-Test-Suite Waiting for Author A PR waiting for author changes or issue waiting on reporter feedback labels Jan 16, 2024
@Julian
Copy link
Member

Julian commented Jan 16, 2024

Thanks again @otto-ifak!

@Julian Julian merged commit 3e46f22 into python-jsonschema:main Jan 16, 2024
99 checks passed
@otto-ifak
Copy link
Contributor Author

@Julian you're welcome - thank you for this great library 😄

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.

3 participants