Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeTypeHandler: allow bare types #541

Merged
merged 2 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 6 additions & 0 deletions tests/models/xsd/test_attribute.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest import TestCase

from xsdata.exceptions import SchemaValueError
from xsdata.models.enums import Namespace
from xsdata.models.enums import UseType
from xsdata.models.xsd import Attribute
from xsdata.models.xsd import Length
Expand Down Expand Up @@ -64,7 +65,12 @@ def test_get_restrictions(self):

def test_property_bases(self):
obj = Attribute()
obj.ns_map["xs"] = Namespace.XS.uri
self.assertEqual(["xs:string"], list(obj.bases))

obj.simple_type = SimpleType()
self.assertEqual([], list(obj.bases))

obj.type = "foo"
obj.simple_type = None
self.assertEqual(["foo"], list(obj.bases))
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
2 changes: 2 additions & 0 deletions xsdata/models/xsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ class Attribute(AnnotationBase):
def bases(self) -> Iterator[str]:
if self.type:
yield self.type
elif not self.has_children:
yield DataType.STRING.prefixed(self.xs_prefix)

@property
def is_attribute(self) -> bool:
Expand Down