Zend soap autodiscovery and nillable=“true” in gen

2019-06-28 07:04发布

问题:

i'm using Zend soap autodiscovery to generate a WSDL file for my web server. The problem is that every element of every complexType defaults to nillable="true". How do i declare elements as required? I read PHPDoc but found nothing.

EDIT: The code:

class MyService {
    /**
     * Identify remote user.
     *
     * @param LoginReq
     * @return LoginResp
     */
    public function login($request) {
    // Code ....
    }
}

class LoginReq {
    /** @var string */
    public $username;
    /** @var string */
    public $password;
}
class LoginResp {
    /** @var string */
    public $errorCode;
}

Generated WSDL:

  <xsd:complexType name="LoginReq">
    <xsd:all>
      <xsd:element name="username" type="xsd:string" nillable="true"/>
      <xsd:element name="password" type="xsd:string" nillable="true"/>
    </xsd:all>
  </xsd:complexType>
  <xsd:complexType name="LoginResp">
    <xsd:all>
      <xsd:element name="errorCode" type="xsd:string" nillable="true"/>
    </xsd:all>
  </xsd:complexType>

EDIT2: I just found that to declare an element as required/optional you need to use minOccurs/maxOcurrs. Both of them default to 1, so every element is required by default. In order to declare an optional element, you declare it with minOccurs="1". Nillable is just for allowing elements to be empty. Again, how do i declare an element as optional (so Zend adds minOccurs="0" to that element)?

回答1:

if you have a default value set in your function definition, it will be nillable.

public function myMethod($argument = 'hello') {
    // $argument is nillable
}

If that isn't it, can you post your code with doc blocks?

EDIT: Your code sample clarifies a lot.

If you look at Zend/Soap/Wsdl/Strategy/DefaultComplesType.php around line 76, you'll see this:

            // If the default value is null, then this property is nillable.
            if ($defaultProperties[$propertyName] === null) {
                $element->setAttribute('nillable', 'true');
            }

That is the code that is determining if your "complex type" attribute is nillable. I would try updating your code to include a default value for the strings. Something like:

class LoginReq {
    /** @var string */
    public $username = '';
    /** @var string */
    public $password = '';
}

If you do that, the === null should evaluate to false. Be sure your code handles the data validation properly, though.

If that doesn't work, let me know!



回答2:

There is a functional patch available on the Zend forum. It consists in hacking DefaultComplexType.php to add the management of the minOccurs and maxOccurs attributes. This works perfectly and improves interoperability with some webservices.