How to pass NULL value for integer/fixnum type in ruby for SOAP request - ruby

I need to invoke the method getquotedata() through a SOAP request using the parameters:
username, password, id, revnum
revnum is the revision number of the data we are fetching. It can have values like ... 1,2,3... and so on. If we pass a null value to it, we get the latest data.
How can I initialize an Integer with null value in ruby for the method call? The method call does not work if we pass nil to revnum or even if we do not send this parameter. Below is our sample method call:
getQuoteData(:username => user, :password=> pass, id => "XKS32", revnum=> 0)
The WSDL definition of the method is:
<s:element name="GetQuoteData">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="username" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="password" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="cartCompositeNumber" type="s:string" />
<s:element minOccurs="1" maxOccurs="1" name="revNum" nillable="true" type="s:int" />
</s:sequence>
</s:complexType>
</s:element>
Thanks for any suggestions

Related

Magento: getSingleton('customer/session') doesn't return information when called via SOAP

I am currently trying to get back customer data after submitting an order and capturing the checkout_submit_all_after event with an Observer. If I make the order via the frontend, Mage::getSingleton('customer/session') and Mage::getSingleton('checkout/session') ran inside the observer give me a wealth of information, however when I make an order via SOAP, these methods returns nothing. I also tried Mage::helper('customer')->getCustomer() but it didn't return anything either.
Is there another way I can obtain the data of the last submitted order? Specifically I need the customer from the session and the last order id ala Mage::getSingleton('checkout/session')->getLastRealOrderId().
Check out this nonsense: the quote->customer_email field is originally populated with the email, then after setting the addresses with the SOAP method 'shoppingCartCustomerAddresses', _prepareGuestQuote from Checkout/Model/Api/Resource/Customer.php OVERWRITES this with "$quote->getBillingAddress()->getEmail()). Which has to be blank! Why? Well check out the wsdl definition of the address object:
<complexType name="shoppingCartCustomerAddressEntity">
<all>
<element name="mode" type="xsd:string" minOccurs="0"/>
<element name="address_id" type="xsd:string" minOccurs="0"/>
<element name="firstname" type="xsd:string" minOccurs="0"/>
<element name="lastname" type="xsd:string" minOccurs="0"/>
<element name="company" type="xsd:string" minOccurs="0"/>
<element name="street" type="xsd:string" minOccurs="0"/>
<element name="city" type="xsd:string" minOccurs="0"/>
<element name="region" type="xsd:string" minOccurs="0"/>
<element name="region_id" type="xsd:string" minOccurs="0"/>
<element name="postcode" type="xsd:string" minOccurs="0"/>
<element name="country_id" type="xsd:string" minOccurs="0"/>
<element name="telephone" type="xsd:string" minOccurs="0"/>
<element name="fax" type="xsd:string" minOccurs="0"/>
<element name="is_default_billing" type="xsd:int" minOccurs="0"/>
<element name="is_default_shipping" type="xsd:int" minOccurs="0"/>
</all>
</complexType>
Do you see an 'email' field in there? I sure don't!
So now I have to override the thing just to make it FUNCTIONAL. Seriously Magento, wtf?

How to add xmlns="MyNamespace" to AuthSOAPHeader in gSOAP

I'm building a client application to access a web service from a 3rd party server.
From this server WSDL:
<s:element name="AuthSOAPHeader" type="tns:AuthSOAPHeader" />
<s:complexType name="AuthSOAPHeader">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="User" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string" />
</s:sequence>
<s:anyAttribute />
</s:complexType>
And these commands:
wsdl2h -k -o svc.h .\svc.asmx.xml
soapcpp2 -i -C .\svc.h
My gSOAP-based client application is generating this XML:
<SOAP-ENV:Header>
<ns1:AuthSOAPHeader>
<User>user</User>
<Password>password</Password>
</ns1:AuthSOAPHeader>
</SOAP-ENV:Header>
But my server only responds to:
<SOAP-ENV:Header>
<ns1:AuthSOAPHeader xmlns="Server.WebServices">
<User>user</User>
<Password>password</Password>
</ns1:AuthSOAPHeader>
</SOAP-ENV:Header>
I'm struggling to figure out a way to add the xmlns="Server.WebServices" attribute to the AuthSOAPHeader tag. Is there a way to do this in gSOAP? Thank you in advance!
I finally fixed this by adding this flag to the soap initialization:
SOAP_XML_DEFAULTNS

XSD key not in root element and complex keys

