0

Is there a way to get the specific class tag names from my spyne class definitions?

Given the case, my class hierarchy looks like this:

# models.py
class Vehicle(ComplexModel):
    ...

class Car(Vehicle):
    ...

class Bike(Vehicle):
    ...

Then I followed the instructions in the answers of this questions, cause their problem seems similar, to get the polymorphism working.

how to implement abstract model in spyne

How do you @rpc _returns polymorphic types in spyne?

Important code lines which were changed:

# start.py
application=Application(
    ...
    in_protocol=Soap11(...),
    out_protocol=Soap11(polymorphic=True)
)

So far, so good. After i did this, i've got the following response:

<!-- Response.xml -->
...
<Vehicle xsi:type="Car">
    ...
</Vehicle>
...

So my question is:

Can i get rid of the general class name Vehicle as the tag name and get the name of the specific class e.g. Car as tag name instead?

Therefore the response should be something like this:

<Car>
    ...
</Car>

I've seen some approaches with the `ctx.out_string" and some text replacement in this question:

Remove the namespace from Spyne response variables

Which I think probably can work out fine for me, but if there is a "conventional" way in the API, which I did not find yet, I would prefer to get known of it to use it instead.

Community
  • 1
  • 1
0x00F
  • 50
  • 7
  • This is how the SOAP polymorphism was implemented. Do you think this is a bug? – Burak Arslan Jun 15 '16 at 04:12
  • @BurakArslan Ok i wasn't familiar with that. I'll do a bit of research on that. No. So what you are saying that there is no need for this kind of feature, cause xml interpreter like lxml should determine the `xsi:type` as the tagname when working with the xml. And ignore the general superclass as it is in the tagname, since the `xsi:type` is the specific type. – 0x00F Jun 17 '16 at 12:21
  • Now I see where you are coming from. Please see my answer. – Burak Arslan Jun 18 '16 at 12:57

1 Answers1

0

This is how XML polymorphism is supposed to work. libxml2's schema validator (that you use through lxml) should interpret it properly.

OTOH, If you are sure you want distinction solely through tag names, have a look at the <choice> tag: http://w3.org/TR/xmlschema-1/#declare-contentModel

Also see an article comparing the two approaches: http://ibm.com/developerworks/library/ws-tip-xsdchoice

You seem to have already figured out how to use polymorphism in Spyne. So here's how you use the <choice> tag: https://github.com/arskom/spyne/blob/57ef5c0db51cb194353c67d317990fe89bc4177d/spyne/test/interface/test_xml_schema.py#L51

Burak Arslan
  • 7,671
  • 2
  • 15
  • 24
  • Ok thanks. It's doing quite well, as you predicted it, but at the request execution i get this line repeatedly `DEBUG:spyne.protocol._base:Polymap miss cls switch: => `. After getting into the code of `spyne.protocol._base.ProtocolMixin` it seems that in my case, `Vehicle` has no information about an inheriting class `Car` in `Vehicle.polymap`. How to fix this or should this be no concern to me, cause there no lacking functionality, when the polymap is not set? – 0x00F Jul 07 '16 at 10:48
  • This is not an error, it's just informing you that polymorphic class switch was performed without a `poly_map` lookup. – Burak Arslan Jul 07 '16 at 10:55
  • Ok, but I assume that this polymap should hold in my case the two subclasses, so that a proper lookup can happen. Is this correct? If it is, am i missing some kind of annotation to get this whole thing working? – 0x00F Jul 07 '16 at 13:14
  • Am i right, that if the subclasses are in the polymap, that the tagnames will switch from general `Vehicle` to specific `Car`? Also I think if this is the case, you would already have told me that, but just want to be clear with my question. – 0x00F Jul 07 '16 at 13:19
  • Believe me when I tell you that your use case has nothing to do with `poly_map` :) If you want actual tags, you need to use the `` tag like I showed you. – Burak Arslan Jul 08 '16 at 13:51
  • Ok but I can't use choice tags cause it would distinguish from my given interface, it's using ``. Thanks anyway for responding so fast! – 0x00F Jul 14 '16 at 13:46