Skip to content

Commit b5b7cba

Browse files
committed
resolving uniqueItems is stable now
1 parent 4b0d76c commit b5b7cba

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

fastjsonschema/draft04.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,16 @@ def generate_unique_items(self):
349349
"""
350350
self.create_variable_is_list()
351351
with self.l('if {variable}_is_list:'):
352+
self.l(
353+
'def fn(var): '
354+
'return frozenset(dict((k, fn(v)) '
355+
'for k, v in var.items()).items()) '
356+
'if hasattr(var, "items") else tuple(fn(v) '
357+
'for v in var) '
358+
'if isinstance(var, (dict, list)) else str(var) '
359+
'if isinstance(var, bool) else var')
352360
self.create_variable_with_length()
353-
with self.l('if {variable}_len > len(set(str({variable}_x) for {variable}_x in {variable})):'):
361+
with self.l('if {variable}_len > len(set(fn({variable}_x) for {variable}_x in {variable})):'):
354362
self.exc('{name} must contain unique items', rule='uniqueItems')
355363

356364
def generate_items(self):

tests/conftest.py

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
from fastjsonschema.draft07 import CodeGeneratorDraft07
1414

1515

16+
def pytest_configure(config):
17+
config.addinivalue_line("markers", "none")
18+
config.addinivalue_line("markers", "benchmark")
19+
20+
1621
@pytest.fixture
1722
def asserter():
1823
def f(definition, value, expected, formats={}):

tests/test_array.py

+14
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ def test_min_items(asserter, value, expected):
5151
([1], [1]),
5252
([1, 1], JsonSchemaException('data must contain unique items', value='{data}', name='data', definition='{definition}', rule='uniqueItems')),
5353
([1, 2, 3], [1, 2, 3]),
54+
([True, False], [True, False]),
55+
([True, True], JsonSchemaException('data must contain unique items', value='{data}', name='data', definition='{definition}', rule='uniqueItems')),
56+
(['abc', 'bce', 'hhh'], ['abc', 'bce', 'hhh']),
57+
(['abc', 'abc'], JsonSchemaException('data must contain unique items', value='{data}', name='data', definition='{definition}', rule='uniqueItems')),
58+
([{'a': 'a'}, {'b': 'b'}], [{'a': 'a'}, {'b': 'b'}]),
59+
([{'a': 'a'}, {'a': 'a'}], JsonSchemaException('data must contain unique items', value='{data}', name='data', definition='{definition}', rule='uniqueItems')),
60+
([{'a': 'a', 'b': 'b'}, {'b': 'b', 'c': 'c'}], [{'a': 'a', 'b': 'b'}, {'b': 'b', 'c': 'c'}]),
61+
([{'a': 'a', 'b': 'b'}, {'b': 'b', 'a': 'a'}], JsonSchemaException('data must contain unique items', value='{data}', name='data', definition='{definition}', rule='uniqueItems')),
62+
([1, '1'], [1, '1']),
63+
([{'a': 'b'}, "{'a': 'b'}"], [{'a': 'b'}, "{'a': 'b'}"]),
64+
([[1, 2], [2, 1]], [[1, 2], [2, 1]]),
65+
([[1, 2], [1, 2]], JsonSchemaException('data must contain unique items', value='{data}', name='data', definition='{definition}', rule='uniqueItems')),
66+
([{'a': {'b': {'c': [1, 2]}}}, {'a': {'b': {'c': [1, 2]}}}], JsonSchemaException('data must contain unique items', value='{data}', name='data', definition='{definition}', rule='uniqueItems')),
67+
([{'a': {'b': {'c': [2, 1]}}}, {'a': {'b': {'c': [1, 2]}}}], [{'a': {'b': {'c': [2, 1]}}}, {'a': {'b': {'c': [1, 2]}}}]),
5468
])
5569
def test_unique_items(asserter, value, expected):
5670
asserter({

0 commit comments

Comments
 (0)