I searched for this, but can't find solution to my problem, so please don't trash this question.
We have a service with very complex XML configuration which is described by XSD. There are many rules that must be obeyed for things to run smoothly. XSD describes structure of configuration, but not the rules, which we must do now. After creating some global rules we have to do some more complex now and we encountered a problem. It seams like validator only uses key/keyref/unique from root element. I've created small XSD and XML file to illustrate that:
XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://test.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://test.org/XMLSchema.xsd"
xmlns:t="http://test.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Orders" type="t:OrdersList">
<xs:key name="OrderNo">
<xs:selector xpath="./t:Order" />
<xs:field xpath="#Number" />
</xs:key>
</xs:element>
<xs:complexType name="OrdersList">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Order" nillable="false" type="t:Order" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Order">
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="Lines" nillable="false" type="t:OrdersLinesList" />
</xs:sequence>
<xs:attribute name="Number" use="optional" type="xs:string" />
<xs:attribute name="ClientId" use="optional" type="xs:int" />
</xs:complexType>
<xs:complexType name="OrdersLinesList">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Line" nillable="false" type="t:OrderLine">
<!-- THE PROBLEM -->
<xs:key name="LineNoKey">
<xs:selector xpath="./t:Line" />
<xs:field xpath="#LineNumber" />
</xs:key>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="OrderLine">
<xs:attribute name="LineNumber" use="optional" type="xs:string" />
<xs:attribute name="ProductId" use="optional" type="xs:int" />
<xs:attribute name="Amount" use="optional" type="xs:decimal" />
</xs:complexType>
</xs:schema>
XML:
<?xml version="1.0" encoding="utf-8"?>
<Orders xmlns="http://test.org/XMLSchema.xsd">
<Order Number="0001/5/13" ClientId="123">
<Lines>
<Line LineNumber="1" ProductId="123" Amount="4" />
<Line LineNumber="2" ProductId="124" Amount="4" />
</Lines>
</Order>
<Order Number="0002/5/13" ClientId="123">
<Lines>
<Line LineNumber="1" ProductId="123" Amount="4" />
<!-- Duplicate number - it DOES validate as expected. -->
<Line LineNumber="1" ProductId="124" Amount="4" />
</Lines>
</Order>
<!-- Duplicate number - it doesn't validate as expected. -->
<Order Number="0002/5/13" ClientId="123">
<Lines>
<Line LineNumber="1" ProductId="123" Amount="4" />
<Line LineNumber="2" ProductId="124" Amount="4" />
</Lines>
</Order>
</Orders>
I have a few questions which I can't find any sensible answer:
How to solve the above. LineNumber must be unique, but only within Order/Lines.
Is it possible to allow keyref to contain non existing virtual value (our service uses some predefined... let say things. Custom ones are defined in XML to allow extending functionality, but in most of the cases, users will use predefined ones)
Is there any way to determine if list of elements within a tag (something like Lines above) has at least one tag with attribute set to certain value. Good example would be to describe it like a list of tables with unique names, that has list of columns with unique names and at least one of the columns is a primary key.
This is the corrected constraint for Lines/Line:
<xs:complexType name="Order">
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="Lines" nillable="false" type="t:OrdersLinesList">
<!-- THE PROBLEM SOLVED -->
<xs:key name="LineNoKey">
<xs:selector xpath="t:Line"/>
<xs:field xpath="#LineNumber"/>
</xs:key>
</xs:element>
</xs:sequence>
<xs:attribute name="Number" use="optional" type="xs:string"/>
<xs:attribute name="ClientId" use="optional" type="xs:int"/>
</xs:complexType>
If always helps if you can visualize your constraints, to understand the scope they act upon:
The correct diagram:
vs. the original one:
You can see that the selector rooted in the element Line is looking for yet another Line (./t:Line); and even if you fix the selector, it'll always match at most one attribute. The idea of a key is that the selector should match a set of nodes among which the field must be present and unique.
The above should take care of 1.
No.
Not based on an attribute. To stick with your parallel, for XSD 1.0 I would probably enforce an element called PrimaryKey, which should contain one or more references to names of the other columns; as if instead of using the PRIMARY KEY constraint clause in a column definition, I would enforce the use of the same at the table level.

Consuming Web Services in Ruby that use reserved words as field names

