Skip to content

Commit

Permalink
AttributeTypeHandler: allow bare types
Browse files Browse the repository at this point in the history
  • Loading branch information
tefra committed Jun 24, 2021
1 parent db20145 commit 717f672
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
33 changes: 19 additions & 14 deletions tests/codegen/handlers/test_attribute_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,6 @@ def test_process_dependency_type_with_absent_type(
self.processor.process_dependency_type(target, attr, attr_type)
mock_reset_attribute_type.assert_called_once_with(attr_type, True)

@mock.patch.object(AttributeTypeHandler, "reset_attribute_type")
@mock.patch.object(AttributeTypeHandler, "find_dependency")
def test_process_dependency_type_with_dummy_type(
self, mock_find_dependency, mock_reset_attribute_type
):
mock_find_dependency.return_value = ClassFactory.create(tag=Tag.ELEMENT)
target = ClassFactory.create()
attr = AttrFactory.create()
attr_type = attr.types[0]

self.processor.process_dependency_type(target, attr, attr_type)
mock_reset_attribute_type.assert_called_once_with(attr_type, False)

@mock.patch.object(AttributeTypeHandler, "copy_attribute_properties")
@mock.patch.object(AttributeTypeHandler, "find_dependency")
def test_process_dependency_type_with_simple_type(
Expand Down Expand Up @@ -260,7 +247,7 @@ def test_copy_attribute_properties_from_nillable_source(self):
self.processor.copy_attribute_properties(source, target, attr, attr.types[0])
self.assertTrue(attr.restrictions.nillable)

def test_copy_attribute_properties_to_attribute_target(self):
def test_copy_attribute_properties_to_attribute_target_preserve_occurrences(self):
source = ClassFactory.elements(1, nillable=True)
target = ClassFactory.create(attrs=AttrFactory.list(1, tag=Tag.ATTRIBUTE))
attr = target.attrs[0]
Expand All @@ -274,6 +261,24 @@ def test_copy_attribute_properties_to_attribute_target(self):
self.processor.copy_attribute_properties(source, target, attr, attr.types[0])
self.assertFalse(attr.is_optional)

def test_copy_attribute_properties_set_default_value_if_none(self):
target = ClassFactory.create(attrs=AttrFactory.list(1, tag=Tag.ATTRIBUTE))
attr = target.attrs[0]

source = ClassFactory.elements(1)
source.attrs[0].default = "foo"
source.attrs[0].fixed = True

self.processor.copy_attribute_properties(source, target, attr, attr.types[0])
self.assertEqual("foo", attr.default)
self.assertTrue("foo", attr.fixed)

source.attrs[0].default = "bar"
source.attrs[0].fixed = False
self.processor.copy_attribute_properties(source, target, attr, attr.types[0])
self.assertEqual("foo", attr.default)
self.assertTrue("foo", attr.fixed)

@mock.patch.object(AttributeTypeHandler, "is_circular_dependency")
def test_set_circular_flag(self, mock_is_circular_dependency):
source = ClassFactory.create()
Expand Down
8 changes: 5 additions & 3 deletions xsdata/codegen/handlers/attribute_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ def process_dependency_type(self, target: Class, attr: Attr, attr_type: AttrType
Process an attributes type that depends on any global type.
Strategies:
1. Reset absent or dummy attribute types with a warning
1. Reset absent types with a warning
2. Copy attribute properties from a simple type
3. Copy format restriction from an enumeration
4. Set circular flag for the rest
"""

source = self.find_dependency(attr_type, attr.tag)
if not source or (not source.attrs and not source.extensions):
logger.warning("Reset dummy type: %s", attr_type.name)
if not source:
logger.warning("Reset absent type: %s", attr_type.name)
use_str = not source or not source.is_complex
self.reset_attribute_type(attr_type, use_str)
elif source.is_simple_type:
Expand Down Expand Up @@ -167,6 +167,8 @@ def copy_attribute_properties(

attr.restrictions = restrictions
attr.help = attr.help or source_attr.help
attr.fixed = attr.fixed or source_attr.fixed
attr.default = attr.default or source_attr.default

def set_circular_flag(self, source: Class, target: Class, attr_type: AttrType):
"""Update circular reference flag."""
Expand Down

0 comments on commit 717f672

Please sign in to comment.