Skip to content

Commit

Permalink
Prevent flattening extensions with extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
tefra committed May 1, 2021
1 parent 9909d52 commit 338c401
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 59 deletions.
4 changes: 4 additions & 0 deletions tests/codegen/handlers/test_class_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ def test_should_flatten_extension(self):
target.attrs.append(AttrFactory.create())
self.assertTrue(self.processor.should_flatten_extension(source, target))

# Source has is a subclass
source.extensions.append(ExtensionFactory.create())
self.assertFalse(self.processor.should_flatten_extension(source, target))

# Target has suffix attr
source = ClassFactory.create()
target = ClassFactory.elements(1)
Expand Down
10 changes: 5 additions & 5 deletions tests/fixtures/defxmlschema/chapter13.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
],
"umbrella": [
{
"routingNum": 123,
"number": 557,
"name": "Ten-Gallon Hat",
"description": null,
"routingNum": 123,
"lang": null,
"effDate": "2002-04-02"
"effDate": "2002-04-02",
"lang": null
}
],
"shirt": [
{
"routingNum": 124,
"number": 557,
"name": "Short-Sleeved Linen Blouse",
"description": null,
"routingNum": 124,
"lang": null,
"effDate": "2002-04-02",
"lang": null,
"size": [
{
"value": 6,
Expand Down
75 changes: 27 additions & 48 deletions tests/fixtures/defxmlschema/chapter13.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,54 +24,6 @@ class ItemType:
)


@dataclass
class RestrictedProductType:
number: Optional[int] = field(
default=None,
metadata={
"type": "Element",
"namespace": "",
"required": True,
}
)
name: Optional[str] = field(
default=None,
metadata={
"type": "Element",
"namespace": "",
"required": True,
}
)
description: Optional[str] = field(
default=None,
metadata={
"type": "Element",
"namespace": "",
}
)
routing_num: Optional[int] = field(
default=None,
metadata={
"name": "routingNum",
"type": "Attribute",
"required": True,
}
)
lang: Optional[str] = field(
default=None,
metadata={
"type": "Attribute",
}
)
eff_date: XmlDate = field(
default=XmlDate(1900, 1, 1),
metadata={
"name": "effDate",
"type": "Attribute",
}
)


@dataclass
class SizeType:
value: Optional[int] = field(
Expand Down Expand Up @@ -139,6 +91,33 @@ class SmallSizeType(SizeType):
)


@dataclass
class RestrictedProductType(ProductType):
name: Optional[str] = field(
default=None,
metadata={
"type": "Element",
"namespace": "",
"required": True,
}
)
routing_num: Optional[int] = field(
default=None,
metadata={
"name": "routingNum",
"type": "Attribute",
"required": True,
}
)
eff_date: XmlDate = field(
default=XmlDate(1900, 1, 1),
metadata={
"name": "effDate",
"type": "Attribute",
}
)


@dataclass
class ShirtType(RestrictedProductType):
size: List[SmallSizeType] = field(
Expand Down
13 changes: 7 additions & 6 deletions xsdata/codegen/handlers/class_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,15 @@ def should_flatten_extension(cls, source: Class, target: Class) -> bool:
Return whether the extension should be flattened because of rules.
Rules:
1. Source class is a simple type
2. Source class has a suffix attr and target has its own attrs
3. Target class has a suffix attr
4. Target restrictions parent attrs in different sequential order
5. Target restricts parent attr with a not matching type.
1. Source doesn't have a parent class
2. Source class is a simple type
3. Source class has a suffix attr and target has its own attrs
4. Target class has a suffix attr
5. Target restrictions parent attrs in different sequential order
6. Target restricts parent attr with a not matching type.
"""

if (
if not source.extensions and (
source.is_simple_type
or target.has_suffix_attr
or (source.has_suffix_attr and target.attrs)
Expand Down

0 comments on commit 338c401

Please sign in to comment.