Skip to content

Non-unique array items with uniqueItems=True does not fail validation in 4.0.1+ #872

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

Closed
DrGFreeman opened this issue Nov 3, 2021 · 3 comments

Comments

@DrGFreeman
Copy link
Contributor

Description

An array with non-unique items does not fail validation when uniqueItems is True.

Steps to reproduce

Run the following test:

import jsonschema
import pytest


def test_unique_items():
    """names has non-unique items and is expected to raise a validation error."""
    instance = {"names": ["foo", "bar", "foo"]}
    schema = {
        "properties": {"names": {"type": "array", "uniqueItems": True}},
        "type": "object",
    }

    with pytest.raises(
        jsonschema.exceptions.ValidationError, match="Failed validating 'uniqueItems'"
    ):
        jsonschema.validate(instance=instance, schema=schema)

Expected behavior

The test is expected to pass, i.e. a jsonschema.exceptions.ValidationError is raised:

jsonschema.exceptions.ValidationError: ['foo', 'bar', 'foo'] has non-unique elements

    Failed validating 'uniqueItems' in schema['properties']['names']:
        {'type': 'array', 'uniqueItems': True}

    On instance['names']:
        ['foo', 'bar', 'foo']

Actual behavior

The test fails, i.e. no exception is raised:

>           jsonschema.validate(instance=instance, schema=schema)
E           Failed: DID NOT RAISE <class 'jsonschema.exceptions.ValidationError'>

Version affected: 4.0.1 and up;

nox > Ran multiple sessions:
nox > * tests(jsonschema='3.2.0'): success
nox > * tests(jsonschema='4.0.1'): failed
nox > * tests(jsonschema='4.1.0'): failed
nox > * tests(jsonschema='4.1.1'): failed
nox > * tests(jsonschema='4.1.2'): failed
@Julian
Copy link
Member

Julian commented Nov 3, 2021

Hi. Can you include an example that runs the same thing on both versions? Your schema doesn't set a $schema key to declare what version you're trying to use, so you're comparing behavior across two different versions of the JSON Schema specification.

@Julian
Copy link
Member

Julian commented Nov 3, 2021

OK I see, setting that myself, this looks like a duplicate of the unfortunate #866.

A PR is definitely welcome, tests are needed upstream as a first step.

@Julian Julian closed this as completed Nov 3, 2021
@DrGFreeman
Copy link
Contributor Author

Oh! Sorry for the duplicate, I had looked for this issue but that was a few weeks ago and did not verify again before raising this one.

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

No branches or pull requests

2 participants