Modifying Wsdl2Apex Generated Classes to Handle xs:extension and imported namespaces - wsdl

I am having trouble with wsdl2apex code generation, mainly due to the use of xs:import namespace and xs:extension in my WSDL.
In particular, I am seeing the error System.CalloutException: Web service callout failed: Unable to parse callout response. Apex type not found for element . . .. The raw SOAP response returned by the web service looks as I would expect.
I would like to modify the generated Apex classes to work around this issue, as server-side changes to the web service is not an option.
The SOAP response looks like the below:
<ns:getAccountsResponse>
<ns:return xsi:type="ax1:AccountReturn">
<ax2:successful>true</ax2:successful>
<ax2:transactionId>1000</ax2:transactionId>
<ax1:Accounts xsi:type="ax1:Account">
And the WSDL looks like this for the ax1 target namespace:
<xs:complexType name="AccountReturn">
<xs:complexContent>
<xs:extension base="ax100:BaseReturnObject">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="Accounts" nillable="true" type="ax1:Account"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
And for the ax2 target namespace:
<xs:complexType name="BaseReturnObject">
<xs:sequence>
<xs:element minOccurs="0" name="successful" type="xs:boolean"/>
<xs:element minOccurs="0" name="transactionId" nillable="true" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
The generated Apex Class which maps to AccountReturn only contains the fields for Accounts in ax1, but not the BaseReturnObject fields in ax2, since the wsdl2apex generation does not respect the use of xs:extension or the importing of namespaces (based on my research).
Is there anyway to modify the Apex Class to make this work? I tried adding the fields from BaseReturnObject to AccountReturn, and modifying field_order_type_info. However, it appears that apex_schema_type_info can only point to one namespace, and this may be the reason that the parsing of the callout response is still failing.

I've built a tool for automating the creation of the Apex classes. It includes support for xs:extension and xs:import (among other things).
In the case of an ex:extension, the tool will pull the required fields from the base class into the subclass and correctly configure the _type_info members.
You can get it for free - FuseIT SFDC Explorer. Currently in only runs directly in Windows. I've had reports of people running it successfully using Wine. (Disclosure: I work for the company that releases this tool).
Incidentally, the salesforce.stackexchange.com site is a great place to ask Salesforce specific questions.

Related

Is this wsdl fragment illegal?

I'm scratching my head. This WSDL is generated by CXF. It liked the input WSDL and schema files but SOAPUI is complaining when I try to create a test project. The whole wsdl is too long but here is the part that matches up with the error messages:
<xs:complexType name="RelatedResourcesType">
<xs:sequence>
<xs:element minOccurs="0" name="RelatedResource">
<xs:complexType>
<xs:complexContent>
<xs:extension base="tns:CompoundResourceIdentifierType">
<xs:sequence>
<xs:element minOccurs="0" name="link">
<xs:complexType>
<xs:sequence/>
<xs:attribute ref="ns3:type" xmlns:ns3="http://www.w3.org/1999/xlink"/>
<xs:attribute ref="ns4:href" use="required" xmlns:ns4="http://www.w3.org/1999/xlink"/>
<xs:attribute ref="ns5:role" xmlns:ns5="http://www.w3.org/1999/xlink"/>
<xs:attribute ref="ns6:title" xmlns:ns6="http://www.w3.org/1999/xlink"/>
<xs:attribute ref="ns7:label" xmlns:ns7="http://www.w3.org/1999/xlink"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
...
...
Error: Could not find attribute 'type#http://www.w3.org/1999/xlink' Do you mean to refer to the attribute named type#http://dws.sec?
Error: attribute 'href#http://www.w3.org/1999/xlink' not found
Error: Could not find attribute 'role#http://www.w3.org/1999/xlink' Do you mean to refer to the attribute named role#http://www.w3.org/2003/05/soap-envelope?
Error: Could not find attribute 'title#http://www.w3.org/1999/xlink' Do you mean to refer to the attribute named title#http://metadata.esd.com/?
Error: Could not find attribute 'label#http://www.w3.org/1999/xlink' Do you mean to refer to the attribute named title#http://metadata.esd.com/?
So these should all be coming from the xlink definition and I'm not sure why it's not seeing it. Any thoughts? or suggestions for what to try next?
I had success making changes that allowed Visual Studio 2008 to retrieve the WSDL from the server and to generate code. The steps were
1) Configure CXF to display the original WSDL files instead of its generated version
2) Change imported schemas to be inline in the WSDL until Visual Studio is happy
Details:
1) In the CXF configuration file add the wsdlLocation attribute
<jaxws:endpoint xmlnse="urn:discovery.services.sec.com"
id="DiscoveryImpl"
endpointName="e:discovery"
serviceName="e:DiscoveryService"
address="discovery"
implementor=com.sec.services.discovery.DiscoveryImpl"
wsdlLocation="WEB-INF\discovery.wsdl
>
There are several ways to specify the WSDL location. Google Code Search was helpful.
2) The original WSDL started off
...
Removing the import above, the result is
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions ...>
<wsdl:types>
<xsd:schema ..> <!-- Discovery.xsd contents here -->
...
</xsd:schema>
</xsd:types>
<xsd:schema ..>
<!-- import of Discovery.xsd removed from here -->
...
</xsd:schema>
</xsd:types>
<wsdl:message ...>
I hope this saves some else a little bit of time.

