SOAP - nillable="true"

Gabriel Spiteri picture Gabriel Spiteri · Feb 15, 2011 · Viewed 21.9k times · Source

I am building a web service using the Zend Framework. I am using the Zend_Soap_AutoDiscover class for the generation of my WSDL. I am using various complex type in this web service form example:

StockItemEntity Class

class StockItemEntity {
    /** @var string */
    public $sStockCode;
    /** @var string */
    public $sQty;

    public function __construct($sStockCode, $sQty){
        $this->sStockCode = $sStockCode;
        $this->sQty = $sQty;
    }   
}

WSDL Definition

<xsd:complexType name="StockItemEntity">
  <xsd:all>
    <xsd:element name="sStockCode" type="xsd:string" nillable="true"/>
    <xsd:element name="sQty" type="xsd:string" nillable="true"/>
  </xsd:all>
</xsd:complexType>

From what I understood from reading over the web the nillable="true" is there because properties of any object can be set to null. Thus the nillable="true" is need to maintain a valid XML document even if the StockItemEntity object has all its properties set to null.

My concern is that those two properties must always be passed to the web method. Is it possible to remove the "nillable=true" to sort of force the properties not to be null? Or else is there any way to force non null values in those properties. I was hoping to avoid having to validate them on the webservice side.

Thanks

Kind Regards

Gabriel

Answer

JamesG picture JamesG · Jan 16, 2012

At some stage between Zend Framework 1.10.7 and 1.11.0, they introduced a piece of code that uses reflection to check if there's a default value defined for an attribute of a class and, if not, it decides that the class is "nillable" and adds nillable="True" to the attribute definition in the WSDL. This is apparently meant to improve interoperability with some flaky version of .Net.

To stop Zend Framework from adding the nillable flag, just initialize those properties when they are declared, eg.

/** @var string */
public $sStockCode = '';
/** @var string */
public $sQty = '';

Hope that helps.