SOAP - nillable=“true”

2019-06-07 21:41发布

问题:

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

回答1:

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.



回答2:

nillable='true' indicates that the element CAN be null in an xml document instance. If the element in an xml document IS null, then this is indicated by

<sStockCode xsi:nil='true' />

(the element itself must be empty)

It sounds like you need to change the xsd to nillable='false'. Also, string can be empty - if you want to prevent against empty strings with the minLength attribute in your xsd. You can also use default to specify a default value for the string if it isn't provided.

W3schools has good reference material for XSD's here

HTH