Skip to content

Commit 37c3f10

Browse files
authored
Merge pull request #7 from dapper91/dev
- attribute default namespace bug fixed.
2 parents b56c55b + aeacca8 commit 37c3f10

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

CHANGELOG.rst

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
0.2.2 (2022-10-07)
5+
------------------
6+
7+
- attribute default namespace bug fixed.
8+
49

510
0.2.1 (2022-10-06)
611
------------------

pydantic_xml/serializers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def __init__(
221221
ns = ctx.entity_ns or (ctx.parent_ns if ns_attrs else None)
222222
nsmap = ctx.parent_nsmap
223223

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

226226
def serialize(
227227
self, element: etree.Element, value: Any, *, encoder: XmlEncoder, skip_empty: bool = False,

pydantic_xml/utils.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,23 @@ def from_uri(cls, uri: str) -> 'QName':
3535
return cls(tag=m[3], ns=m[2])
3636

3737
@classmethod
38-
def from_alias(cls, tag: str, ns: Optional[str] = None, nsmap: Optional[NsMap] = None) -> 'QName':
38+
def from_alias(
39+
cls, tag: str, ns: Optional[str] = None, nsmap: Optional[NsMap] = None, is_attr: bool = False,
40+
) -> 'QName':
3941
"""
4042
Creates `QName` from namespace alias.
4143
4244
:param tag: entity tag
4345
:param ns: xml namespace alias
4446
:param nsmap: xml namespace mapping
47+
:param is_attr: is the tag of attribute type
4548
:return: qualified name
4649
"""
4750

48-
return QName(tag=tag, ns=nsmap.get(ns or '') if nsmap else None)
51+
if not is_attr or ns is not None:
52+
ns = nsmap.get(ns or '') if nsmap else None
53+
54+
return QName(tag=tag, ns=ns)
4955

5056
@property
5157
def uri(self) -> str:

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pydantic-xml"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
description = "pydantic xml serialization/deserialization extension"
55
authors = ["Dmitry Pershin <[email protected]>"]
66
license = "Unlicense"

tests/test_namespaces.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88

99
def test_default_namespaces():
10-
class TestSubMode2(BaseXmlModel):
10+
class TestSubMode2(BaseXmlModel, nsmap={'tst': 'http://test3.org'}):
11+
attr1: int = attr()
12+
attr2: int = attr(ns='tst')
1113
element: str = element()
1214

1315
class TestSubModel1(BaseXmlModel):
@@ -19,7 +21,7 @@ class TestModel(BaseXmlModel, tag='model'):
1921
xml = '''
2022
<model>
2123
<submodel1 xmlns="http://test1.org">
22-
<submodel2 xmlns="http://test2.org">
24+
<submodel2 xmlns="http://test2.org" xmlns:tst="http://test3.org" attr1="1" tst:attr2="2">
2325
<element>value</element>
2426
</submodel2>
2527
</submodel1>
@@ -28,7 +30,7 @@ class TestModel(BaseXmlModel, tag='model'):
2830

2931
actual_obj = TestModel.from_xml(xml)
3032
expected_obj = TestModel(
31-
submodel1=TestSubModel1(submodel2=TestSubMode2(element='value')),
33+
submodel1=TestSubModel1(submodel2=TestSubMode2(element='value', attr1=1, attr2=2)),
3234
)
3335

3436
assert actual_obj == expected_obj

0 commit comments

Comments
 (0)