custom binding to force Inner class creation or getters/setters for xs:element

I have a schema declaration as follow from a third-party provider.
<xs:complexType name="GroupParameterType">
<xs:sequence minOccurs="0" maxOccurs="4">
<xs:element name="name" type="xs:string">
<xs:annotation>
<xs:documentation>The name of the parameter.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="value" type="xs:string">
<xs:annotation>
<xs:documentation>The value of the parameter.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
Above is the schema that i CANNOT change. I am trying to write a custom binding for jaxb 2.0 such that I can refer to name as GroupParameterType.Name or GroupParameterType.Value in java code.
Current default binding generates List for me i.e. getNameandValueList, but I want separate getters and setters for name and value respectively.
I tried putting in a custom binding like the following :
<jxb:bindings schemaLocation="GroupParameter.xsd" node="xs:element[#name='name']">
<jxb:globalBindings localScoping="toplevel" generateIsSetMethod="true"/>
</jxb:bindings>
<jxb:bindings schemaLocation="GroupParameter.xsd" node="xs:element[#name='value']">
<jxb:globalBindings localScoping="toplevel" generateIsSetMethod="true"/>
</jxb:bindings>
and it did nothing to change the default class generation. Can anyone give me some pointers as so what else can I try next ? I am looking to have the default List generation ALONG WITH the getters/setters for name and value OR have name and value as Inner Classes. If i remove the maxOccurs=4 option, I can generate getters/setters but since I can't modify the schema, I am trying to get that behavior using external binding file.
Thanks
Shon
You can't get this behaviour unless you modify your schema. You do have a schema model which maps to a heterogeneous element property, and you can't change it with a customization.
You can try the code injection plugin as the last retreat.

svcutil generated unneccesary wrapper classes

I am working on a project that uses the contract first approach. I was given a WSDL and three xsd's. When I use svcutil it generates a wrapper around the response class like so:
public partial class getDataByIdResponse1 {
public getDataByIdResponse getDataByIdResponse;
public getDataByIdResponse1() {
}
public getDataByIdResponse1(getDataByIdResponse getDataByIdResponse) {
this.getDataByIdResponse = getDataByIdResponse;
}
}
The getDataByIdResponse is wrapped inside a getDataByIdResponse1 object. This is done by svcutil and I have no idea why. The getDataByIdResponse1 object does not exist in the WSDL:
<wsdl:message name="getDataById">
<wsdl:part name="response" element="tns:getDataByIdResponse"/>
</wsdl:message>
<xs:element name="getDataByIdResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="data" type="sbc:DataType" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
Why is the type getDataByIdResponse wrapped in getDataByIdResponse1? Is there a switch for svcutil I should have used?
I'm in the same boat as you but I don't just want to live with it. I want to generate clean (unwrapped) contracts. If the wsdl and xsd's were given to you then there are some rules that your schema and wsdl need to follow in order for svc util to generate unwrapped code. These links helped me understand the issue a little better
http://pzf.fremantle.org/2007/05/handlign.html
http://mharbauer.wordpress.com/2007/10/19/wcf-datacontract-serializer-and-documentwrapped/
For now my schema and wsdl are small enough that I can tweak them to adhere to this rules.
However, like Ron, I've also been in situations where the easiest thing is just to live with it.
Hope this helps.
I am in the same situation (contract-first) and svcutil is generating this same kind of code for me but I just closed my eyes took a deep breath and accepted it :-)
Just use the types without the numeric postfix and it just works.

WSClient soap client in grails/groovy

