Skip to content

Commit

Permalink
feat: Generate compound field choices max occurs (#994)
Browse files Browse the repository at this point in the history
Resolves #742
  • Loading branch information
tefra committed Mar 23, 2024
1 parent e652e08 commit cd65e54
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 11 deletions.
5 changes: 1 addition & 4 deletions tests/codegen/handlers/test_create_compound_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,7 @@ def test_build_attr_choice(self):
choice="abc",
sequence=1,
)
expected_res = attr.restrictions.clone()
expected_res.min_occurs = None
expected_res.max_occurs = None
expected_res.sequence = None
expected_res = attr.restrictions.clone(min_occurs=0, sequence=None)

actual = self.processor.build_attr_choice(attr)

Expand Down
2 changes: 1 addition & 1 deletion tests/formats/dataclass/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def test_field_choices(self):
"wildcard": True,
"type": "Type[object]",
},
{"default": "'aa'", "name": "bar", "type": "Type[str]"},
{"name": "bar", "type": "Type[str]"},
{
"default_factory": "list",
"name": "tok",
Expand Down
5 changes: 1 addition & 4 deletions xsdata/codegen/handlers/create_compound_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,7 @@ def build_attr_choice(cls, attr: Attr) -> Attr:
Returns:
The new choice attr for the compound attr.
"""
restrictions = attr.restrictions.clone()
restrictions.min_occurs = None
restrictions.max_occurs = None
restrictions.sequence = None
restrictions = attr.restrictions.clone(min_occurs=0, sequence=None)

return Attr(
name=attr.name,
Expand Down
5 changes: 3 additions & 2 deletions xsdata/formats/dataclass/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,9 @@ def field_choices(
if choice.is_nameless:
del metadata["name"]

default_key = self.FACTORY_KEY if choice.is_factory else self.DEFAULT_KEY
metadata[default_key] = self.field_default_value(choice)
if choice.is_tokens:
metadata[self.FACTORY_KEY] = self.field_default_value(choice)

metadata.update(restrictions)

if self.docstring_style == DocstringStyle.ACCESSIBLE and choice.help:
Expand Down

0 comments on commit cd65e54

Please sign in to comment.