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

XSD Enumeration decodes but does not re-encode due to namespaces #411

Open
GSam opened this issue Aug 8, 2024 · 3 comments
Open

XSD Enumeration decodes but does not re-encode due to namespaces #411

GSam opened this issue Aug 8, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@GSam
Copy link

GSam commented Aug 8, 2024

Version: xmlschema 3.3.1

I encountered an issue where I am unable to encode an XSD enumeration which utilizes namespaces (which had just been decoded). The code complains that s:Sender must belong to a set of values, but this set of values includes {http://www.w3.org/2003/05/soap-envelope}Sender. The xml as shown by the error has the namespace xmlns:s="http://www.w3.org/2003/05/soap-envelope". As a side-note the .xsd schema file denotes this namespace by tns rather than s.

  File "/home/garming/.local/lib/python3.10/site-packages/xmlschema/validators/groups.py", line 1198, in iter_encode
    for result in xsd_element.iter_encode(value, validation, **kwargs):
  File "/home/garming/.local/lib/python3.10/site-packages/xmlschema/validators/elements.py", line 1080, in iter_encode
    yield self.validation_error(validation, e, elem, **kwargs)
  File "/home/garming/.local/lib/python3.10/site-packages/xmlschema/validators/xsdbase.py", line 249, in validation_error
    raise error
  File "/home/garming/.local/lib/python3.10/site-packages/xmlschema/validators/simple_types.py", line 1475, in iter_encode
    validator(obj)
  File "/home/garming/.local/lib/python3.10/site-packages/xmlschema/validators/facets.py", line 633, in __call__
    raise XMLSchemaValidationError(self, value, reason)
xmlschema.validators.exceptions.XMLSchemaValidationError: failed validating 's:Sender' with XsdEnumerationFacets(['{http://www.w3.org/2003/05/soap-envelope}DataEncodingUnknown', '{http://www.w3.org/2003/05/soap-envelope}MustUnderstand', '{http://www.w3.org/2003/05/soap-envelope}Receiver', '{http://www.w3.org/2003/05/soap-envelope}Sender', '{http://www.w3.org/2003/05/soap-envelope}VersionMismatch']):

Reason: value must be one of ['{http://www.w3.org/2003/05/soap-envelope}DataEncodingUnknown', '{http://www.w3.org/2003/05/soap-envelope}MustUnderstand', '{http://www.w3.org/2003/05/soap-envelope}Receiver', '{http://www.w3.org/2003/05/soap-envelope}Sender', '{http://www.w3.org/2003/05/soap-envelope}VersionMismatch']

Schema component:

  <xs:enumeration xmlns:xs="http://www.w3.org/2001/XMLSchema" value="tns:DataEncodingUnknown" />

Instance type: <class 'xml.etree.ElementTree.Element'>

Instance:

  <s:Value xmlns:s="http://www.w3.org/2003/05/soap-envelope">s:Sender</s:Value>
@brunato
Copy link
Member

brunato commented Aug 14, 2024

Hi,
the XSD enumeration has a xs:QName as base type, so prefixed values have to be converted to expanded form (the prefix can change ...).

The fix for this is to provide the expanded form to facets validators in case of type derived from xs:QName or xs:NOTATION also during the encode phase:

if self.validators:
    if self.root_type.name in (XSD_QNAME, XSD_NOTATION):
        value = get_extended_qname(obj, kwargs.get('namespaces'))
    else:
        value = obj

    for validator in self.validators:
        try:
            validator(value)
        except XMLSchemaValidationError as err:
            yield err

that fix will be included in the next release of the package.

thank you

@GSam
Copy link
Author

GSam commented Aug 20, 2024

Cool, thanks for that!

brunato added a commit that referenced this issue Sep 10, 2024
  - Add occurs check methods to ModelVisitor
@brunato
Copy link
Member

brunato commented Sep 19, 2024

Hi, release v3.4.2 with the latest release of elementpath should resolve the encoding of QNames (by improving the decode of XSD datatypes and taking the namespace information from the element nodes).

@brunato brunato added the bug Something isn't working label Sep 19, 2024
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

No branches or pull requests

2 participants