Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

0.2.2 (2022-10-07)
------------------

- attribute default namespace bug fixed.


0.2.1 (2022-10-06)
------------------
Expand Down
2 changes: 1 addition & 1 deletion pydantic_xml/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def __init__(
ns = ctx.entity_ns or (ctx.parent_ns if ns_attrs else None)
nsmap = ctx.parent_nsmap

self.attr_name = QName.from_alias(tag=name, ns=ns, nsmap=nsmap).uri
self.attr_name = QName.from_alias(tag=name, ns=ns, nsmap=nsmap, is_attr=True).uri

def serialize(
self, element: etree.Element, value: Any, *, encoder: XmlEncoder, skip_empty: bool = False,
Expand Down
10 changes: 8 additions & 2 deletions pydantic_xml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,23 @@ def from_uri(cls, uri: str) -> 'QName':
return cls(tag=m[3], ns=m[2])

@classmethod
def from_alias(cls, tag: str, ns: Optional[str] = None, nsmap: Optional[NsMap] = None) -> 'QName':
def from_alias(
cls, tag: str, ns: Optional[str] = None, nsmap: Optional[NsMap] = None, is_attr: bool = False,
) -> 'QName':
"""
Creates `QName` from namespace alias.

:param tag: entity tag
:param ns: xml namespace alias
:param nsmap: xml namespace mapping
:param is_attr: is the tag of attribute type
:return: qualified name
"""

return QName(tag=tag, ns=nsmap.get(ns or '') if nsmap else None)
if not is_attr or ns is not None:
ns = nsmap.get(ns or '') if nsmap else None

return QName(tag=tag, ns=ns)

@property
def uri(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pydantic-xml"
version = "0.2.1"
version = "0.2.2"
description = "pydantic xml serialization/deserialization extension"
authors = ["Dmitry Pershin <dapper1291@gmail.com>"]
license = "Unlicense"
Expand Down
8 changes: 5 additions & 3 deletions tests/test_namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@


def test_default_namespaces():
class TestSubMode2(BaseXmlModel):
class TestSubMode2(BaseXmlModel, nsmap={'tst': 'http://test3.org'}):
attr1: int = attr()
attr2: int = attr(ns='tst')
element: str = element()

class TestSubModel1(BaseXmlModel):
Expand All @@ -19,7 +21,7 @@ class TestModel(BaseXmlModel, tag='model'):
xml = '''
<model>
<submodel1 xmlns="http://test1.org">
<submodel2 xmlns="http://test2.org">
<submodel2 xmlns="http://test2.org" xmlns:tst="http://test3.org" attr1="1" tst:attr2="2">
<element>value</element>
</submodel2>
</submodel1>
Expand All @@ -28,7 +30,7 @@ class TestModel(BaseXmlModel, tag='model'):

actual_obj = TestModel.from_xml(xml)
expected_obj = TestModel(
submodel1=TestSubModel1(submodel2=TestSubMode2(element='value')),
submodel1=TestSubModel1(submodel2=TestSubMode2(element='value', attr1=1, attr2=2)),
)

assert actual_obj == expected_obj
Expand Down