Skip to content

Commit

Permalink
AttributeTypeHandler: filter abstract elements from class attrs
Browse files Browse the repository at this point in the history
Notes:
The schema is invalid if an element ref points
to an abstract element, the only acceptable case
is when abstract elements are used like placeholders
for substitutions groups.
  • Loading branch information
tefra committed Nov 26, 2021
1 parent a76175a commit a5fd4dd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/codegen/handlers/test_attribute_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ def test_process_dependency_type_with_complex_type(
self.processor.process_dependency_type(target, attr, attr_type)
self.assertTrue(attr.restrictions.nillable)

@mock.patch.object(AttributeTypeHandler, "find_dependency")
def test_process_dependency_type_with_abstract_type_type(
self, mock_find_dependency
):
complex_type = ClassFactory.create(tag=Tag.ELEMENT, abstract=True)
mock_find_dependency.return_value = complex_type

attr = AttrFactory.create()
attr_type = attr.types[0]
target = ClassFactory.create()
target.attrs.append(attr)

self.assertEqual(1, len(target.attrs))
self.processor.process_dependency_type(target, attr, attr_type)
self.assertEqual(0, len(target.attrs))

@mock.patch.object(AttributeTypeHandler, "update_restrictions")
@mock.patch.object(AttributeTypeHandler, "copy_attribute_properties")
def test_process_inner_type_with_simple_type(
Expand Down
4 changes: 4 additions & 0 deletions xsdata/codegen/handlers/attribute_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ def process_dependency_type(self, target: Class, attr: Attr, attr_type: AttrType
x.restrictions.format for x in source.attrs if x.restrictions.format
)
attr_type.reference = id(source)
elif source.is_element and source.abstract:
# Substitution groups with abstract elements are used like
# placeholders and shouldn't be added as standalone fields.
target.attrs.remove(attr)
else:
if source.nillable:
attr.restrictions.nillable = True
Expand Down

0 comments on commit a5fd4dd

Please sign in to comment.