spyne

get_xml_as_object function not working with polymorphism - spyne 2.13.12 alpha0


A spyne error is returned when loading polymorphic object in spyne 2.13.12 alpha0. Could you please have a look at the following example and indicate which function should be used to load an object from a xml file ?

import sys

from lxml import etree

from spyne.util import six
from spyne import ComplexModel, Unicode
from spyne.util.xml import get_object_as_xml_polymorphic, get_xml_as_object


class B(ComplexModel):
    _type_info = {
        '_b': Unicode,
    }

    def __init__(self):
        super().__init__()
        self._b = "b"


class C(B):
    _type_info = {
        '_c': Unicode,
    }

    def __init__(self):
        super().__init__()
        self._c = "c"


class A(ComplexModel):
    _type_info = {
        '_a': Unicode,
        '_b': B,
    }

    def __init__(self, b=None):
        super().__init__()
        self._a = 'a'
        self._b = b


a = A(b=C())
elt = get_object_as_xml_polymorphic(a, A, no_namespace=True)
xml_string = etree.tostring(elt, pretty_print=True)
if six.PY2:
    print(xml_string, end="")
else:
    sys.stdout.buffer.write(xml_string)

element_tree = etree.fromstring(xml_string)
new_a = get_xml_as_object(elt, A)

The error is at the last line and the message is

raise ValidationError(xsi_type)
spyne.error.ValidationError: ValidationError(Client.ValidationError: "The value 'C' could not be validated.")

Thank you for your help


Solution

  • Your code was used as test and example as part of PR #635 to fix the issue you were having. Please test with >=spyne-2.13.14 and report back.

    Thanks!