'add xsi:type in SOAP python spyne xml response

I am using example/complextype.py script provided in spyne official repo and I got the following reponse:

--------------- RESPONSE ------------------------
 <xml version='1.0' encoding='UTF-8'?>
<soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="spyne.examples.complex">
    <soap11env:Body>
        <tns:super_userResponse>
            <ns0:userid xmlns:ns0="user">0</ns0:userid>
            <ns0:username xmlns:ns0="user">root</ns0:username>
            <ns0:firstname xmlns:ns0="user">Super</ns0:firstname>
            <ns0:lastname xmlns:ns0="user">User</ns0:lastname>
         </tns:super_userResponse>
    </soap11env:Body>
</soap11env:Envelope>

what I am trying to unnderstand is how to add xsi:type attributes

<ns0:userid xmlns:ns0="user" xsi:type="integer" >0</ns0:userid>

class Permission(ComplexModel):
    __namespace__ = "permission"

    app = String(values=['library', 'delivery', 'accounting'])
    perms = String(min_occurs=1, max_occurs=2, values=['read', 'write'])


class User(ComplexModel):
    __namespace__ = "user"
    
    userid = Integer
    username = String
    firstname = String
    lastname = String

    

user_database[0] = User(
    userid=0,
    username='root',
    firstname='Super',
    lastname='User',
    permissions=all_permissions
)

   
class UserManager(Service):

    @rpc(_returns=User , _body_style='bare' )
    def super_user(ctx):
        return user_database[0]

  
if __name__ == '__main__':
    from wsgiref.simple_server import make_server


    application = Application([UserManager], 'spyne.examples.complex',
                              in_protocol=Soap11(), out_protocol=Soap11())

    server = make_server('127.0.0.1', 8000, WsgiApplication(application))


    server.serve_forever()

using the provided example, how can I fix it?



Solution 1:[1]

Spyne adds xsi:type only in case of ambiguity, which only happens in case of polymorphic types.

Eg. if the type in WSDL says Vehicle but you return a child class named Car AND you have polymorphism enabled in in the output protocol, you will get xsi:type="nsprefix:Car" opening tag of the Car instance.

If you want to override this behavior, you need to override to_parent function in a custom protocol and pass it as the output protocol to the Application instantiation.

PS: xsi:type tag is added when add_type == True here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1