Skip to content

Commit

Permalink
Explicit required field metadata property
Browse files Browse the repository at this point in the history
Notes:
Removed a couple of hacks that omitted generating
the required property.
  • Loading branch information
tefra committed Aug 2, 2021
1 parent 79a9889 commit 7234cf3
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 24 deletions.
7 changes: 1 addition & 6 deletions tests/formats/dataclass/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,9 @@ def test_field_metadata_restrictions(self):
expected = {"nillable": True, "max_inclusive": 2}
self.assertEqual(expected, self.filters.field_metadata(attr, None, []))

attr.default = "foo"
attr.restrictions.nillable = False
expected = {"max_inclusive": 2}
self.assertEqual(expected, self.filters.field_metadata(attr, None, []))

attr.default = None
attr.restrictions.tokens = True
expected = {"max_inclusive": 2, "tokens": True}
expected = {"max_inclusive": 2, "nillable": True, "tokens": True}
self.assertEqual(expected, self.filters.field_metadata(attr, None, []))

def test_field_metadata_mixed(self):
Expand Down
7 changes: 0 additions & 7 deletions tests/models/xsd/test_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ def test_get_restrictions(self):
obj = Attribute()
self.assertEqual({"max_occurs": 1, "min_occurs": 0}, obj.get_restrictions())

obj.default = "foo"
self.assertEqual({"max_occurs": 1, "min_occurs": 1}, obj.get_restrictions())
obj.default = None
obj.fixed = "foo"
self.assertEqual({"max_occurs": 1, "min_occurs": 1}, obj.get_restrictions())

obj.fixed = None
obj.use = UseType.REQUIRED
expected = {"max_occurs": 1, "min_occurs": 1}
self.assertEqual(expected, obj.get_restrictions())
Expand Down
3 changes: 0 additions & 3 deletions xsdata/formats/dataclass/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,6 @@ def field_metadata(

restrictions = attr.restrictions.asdict(attr.native_types)

if attr.default or attr.is_factory:
restrictions.pop("required", None)

metadata = {
"name": name,
"type": attr.xml_type,
Expand Down
11 changes: 3 additions & 8 deletions xsdata/models/xsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,12 @@ def attr_types(self) -> Iterator[str]:
yield self.ref

def get_restrictions(self) -> Dict[str, Anything]:
restrictions = {}

if self.default or self.fixed:
self.use = UseType.REQUIRED

if self.use == UseType.REQUIRED:
restrictions.update({"min_occurs": 1, "max_occurs": 1})
restrictions = {"min_occurs": 1, "max_occurs": 1}
elif self.use == UseType.PROHIBITED:
restrictions.update({"max_occurs": 0, "min_occurs": 0})
restrictions = {"max_occurs": 0, "min_occurs": 0}
else:
restrictions.update({"max_occurs": 1, "min_occurs": 0})
restrictions = {"max_occurs": 1, "min_occurs": 0}

if self.simple_type:
restrictions.update(self.simple_type.get_restrictions())
Expand Down

0 comments on commit 7234cf3

Please sign in to comment.