Python Spyne, return with array

934 Views Asked by At

I'm working with spyne 2.12.14 in python 2.7 and django 1.9 I want to return a response like this:

?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

<soapenv:Header/>
    <soapenv:Body>
        <ns2:GetResponse xmlns:ns2="http://test.example.com/test">
            <return>
                <myHeader>
                    <id>1234abc</id>
                    <code>000</code>
                    <message>Success</message>
                </myHeader>
                <MyDetail>
                    <item1>myItem1</item1>
                    <item2>myItem2</item2>
                </MyDetail>
                <MyDetail>
                    <item1>myItem1</item1>
                    <item2>myItem2</item2>
                </MyDetail>
            </return>
        </ns2:GetResponse>
    </soapenv:Body>
</soapenv:Envelope>

but get this response:

?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

<soapenv:Header/>
    <soapenv:Body>
        <ns2:GetResponse xmlns:ns2="http://test.example.com/test">
            <return>
                <myHeader>
                    <id>1234abc</id>
                    <code>000</code>
                    <message>Success</message>
                </myHeader>
                <MyDetail>
                    <MyDetail>
                        <item1>myItem1</item1>
                        <item2>myItem2</item2>
                    </MyDetail>
                    <MyDetail>
                        <item1>myItem1</item1>
                        <item2>myItem2</item2>
                    </MyDetail>
                </MyDetail>
            </return>
        </ns2:GetResponse>
    </soapenv:Body>
</soapenv:Envelope>

This is my code:

class MyHeader(ComplexModel):
    __namespace__ = 'http://test.example.com/test'
    INHERITANCE = None,
    INDICATOR = Sequence,
    _type_info = {
        'id': String,
        'code': String,
        'message': String,
    }

class MyDetail(ComplexModel):
    __namespace__ = 'http://test.example.com/test'
    INHERITANCE = None,
    INDICATOR = Sequence,
    _type_info = {
        'item1': String,
        'item2': String,
    }

class GetResponse(ComplexModel):
    INHERITANCE = None,
    INDICATOR = Sequence,
    _type_info = {
        'myHeader': MyHeader,
        'MyDetail': Array(MyDetail, minOccurs=0, maxOccurs='unbounded')
    }

@rpc(MyObject, _returns=[GetResponse], _out_variable_names=["return"])

    def GetMiniStatement(ctx, MyObjectInfo):
            do_something

Can anyone help please?

2

There are 2 best solutions below

0
On BEST ANSWER

change my declaration to that 'MyDetail': Array(MyDetail, maxOccurs='unbounded', wrapped = False) and it works.

1
On

You want a non-wrapped array.

Remove this:

     'MyDetail': Array(MyDetail, minOccurs=0, maxOccurs='unbounded')

Add this:

     'MyDetail': MyDetail.customize(minOccurs=0, maxOccurs='unbounded')

As an aside, your Array definition seems odd because by setting maxOccurs of an Array type to a value greater than 1, you are creating a non-wrapped array of a wrapped array which doesn't seem like what you want. (in other words, you are telling Spyne that the Array type can occur more than once, not what's inside it.)