Skip to content

Commit

Permalink
indexed fields serialization fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
dapper91 committed Aug 25, 2019
1 parent a27bc58 commit ae643bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
25 changes: 20 additions & 5 deletions paxb/mappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,18 @@ def __init__(self, name, ns=None, ns_map=None, idx=None, required=True):
self.idx = idx
self.required = required

def xml(self, obj, root, name=None, ns=None, ns_map=None, _=None, encoder=default_encoder):
# TODO idx
def xml(self, obj, root, name=None, ns=None, ns_map=None, idx=None, encoder=default_encoder):
name = first(self.name, name)
ns = first(self.ns, ns)
ns_map = merge_dicts(self.ns_map, ns_map)
idx = first(idx, self.idx, 1)

existing_elements = root.findall(qname(ns=ns_map.get(ns), name=name), ns_map)
if idx > len(existing_elements) + 1:
raise exc.SerializationError(
"serialization can't be completed because {name}[{cur}] is going to be serialized, "
"but {name}[{prev}] is not serialized.".format(name=name, cur=idx, prev=idx-1)
)

if obj is None:
if self.required:
Expand Down Expand Up @@ -241,7 +248,8 @@ def xml(self, obj, root, name=None, ns=None, ns_map=None, idx=None, encoder=defa
existing_elements = root.findall(qname(ns=ns_map.get(ns), name=self.name), ns_map)
if idx > len(existing_elements) + 1:
raise exc.SerializationError(
"element {} at index {} is going to be serialized, but the previous one is omitted".format(name, idx)
"serialization can't be completed because {name}[{cur}] is going to be serialized, "
"but {name}[{prev}] is not serialized.".format(name=name, cur=idx, prev=idx - 1)
)
if idx == len(existing_elements) + 1:
element = et.Element(qname(ns=ns_map.get(ns), name=self.name))
Expand Down Expand Up @@ -321,11 +329,18 @@ def __init__(self, cls, name=None, ns=None, ns_map=None, idx=None, required=True
self.idx = idx
self.required = required

def xml(self, obj, root, name=None, ns=None, ns_map=None, _=None, encoder=default_encoder):
# TODO idx
def xml(self, obj, root, name=None, ns=None, ns_map=None, idx=None, encoder=default_encoder):
name = first(self.name, name)
ns = first(self.ns, ns)
ns_map = merge_dicts(self.ns_map, ns_map)
idx = first(idx, self.idx, 1)

existing_elements = root.findall(qname(ns=ns_map.get(ns), name=name), ns_map)
if idx > len(existing_elements) + 1:
raise exc.SerializationError(
"serialization can't be completed because {name}[{cur}] is going to be serialized, "
"but {name}[{prev}] is not serialized.".format(name=name, cur=idx, prev=idx-1)
)

if obj is None:
if self.required:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,17 @@ def test_order():
@pb.model(order=('element1', 'element2'))
class TestModel:
element1 = pb.attr()


def test_indexes_deserialization():

@pb.model()
class TestModel:
field = pb.field(idx=2)

obj = TestModel(field="value1")
with pytest.raises(
exc.SerializationError,
match=r"serialization can't be completed because field\[2\] is going to be serialized, "
r"but field\[1\] is not serialized."):
pb.to_xml(obj)

0 comments on commit ae643bf

Please sign in to comment.