Skip to content

Commit e432302

Browse files
authored
Merge pull request #136 from dapper91/dev
- bool type encoding format changed from 'True' to 'true'. - None type encoding format changed from 'None' to ''.
2 parents fc3f499 + 9e2b126 commit e432302

File tree

8 files changed

+26
-12
lines changed

8 files changed

+26
-12
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
2.3.0 (2023-10-22)
5+
------------------
6+
7+
- bool type encoding format changed from 'True' to 'true'. See https://github.com/dapper91/pydantic-xml/issues/126.
8+
- None type encoding format changed from 'None' to ''.
9+
10+
411
2.2.4 (2023-10-06)
512
------------------
613

docs/source/pages/misc.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ None type encoding
4646
__________________
4747

4848
Since xml format doesn't support ``null`` type natively it is not obvious how to encode ``None`` fields
49-
(ignore it, encode it as an empty string or mark it as ``xsi:nil``) the library doesn't implement
50-
``None`` type encoding by default.
51-
52-
You can define your own encoding format:
49+
(ignore it, encode it as an empty string or mark it as ``xsi:nil``). The library encodes ``None`` typed fields
50+
as empty strings by default but you can define your own encoding format:
5351

5452
.. literalinclude:: ../../../examples/snippets/py3.9/serialization.py
5553
:language: python

pydantic_xml/serializers/factories/primitive.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from pydantic_xml import errors
66
from pydantic_xml.element import XmlElementReader, XmlElementWriter
7-
from pydantic_xml.serializers.serializer import SearchMode, Serializer
7+
from pydantic_xml.serializers.serializer import SearchMode, Serializer, encode_primitive
88
from pydantic_xml.typedefs import EntityLocation, NsMap
99
from pydantic_xml.utils import QName, merge_nsmaps
1010

@@ -44,7 +44,7 @@ def serialize(
4444
if value is None and skip_empty:
4545
return element
4646

47-
element.set_text(str(encoded))
47+
element.set_text(encode_primitive(encoded))
4848
return element
4949

5050
def deserialize(
@@ -90,7 +90,7 @@ def serialize(
9090
if value is None and skip_empty:
9191
return element
9292

93-
element.set_attribute(self._attr_name, str(encoded))
93+
element.set_attribute(self._attr_name, encode_primitive(encoded))
9494

9595
return element
9696

pydantic_xml/serializers/serializer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
from . import factories
1616

1717

18+
def encode_primitive(value: Any) -> str:
19+
if value is None:
20+
return ''
21+
elif isinstance(value, bool):
22+
return str(value).lower()
23+
else:
24+
return str(value)
25+
26+
1827
class SchemaTypeFamily(IntEnum):
1928
META = 0
2029
PRIMITIVE = 1

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pydantic-xml"
3-
version = "2.2.4"
3+
version = "2.3.0"
44
description = "pydantic xml extension"
55
authors = ["Dmitry Pershin <dapper1291@gmail.com>"]
66
license = "Unlicense"

tests/test_encoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class TestModel(BaseXmlModel, tag='model'):
3939
<model>
4040
<field1>1</field1>
4141
<field2>1.1</field2>
42-
<field3>True</field3>
42+
<field3>true</field3>
4343
<field4>3.14</field4>
4444
<field5>2023-02-04T12:01:02</field5>
4545
<field6>2023-02-04</field6>

tests/test_generics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class GenericModel(BaseXmlModel, Generic[GenericType1, GenericType2], tag='model
2828
assert_xml_equal(actual_xml, xml1)
2929

3030
xml2 = '''
31-
<model1 attr1="True" attr2="string"/>
31+
<model1 attr1="true" attr2="string"/>
3232
'''
3333

3434
TestModel = GenericModel[bool, str]

tests/test_misc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class TestModel(BaseXmlModel, tag='model'):
7575
xml = '''
7676
<model>
7777
<submodel></submodel>
78-
<element1>None</element1>
78+
<element1></element1>
7979
</model>
8080
'''
8181

@@ -104,7 +104,7 @@ class TestModel(BaseXmlModel, tag='model'):
104104

105105
xml = '''
106106
<model>
107-
<submodel attr1="None">None<element1>None</element1></submodel>
107+
<submodel attr1=""><element1></element1></submodel>
108108
</model>
109109
'''
110110

0 commit comments

Comments
 (0)