Skip to content

Commit

Permalink
fix: Dict mapper to recognize optional attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
tefra committed Apr 14, 2024
1 parent f14630f commit f6eaa8f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
9 changes: 7 additions & 2 deletions tests/codegen/mappers/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ def test_build_class_attribute_from_empty_list(self):
AttrTypeFactory.native(DataType.ANY_SIMPLE_TYPE, tag=Tag.ELEMENT),
],
)
restrictions = Restrictions(min_occurs=1, max_occurs=sys.maxsize)
restrictions = Restrictions(min_occurs=0, max_occurs=sys.maxsize)
self.assertEqual(expected, target.attrs[0])
self.assertEqual(restrictions, target.attrs[0].restrictions)

def test_build_class_attribute_from_dict(self):
target = ClassFactory.create()
data = {"sub1": 1, "sub2": "value"}
data = {"sub1": 1, "sub2": "value", "sub3": None}
DictMapper.build_class_attribute(target, "a", data)

expected = AttrFactory.create(
Expand All @@ -113,9 +113,14 @@ def test_build_class_attribute_from_dict(self):
attrs=[
AttrFactory.native(DataType.SHORT, name="sub1"),
AttrFactory.native(DataType.STRING, name="sub2"),
AttrFactory.native(DataType.STRING, name="sub3"),
],
)

self.assertEqual(expected, target.attrs[0])
self.assertEqual(expected_inner, target.inner[0])
self.assertEqual(1, len(target.inner))

self.assertFalse(target.inner[0].attrs[0].restrictions.is_optional)
self.assertFalse(target.inner[0].attrs[1].restrictions.is_optional)
self.assertTrue(target.inner[0].attrs[2].restrictions.is_optional)
2 changes: 1 addition & 1 deletion xsdata/codegen/mappers/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ def build_class_attribute(cls, target: Class, name: str, value: Any):
else:
attr_type = cls.build_attr_type(name, value)

cls.build_attr(target, name, attr_type)
cls.build_attr(target, name, attr_type, value=value)
8 changes: 8 additions & 0 deletions xsdata/codegen/mappers/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from xsdata.utils import collections
from xsdata.utils.namespaces import split_qname

_UNSET = object()


class RawDocumentMapper:
"""Mixin class for raw json/xml documents."""
Expand All @@ -20,6 +22,7 @@ def build_attr(
parent_namespace: Optional[str] = None,
tag: str = Tag.ELEMENT,
sequence: int = 0,
value: Any = _UNSET,
):
"""Build an attr for the given class instance.
Expand All @@ -30,6 +33,7 @@ def build_attr(
parent_namespace: The parent namespace
tag: The attr tag
sequence: The attr sequence number
value: The attr sample value
"""
namespace, name = split_qname(qname)
namespace = cls.select_namespace(namespace, parent_namespace, tag)
Expand All @@ -43,6 +47,10 @@ def build_attr(

attr.restrictions.min_occurs = 1
attr.restrictions.max_occurs = 1

if value is None:
attr.restrictions.min_occurs = 0

cls.add_attribute(target, attr)

@classmethod
Expand Down

0 comments on commit f6eaa8f

Please sign in to comment.