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

Ignore xsi attrs when fail on unknown attributes is enabled #846

Merged
merged 1 commit into from
Aug 20, 2023
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ repos:
- id: docformatter
args: [ "--in-place", "--pre-summary-newline" ]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.0
rev: v1.5.1
hooks:
- id: mypy
files: ^(xsdata/)
Expand Down
9 changes: 9 additions & 0 deletions tests/formats/dataclass/parsers/nodes/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ def test_bind_with_fail_on_unknown_attributes(self):

self.assertEqual("Unknown attribute ExtendedType:a", str(cm.exception))

def test_bind_with_fail_on_unknown_attributes_ignores_xsi_attributes(self):
self.node.meta = self.context.build(ExtendedType)
self.node.config.fail_on_unknown_attributes = True
self.node.attrs = {QNames.XSI_TYPE: "b"}

objects = []
self.node.bind("foo", "text", "tail", objects)
self.assertEqual(1, len(objects))

@mock.patch("xsdata.formats.dataclass.parsers.nodes.element.logger.warning")
def test_bind_objects(self, mock_warning):
self.node.meta = self.context.build(TypeC)
Expand Down
7 changes: 6 additions & 1 deletion xsdata/formats/dataclass/parsers/nodes/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from xsdata.formats.dataclass.parsers.utils import PendingCollection
from xsdata.logger import logger
from xsdata.models.enums import DataType
from xsdata.models.enums import Namespace
from xsdata.utils.namespaces import target_uri


class ElementNode(XmlNode):
Expand Down Expand Up @@ -134,7 +136,10 @@ def bind_attrs(self, params: Dict):
if var:
self.bind_any_attr(params, var, qname, value)
else:
if self.config.fail_on_unknown_attributes:
if (
self.config.fail_on_unknown_attributes
and target_uri(qname) != Namespace.XSI.uri
):
raise ParserError(
f"Unknown attribute {self.meta.qname}:{qname}"
)
Expand Down