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

Attribute values are parsed as qnames #346

Closed
eriks5 opened this issue Dec 11, 2020 · 1 comment · Fixed by #347
Closed

Attribute values are parsed as qnames #346

eriks5 opened this issue Dec 11, 2020 · 1 comment · Fixed by #347
Labels
bug Something isn't working

Comments

@eriks5
Copy link

eriks5 commented Dec 11, 2020

If an attribute value looks like a qname, but isn't, serializing breaks.

Example:

from dataclasses import dataclass, field

from xsdata.formats.dataclass.serializers import XmlSerializer
from xsdata.formats.dataclass.serializers.config import SerializerConfig


@dataclass
class Element:
    attr: str = field(metadata=dict(type="Attribute", name="attr"))


def test(value):
    element = Element(attr=value)
    config = SerializerConfig(pretty_print=True)
    serializer = XmlSerializer(config=config)
    print(serializer.render(element))


if __name__ == '__main__':
    test('{{}}')

When running the above script, lxml throws a ValueError exception:

Traceback (most recent call last):
  File "scratch.py", line 20, in <module>
    test('{{}}')
  File "scratch.py", line 16, in test
    print(serializer.render(element))
  File "env38.win\lib\site-packages\xsdata\formats\dataclass\serializers\xml.py", line 55, in render
    self.write(output, obj, ns_map)
  File "env38.win\lib\site-packages\xsdata\formats\dataclass\serializers\xml.py", line 70, in write
    handler.write(events)
  File "env38.win\lib\site-packages\xsdata\formats\dataclass\serializers\writers\lxml.py", line 18, in write
    super().write(events)
  File "env38.win\lib\site-packages\xsdata\formats\dataclass\serializers\mixins.py", line 82, in write
    self.end_tag(*args)
  File "env38.win\lib\site-packages\xsdata\formats\dataclass\serializers\mixins.py", line 170, in end_tag
    self.flush_start(True)
  File "env38.win\lib\site-packages\xsdata\formats\dataclass\serializers\mixins.py", line 206, in flush_start
    self.handler.startElementNS(self.pending_tag, None, self.attrs)
  File "src\lxml\sax.py", line 111, in lxml.sax.ElementTreeContentHandler.startElementNS
  File "src\lxml\etree.pyx", line 3022, in lxml.etree.Element
  File "src\lxml\apihelpers.pxi", line 131, in lxml.etree._makeElement
  File "src\lxml\apihelpers.pxi", line 118, in lxml.etree._makeElement
  File "src\lxml\apihelpers.pxi", line 215, in lxml.etree._setNodeNamespaces
  File "src\lxml\apihelpers.pxi", line 1755, in lxml.etree._uriValidOrRaise
ValueError: Invalid namespace URI '{'

Tested with versions 20.11.1 and 20.12

@tefra tefra added the bug Something isn't working label Dec 11, 2020
tefra added a commit that referenced this issue Dec 11, 2020
tefra added a commit that referenced this issue Dec 11, 2020
@tefra
Copy link
Owner

tefra commented Dec 11, 2020

Thanks for reporting @eriks5 the fix is on master @ 8d77557

tefra added a commit that referenced this issue Jan 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants