# Problem w/ActionWebService and Inheritance in Service Params

**URL:** <https://www.ruby-forum.com/t/problem-w-actionwebservice-and-inheritance-in-service-params/161449>\
**Category:** Rails\
**Created:** [February 26, 2009, 3:50pm UTC](https://www.ruby-forum.com/t/problem-w-actionwebservice-and-inheritance-in-service-params/161449 "2009-02-26T15:50:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [February 26, 2009, 3:50pm UTC](https://www.ruby-forum.com/t/problem-w-actionwebservice-and-inheritance-in-service-params/161449/1 "2009-02-26T15:50:08Z")

</div>

I have a service method that takes a PaymentMethod which can either be a  
CreditCard or a PayPal account. When I request the service and pass one  
or the other it always comes over as a PaymentMethod; the service  
doesn’t seem to know that the parameter is really a CreditCard or  
PayPal. Here’s some sample code that illustrates what I’m talking about  
more concretely:

class PaymentsApi \< ActionWebService::API::Base  
api\_method(  
:create\_payment,  
:expects =\> [  
{ :payment\_method =\> PaymentMethod }  
],  
:returns =\> [:payment\_id =\> :string]  
)  
end

class PaymentMethod \< ActionWebService::Struct

# …

end

class CreditCard \< PaymentMethod

# …

end

class PayPal \< PaymentMethod

# …

end

I see in the request XML that it knows that the payment\_method is really  
a CreditCard or Invoice (note the xsi:type).

\<env:Envelope xmlns:xsd=“[http://www.w3.org/2001/XMLSchema](http://www.w3.org/2001/XMLSchema)”  
xmlns:env=“[http://schemas.xmlsoap.org/soap/envelope/](http://schemas.xmlsoap.org/soap/envelope/)”  
xmlns:xsi=“[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)”\>  
env:Body  
\<n1:CreatePayment xmlns:n1=“urn:ActionWebService”  
env:encodingStyle=“[http://schemas.xmlsoap.org/soap/encoding/](http://schemas.xmlsoap.org/soap/encoding/)”\>  
\<payment\_method  
xmlns:n2=“[http://www.ruby-lang.org/xmlns/ruby/type/custom](http://www.ruby-lang.org/xmlns/ruby/type/custom)”  
xsi:type=“n2:CreditCard”\>  
…  
\</payment\_method\>  
\</n1:CreatePayment\>  
\</env:Body\>  
\</env:Envelope\>

However, when I attempt to switch in the controller based on the type of  
the payment\_method it doesn’t seem to remember that it knows this. Is  
it not possible to use this type of polymorphism in service parameters  
or am I doing something wrong?

Any help would be much appreciated.

Thanks in advance,  
Jason

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [February 26, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/problem-w-actionwebservice-and-inheritance-in-service-params/161449/2 "2009-02-26T18:17:44Z")

</div>

Jason F. wrote:

> I have a service method that takes a PaymentMethod which can either be a  
> CreditCard or a PayPal account. When I request the service and pass one  
> or the other it always comes over as a PaymentMethod; the service  
> doesn’t seem to know that the parameter is really a CreditCard or  
> PayPal.

I tracked down the issue to the cast\_to\_structured\_type method of  
casting.rb. Essentially, a condition needs to be added that would  
effectively cause the code to not attempt to “cast” the parameter if it  
is derived from the type specified in the API declaration. Here’s a  
one-line modification:

# lib/action\_web\_service/castings.rb - line 120

obj = value if canonical\_type(value.class) ==  
canonical\_type(signature\_type.type)

And here’s how it would read after the patch along with the next  
unmodified line (121) for reference:

obj = value if canonical\_type(value.class) ==  
canonical\_type(signature\_type.type) or  
derived\_from?(signature\_type.type, value.class)  
obj ||= signature\_type.type\_class.new

I am going to look into submitting a patch. Can anyone think of why this  
shouldn’t work this way? I am also asking the priest who resurrected  
this code from the dead.

> **[DATANOISE.COM](https://www.datanoise.com/)**
>
> DATANOISE.COM - Contact us for any business inquiries

Jason
