kartagis / pysimplesoap

Automatically exported from code.google.com/p/pysimplesoap
0 stars 0 forks source link

Returning a list of dictionaries #30

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I want to return a list of dictionaries, and so I try the folowing function in 
web2py 1.95.1:

@service.soap('MyAdd',returns={'result':[{'a':str, 'b':int, 
'apotelesma':int}]},args={'a':int,'b':int,})
def add(a,b):
    s={'a':str(a),'b':b,'apotelesma':a+b}
    return [s,s]

By calling the function: 
client.MyAdd(a=1,b=2) returns:

{'result': [{'a': u'1'},
            {'apotelesma': 3},
            {'b': 2},
            {'a': u'1'},
            {'apotelesma': 3},
            {'b': 2}]}

What I would expect to see is:
{'result': [{'a': u'1','apotelesma': 3,'b': 2},
            {'a': u'1','apotelesma': 3,'b': 2}]} 

Is this a bug, or I am missing something ?

Original issue reported on code.google.com by kmo...@gmail.com on 29 May 2011 at 9:10

GoogleCodeExporter commented 8 years ago
The trace is the following:

POST http://localhost:8000/go/default/call/soap
SOAPAction: "http://localhost:8000/go/default/call/soapMyAdd"
Content-length: 347
Content-type: text/xml; charset="UTF-8"

<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.
xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:x
si="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
    <MyAdd xmlns="http://localhost:8000/go/default/call/soap">
    <a>1</a>2</MyAdd>
</soap:Body>
</soap:Envelope>

content-length: 435
expires: Sun, 29 May 2011 21:16:32 GMT
server: Rocket 1.2.2 Python/2.6.1
connection: close
pragma: no-cache
cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
date: Sun, 29 May 2011 21:16:32 GMT
content-type: text/xml
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.
xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:x
si="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><MyAddResponse xmlns="
http://localhost:8000/go/default/call/soap"><result><a>1</a><apotelesma>3<
/apotelesma>2<a>1</a><apotelesma>3</apotelesma>2</result></MyAddRe
sponse></soap:Body></soap:Envelope>

Original comment by kmo...@gmail.com on 29 May 2011 at 9:20

GoogleCodeExporter commented 8 years ago
I can't see the problem.

The result you are returining is what is at the xmlresponse, can you provide a 
full code example / test case?

Original comment by reingart@gmail.com on 18 Jun 2011 at 11:27

GoogleCodeExporter commented 8 years ago
As noted in my initial report, 'result' should be a list of 2 dictionaries each 
having 3 key-values. Instead, it returns a list of 6 dictionaries with 1 
key-value.

The 'MyAdd' function I provided, is a simple enhancement of the Add function 
found in the examples. Isn't the code in the initial report complete?

Original comment by kmo...@gmail.com on 19 Jun 2011 at 7:49

GoogleCodeExporter commented 8 years ago
I'm not sure if this is a compatibility issue or a design decision per SOAP 
specification, because "generic" arrays are not common in WSDL.

Most implementation define an intermediate type for the array elements (a 
python dictionary for us).

To archive similar results, you can try:

{{{
@service.soap('MyAdd',returns={'result':[{'ret': {'a':str, 'b':int, 
'apotelesma':int}}]},args={'a':int,'b':int,})
def add(a,b):
    s={'ret': {'a':str(a),'b':b,'apotelesma':a+b}}
    return [s,s]

def test():
    from pysimplesoap.client import SoapClient
    client = SoapClient(wsdl="http://127.0.0.1:8000/welcome/default/call/soap?WSDL",trace=False)

    r = client.MyAdd(a=1,b=2)
    return {'r': r}
    #return client.xml_response
}}}

Here, ret is the array element type, this will return a list with two elements 
correctly:

{{{
result =  [{'ret': {'a': u'1', 'apotelesma': 3, 'b': 2}}, {'ret': {'a': u'1', 
'apotelesma': 3, 'b': 2}}]
}}}

Original comment by reingart@gmail.com on 27 Jun 2011 at 11:10