Skip to content

Commit

Permalink
Merge pull request #1062 from tefra/fix-1059
Browse files Browse the repository at this point in the history
fix: Reset attr types derived from empty simple types
  • Loading branch information
tefra authored Jun 29, 2024
2 parents 3ad50f2 + 6135e9a commit cce0a16
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
22 changes: 21 additions & 1 deletion docs/codegen/download_schemas.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
# Download Schemas

Generating from remote resources is not a great idea, the cli includes a command to
download schemas and wsdl locally.
download schemas and wsdl locally. The command will download any included schemas
recursively.

```console exec="1" source="console"
$ xsdata download --help
```

**Example**

```console
xsdata download https://www.w3.org/Math/XMLSchema/mathml3/mathml3.xsd -o ~/schemas
========= xsdata v24.6.1 / Python 3.11.8 / Platform linux =========

Setting base path to https:/www.w3.org/Math/XMLSchema/mathml3
Fetching https://www.w3.org/Math/XMLSchema/mathml3/mathml3.xsd
Fetching https://www.w3.org/Math/XMLSchema/mathml3/mathml3-content.xsd
Fetching https://www.w3.org/Math/XMLSchema/mathml3/mathml3-strict-content.xsd
Writing /home/chris/schemas/mathml3-strict-content.xsd
Writing /home/chris/schemas/mathml3-content.xsd
Fetching https://www.w3.org/Math/XMLSchema/mathml3/mathml3-presentation.xsd
Writing /home/chris/schemas/mathml3-presentation.xsd
Fetching https://www.w3.org/Math/XMLSchema/mathml3/mathml3-common.xsd
Writing /home/chris/schemas/mathml3-common.xsd
Writing /home/chris/schemas/mathml3.xsd
```
14 changes: 13 additions & 1 deletion tests/codegen/handlers/test_process_attributes_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_process_dependency_type_with_absent_type(
attr_type = attr.types[0]

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

@mock.patch.object(ProcessAttributeTypes, "copy_attribute_properties")
@mock.patch.object(ProcessAttributeTypes, "find_dependency")
Expand Down Expand Up @@ -299,6 +299,18 @@ def test_copy_attribute_properties(self, mock_copy_inner_class):
]
)

@mock.patch.object(ProcessAttributeTypes, "reset_attribute_type")
def test_copy_attribute_properties_from_empty_source(
self, mock_reset_attribute_type
):
source = ClassFactory.create()
target = ClassFactory.elements(1)
attr = target.attrs[0]

self.processor.copy_attribute_properties(source, target, attr, attr.types[0])

mock_reset_attribute_type.assert_called_once_with(attr.types[0])

def test_copy_attribute_properties_from_nillable_source(self):
source = ClassFactory.elements(1, nillable=True)
target = ClassFactory.elements(1)
Expand Down
14 changes: 9 additions & 5 deletions xsdata/codegen/handlers/process_attributes_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def process_dependency_type(self, target: Class, attr: Attr, attr_type: AttrType
source = self.find_dependency(attr_type, attr.tag)
if not source:
logger.warning("Reset absent type: %s", attr_type.name)
self.reset_attribute_type(attr_type, True)
self.reset_attribute_type(attr_type)
elif source.is_enumeration:
attr.restrictions.min_length = None
attr.restrictions.max_length = None
Expand Down Expand Up @@ -230,6 +230,11 @@ def copy_attribute_properties(
Raises:
AnalyzerValueError: if the source class has more than one attributes
"""
if not source.attrs:
logger.warning("Reset absent simple type: %s", attr_type.name)
cls.reset_attribute_type(attr_type)
return

source_attr = source.attrs[0]
index = attr.types.index(attr_type)
attr.types.pop(index)
Expand Down Expand Up @@ -257,17 +262,16 @@ def copy_attribute_properties(
attr.default = attr.default or source_attr.default

@classmethod
def reset_attribute_type(cls, attr_type: AttrType, use_str: bool = True):
"""Reset the attribute type to string or any simple type.
def reset_attribute_type(cls, attr_type: AttrType):
"""Reset the attribute type to string.
The method will also unset the circular/forward flags, as native
types only depend on python builtin types.
Args:
attr_type: The attr type instance to reset
use_str: Whether to use xs:string or xs:anySimpleType
"""
attr_type.qname = str(DataType.STRING if use_str else DataType.ANY_SIMPLE_TYPE)
attr_type.qname = str(DataType.STRING)
attr_type.native = True
attr_type.circular = False
attr_type.forward = False
Expand Down

0 comments on commit cce0a16

Please sign in to comment.