I'm attempting to consume a WebService where one of the complex types in the WSDL has a field name that uses a Ruby reserved word. When I call a method returning one of these complex types and attempt to access the data, I get an error.
How can I consume this web service that uses a reserved word in Ruby?
The relevant section of the WSDL is:
<s:complexType name="someObject">
<s:sequence>
<s:element minOccurs="0" name="field1" type="s:string"/>
<s:element minOccurs="0" name="field2" type="s:boolean"/>
<s:element minOccurs="0" name="field3" type="s:string"/>
<s:element minOccurs="0" name="class" type="s:string"/>
<s:element minOccurs="0" name="field4" type="s:string"/>
</s:sequence>
<s:attribute form="unqualified" name="type" type="s:string"/>
</s:complexType>
Here is the XML coming back from my method call:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<MethodCallResponse>
<MethodCallResult>
<results>
<object type="someObject">
<field1>Whatever</field1>
<field2>true</field2>
<field3>TCP</field3>
<class>CLIENT</class>
</object>
</results>
</MethodCallResult>
</MethodCallResponse>
</soap:Body>
</soap:Envelope>
The error:
undefined method `class_eval' for "CLIENT":String
I am using ruby 1.8.7-p302.
You're probably looking for something like the BlankState method. The idea is that you create an encapsulating class for your data that undefines some the methods that are colliding with the poorly named types.

Why won't this Schema validate this XML file?

The XML file:
<Lista count="3">
<Pelicula nombre="Jurasic Park 3">
<Genero>Drama</Genero>
<Director sexo="M">Esteven Spielberg</Director>
<Temporada>
<Anho>2002</Anho>
<Semestre>Verano</Semestre>
</Temporada>
</Pelicula>
<Pelicula nombre="Maldiciones">
<Genero>Ficcion</Genero>
<Director sexo="M">Pedro Almodovar</Director>
<Temporada>
<Anho>2002</Anho>
<Semestre>Verano</Semestre>
</Temporada>
</Pelicula>
<Pelicula nombre="Amor en New York">
<Genero>Romance</Genero>
<Director sexo="F">Katia Hertz</Director>
<Temporada>
<Anho>2002</Anho>
<Semestre>Verano</Semestre>
</Temporada>
</Pelicula>
</Lista>
And here's the XML Schema file I made, it's not working. :\
<xsd:complexType name="Lista">
<xsd:attribute name="count" type="xsd:integer" />
<xsd:complexContent>
<xsd:element name="Pelicula" type="xsd:string">
<xsd:attribute name="nombre" type="xsd:string" />
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Genero" type="generoType"/>
<xsd:element name="Director" type="directorType">
<xsd:attribute name="sexo" type="sexoType"/>
</xsd:element>
</xsd:element name="Temporada">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Anho" type="anhoType" />
<xsd:element name="Semestre" type="semestreType" />
</xsd:sequence>
</xsd:complexType>
<xsd:element></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:complexContent>
</xsd:complexType>
<xsd:simpleType name="sexoType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="F"/>
<xsd:enumeration value="M"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="directorType">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="generoType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Drama"/>
<xsd:enumeration value="Accion"/>
<xsd:enumeration value="Romance"/>
<xsd:enumeration value="Ficcion"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="semestreType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Verano"/>
<xsd:enumeration value="Invierno"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="anhoType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="1970"/>
<xsd:maxInclusive value="2020"/>
</xsd:restriction>
</xsd:simpleType>
Try declaring and using your types separately. This makes the XSD a bit longer, but less nested and more readable (and more reusable, too):
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- document element -->
<xs:element name="Lista" type="listaType" />
<!-- type definitions -->
<xs:complexType name="listaType">
<xs:sequence>
<xs:element name="Pelicula" type="peliculaType" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="count" type="countType" />
</xs:complexType>
<xs:complexType name="peliculaType">
<xs:all>
<xs:element name="Genero" type="generoType" />
<xs:element name="Director" type="directorType" />
<xs:element name="Temporada" type="temporadaType" />
</xs:all>
<xs:attribute name="nombre" type="xs:string" />
</xs:complexType>
<xs:complexType name="directorType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="sexo" type="sexoType" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="temporadaType">
<xs:all>
<xs:element name="Anho" type="anhoType" />
<xs:element name="Semestre" type="semestreType" />
</xs:all>
</xs:complexType>
<xs:simpleType name="sexoType">
<xs:restriction base="xs:string">
<xs:enumeration value="F" />
<xs:enumeration value="M" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="generoType">
<xs:restriction base="xs:string">
<xs:enumeration value="Drama" />
<xs:enumeration value="Accion" />
<xs:enumeration value="Romance" />
<xs:enumeration value="Ficcion" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="semestreType">
<xs:restriction base="xs:string">
<xs:enumeration value="Verano" />
<xs:enumeration value="Invierno" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="anhoType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="1970" />
<xs:maxInclusive value="2020" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="countType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
The above validates your input XML.
Note that:
The convention for the XML Schema namespace prefix is xs (AFAIK - you can still go on and use xsd if you prefer).
The count attribute is really unnecessary, since the count can a) easily be calculated and b) there is a risk of something going wrong when the count attribute value and the actual count differ for some reason. Things that are derivable from the data should never be part of the data.
Oh, and to answer your initial question (why does mine not work):
You never declare an actual document element ("Lista"), you just declare its type. Compare with my solution.
In the complexType name="Lista":
attribute cannot be the first child of a complex type. Attributes must be declared after everything else.
complexContent cannot contain element.
in fact, you don't need a complexContent at all - just use a sequence instead.
In element name="Pelicula":
The type attribute is illegal when you declare a complexType within.
In the complexType for "Pelicula":
Again, attributes last.
Don't use a sequence unless you want to make any other order of children illegal. In this type of document I would guess child order is irrelevant.
In element name="Director":
You can't declare any attributes since when you already declared a type. Include the sexo attribute in the directorType
In simpleType name="directorType":
This should in reality be a complexType containing simpleContent with an extension. This way you can include the sexo attribute
Your XSD isn't even well formed XML.
Your XML iswas not well-formed, either. I have fixed it to be able to test in the first place.
P.S.: There is enough XSD documentation freely available to fix many of the basic problems you've had. There are XSD validators on the Net that help you by telling you what constructs are illegal. Knowing everything is absolutely not necessary, but a little reading + trial and error would have helped. ;-)
For a start:
<Semestre>Verano<Semestre>
...doesn't look well-formed.

Resources