Skip to content

Commit

Permalink
Fix uniqueItem on array of object/array fails
Browse files Browse the repository at this point in the history
  • Loading branch information
mission-liao committed Jun 17, 2017
1 parent 86f16fa commit 53f352a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pyswagger/primitives/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,18 @@ def apply_with(self, obj, val, ctx):
else:
raise SchemaError("Unsupported collection format '{0}' when converting array: {1}".format(self.__collection_format, val))

val = set(val) if obj.uniqueItems else val
# remove duplication when uniqueItems == True
if obj.uniqueItems:
if isinstance(val, (list, dict)):
seen = []
for e in val:
if e in seen:
continue
seen.append(e)
val = seen
else:
# assume the type is hashable
val = set(val)

if obj.items and len(val):
self.extend(map(functools.partial(ctx['factory'].produce, obj.items), val))
Expand Down
30 changes: 30 additions & 0 deletions pyswagger/tests/data/v2_0/schema/model/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,36 @@
"uuid":{
"type":"string",
"format":"uuid"
},
"unique_array":{
"type":"array",
"uniqueItems":true,
"items":{
"type":"array",
"items":{
"type":"string"
}
}
},
"unique_object":{
"type":"array",
"uniqueItems":true,
"items":{
"type":"object",
"properties":{
"prop_1":{
"type":"string"
},
"prop_2":{
"type":"object",
"properties":{
"prop_2_1":{
"type":"string"
}
}
}
}
}
}
}
}
40 changes: 40 additions & 0 deletions pyswagger/tests/v2_0/test_prim.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,46 @@ def test_float_dump(self):
app = App.create(get_test_data_folder(version='2.0', which=os.path.join('schema', 'floatDump')))
app.dump() # should not raise exception

def test_unique_item_on_array(self):
""" uniqueItem == True on array of array
"""

# no duplication, should work
d = self.app.resolve('#/definitions/unique_array')
arr_1 = [
['a', 'b', 'c'],
['d', 'e', 'f']
]
o = d._prim_(arr_1, self.app.prim_factory)
self.assertEqual(len(arr_1), len(o))

# duplicated, should remove duplication
arr_2 = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['a', 'b', 'c'],
]
o = d._prim_(arr_2, self.app.prim_factory)
self.assertEqual(len(o), 2)

def test_unique_item_on_object(self):
""" uniqueItem == True on array of object
"""
d = self.app.resolve('#/definitions/unique_object')
obj_1 = {'prop_1': '1-1', 'prop_2': {'prop_2_1': '1-2-1'}}
obj_1_2 = {'prop_1': '1-1', 'prop_2': {'prop_2_1': '1-2-1-2'}}
obj_2 = {'prop_1': '2-1', 'prop_2': {'prop_2_1': '2-2-1'}}

# no duplucation, should work
arr_1 = [obj_1, obj_1_2, obj_2]
o = d._prim_(arr_1, self.app.prim_factory)
self.assertEqual(len(arr_1), len(o))

# duplicated, remove duplication
arr_2 = [obj_1, obj_1_2, obj_2, obj_1, obj_1_2, obj_2]
o = d._prim_(arr_2, self.app.prim_factory)
self.assertEqual(len(o), 3)


class HeaderTestCase(unittest.TestCase):
""" test for Header object """
Expand Down

0 comments on commit 53f352a

Please sign in to comment.