Search code examples
pythonxmlsoapspyne

How do I keep Spyne from wrapping my response?


Here is the sample service

NS = 'my_app_namespace'

class MyMethodRS(ComplexModel):
    __tns__ = NS
    Version = XmlAttribute(Unicode)

class MyService(ServiceBase):
    __tns__ = NS

    @srpc(Unicode, _returns=MyMethodRS, _out_variable_name='MyMethodRS')
    def my_method(foo):
        response = MyMethodRS()
        response.Version = '1.0'
        return response

# and then application will be created and starged as wsgi app

Then I post a request

<?xml version='1.0' encoding='UTF-8' ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
  <soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <my_method xmlns="my_app_namespace">
      <foo>bar</foo>
    <my_method>
  </soap:Body>
</soap:Envelope>

And I got

<?xml version='1.0' encoding='UTF-8' ?>
<senv:Envelope>
  <senv:Body>
    <tns:my_methodResponse>
      <tns:MyMethodRS Version="1.0" />
    </tns:my_methodResponse>
  </senv:Body>
</senv:Envelope>

I don't want response to be wrapped into my_methodResponse. I tried to add _body_style='bare' to @srpc and got the fail

<?xml version='1.0' encoding='UTF-8' ?>
<senv:Envelope>
  <senv:Body>
    <senv:Fault>
      <faultcode>senv:Client.SchemaValidationError</faultcode>
      <faultstring><string>:14:0:ERROR:SCHEMASV:SCHEMAV_CVC_TYPE_3_1_2: Element '{http://www.opentravel.org/OTA/2003/05}my_method': Element content is not allowed, because the type definition is simple.</faultstring>
      <faultactor />
    </senv:Fault>
  </senv:Body>
</senv:Envelope>

How should I deal with it? I am trying to implement strict API and cannot add any wrappers to my responses.


Solution

  • Judging from this request:

    <my_method xmlns="my_app_namespace">
      <foo>bar</foo>
    <my_method>
    

    You need this:

    class MyMethodRequest(ComplexModel):
        foo = Unicode
    
    class MyService(ServiceBase):
        @srpc(MyMethodRequest, _returns=MyMethodRS, _body_style='bare' 
                               _out_variable_name='MyMethodRS')
        def my_method(request):
            foo = request.foo
            response = MyMethodRS()
            response.Version = '1.0'
            return response
    

    Which is very close to what the non-bare mode actually does.