I have a few problems with the cxf + WSClient in soap.
I am writing a small tool in grails that must make SOAP calls to an existing service.
WSClient (groovyws-0.5.3-20100521.062225-1.jar) seemed like the perfect solution to my problem.
However when i tried to implement a call to one of the actions i get two problems.
Problem #1 NullPointerException when making a call to some of the actions.
I have tracked down the code that is throwing the exception inside of
AbstractCXFWSClient.invokeMethod(String methodName, Object args)
if (!operationToBeInvoked.isUnwrapped()){
//Operation uses document literal wrapped style.
inputMessageInfo = operationToBeInvoked.getWrappedOperation().getInput();
} else {
inputMessageInfo = operationToBeInvoked.getUnwrappedOperation().getInput();
}
specifically operationToBeInvoked.getWrappedOperation() I have isolated the code in a unit test and find that both operationToBeInvoked.getWrappedOperation() and operationToBeInvoked.getWrappedOperation() result in null objects. I have tried to figure out what is causing it however i think i have gotten to the end of my knowledge.
Problem #2 WSClient.create(String classname) is eating a ClassNotFoundException (and then throwing a NullPointerException)
Basicly when I make the following call
def event = client.create("com.mypackage.MyBean");
Same bean that i can see by browsing the services from the browser.
<xs:complexType name="myBean">
<xs:sequence>
<xs:element minOccurs="0" name="id" type="xs:long"/>
<xs:element maxOccurs="unbounded" minOccurs="0" name="facets" nillable="true" type="tns:beanBean"/>
<xs:element minOccurs="0" name="sortId" type="xs:string"/>
<xs:element minOccurs="0" name="itemId" type="xs:string"/>
<xs:element minOccurs="0" name="preview" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
I get a NPE. When i drill down to figure out why i get an NPE, I find that its because the classloader on the WSClient can't find "com.mypackage.MyBean" eats the ClassNotFoundException and then inevitably throws an NPE.
Edit: Should I be using a different client the WSClient/cxf ? I really wanted to avoid having to roll my own.....
Basicly I didn't find the answer. I basicly was forced to uninstall the two plugins and cxf and not using the groovy WSClient.
What i did was install the ws-client plugin for grails and that worked.
http://www.grails.org/plugin/ws-client
Try to remove the jaxen's jar in .grails lib cxf plugin folder in your project. It should solve your problem.

axis wsdl generation

I'm using Axis to model a sample WebService. What I'm doing now is trying to understand which are the limitations of the automated wsdl and code generation.
Now for some server side code:
this is the skeleton of the sample web service:
public class TestWebService {
public AbstractAttribute[] testCall( AbstractAttribute someAttribute ) {
....
and my data classes:
public abstract class AbstractAttribute {
String name;
/*get/set for name*/
public abstract T getValue();
public abstract void setValue(T value);
}
public class IntAttribute extends AbstractAttribute<Integer> {
Integer value;
public Integer getValue(){ return value; }
public void setValue(Integer value){ this.value = value; }
}
public class StringAttribute extends AbstractAttribute<String> {
String value;
/* ok, you got the point, get/set for value field */
}
The eclipse tool for Axis2 is quite happy to generate a wsdl from these sources, including the schema for the attribute classes, which is:
<xs:complexType name="AbstractAttribute">
<xs:sequence>
<xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="value" nillable="true" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="IntAttribute">
<xs:complexContent>
<xs:extension base="xsd:AbstractAttribute">
<xs:sequence>
<xs:element minOccurs="0" name="value" nillable="true" type="xs:int"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="StringAttribute">
<xs:complexContent>
<xs:extension base="xsd:AbstractAttribute">
<xs:sequence>
<xs:element minOccurs="0" name="value" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
now, if see something strange here, AbstractAttribute hasn't the ** abstract="true" ** attribute, and define an anyType value element, which get rewrite in IntAttribute and StirngAttribute. I don't even know if this is a legal schema (I don't think it's legal, by the way).
More, if I try to generate a client from this wsdl (always using the eclipse tools) the generated sources won't compile, because AbstractAttribute defines an
Object localValue;
field and Int/String Attribute defines
int localValue;
and
String localValue;
..I tried to "accomodate" the sources (without many hopes, obviously), and the results are that the server try to instantiate an AbstractAttribute instance (throwing an InstantiationException).
So my question is: there is a way to model something like the data model above, or web services and XML schemas' in general are not the best tools to use for this particular case?
To explain the problem you are encountering, it helps to think of what Axis needs to do when your service is called.
Axis is simply a java web-application...when it receives a request for a service, it will look up the mapping that you've defined for it. If it finds a mapping, it tries to create an instance of the necessary classes you've defined to service the request.
If you've defined the classes as abstract or as interfaces then you'll get InstantiationExceptions since these types can't be created. When Axis tried to create the wsdl, it won't be able to figure out what type to put so it will use "anyType."
To answer your question: you CAN use the the model you have above in your code, but you won't be able to use these classes with Axis. What we have typically done in our projects is:
Define the classes we need, as we would in a typical Object-Oriented application
Define "transport-only" classes that are used for web services. These classes are composed of simple types and can be easily created. They are only used for exchanging web-service messages. We use these classes with Axis.
Find some way for these two types of classes to easily share/exchange information. You can have interfaces that are shared by both (but Axis doesn't know about) or even use BeanUtils.copyProperites to keep two different objects in sync.
Hope that answers your question.

Resources