【问题标题】:Zend soap autodiscovery and nillable="true" in generated WSDLZend soap 自动发现和生成的 WSDL 中的 nillable="true"
【发布时间】:2011-05-18 08:03:04
【问题描述】:

我正在使用Zend soap autodiscovery 为我的Web 服务器生成一个WSDL 文件。问题是每个 complexType 的每个元素都默认为nillable="true"。我如何根据需要声明元素?我阅读了 PHPDoc,但一无所获。

编辑:代码:

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;
}

生成的 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:我刚刚发现要将元素声明为必需/可选,您需要使用minOccurs/maxOcurrs。它们都默认为 1,因此默认情况下每个元素都是必需的。为了声明一个可选元素,你用minOccurs="1" 声明它。 Nillable 仅用于允许元素为空。同样,我如何将一个元素声明为可选(所以 Zend 将 minOccurs="0" 添加到该元素)?

【问题讨论】:

    标签: php zend-framework soap


    【解决方案1】:

    如果您在函数定义中设置了默认值,它将是可空的。

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

    如果不是这样,您可以使用文档块发布您的代码吗?

    编辑:您的代码示例说明了很多。

    如果您查看 Zend/Soap/Wsdl/Strategy/DefaultComplesType.php 的第 76 行,您会看到:

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

    这是确定您的“复杂类型”属性是否可空的代码。我会尝试更新您的代码以包含字符串的默认值。比如:

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

    如果你这样做,=== null 应该评估为假。不过,请确保您的代码正确处理数据验证。

    如果这不起作用,请告诉我!

    【讨论】:

    • 感谢您挖掘 Zend 源代码! +1 为您的努力。我不明白为什么默认行为是一切都是可空的,但属性具有默认值。不应该是相反的吗?顺便说一句,我刚刚发现这不是我想要的。我需要知道如何在某些元素上强制使用minOccurs=0
    • Zend 中唯一提到 minOccurs 的是 Zend/Soap/Wsdl/Strategy/ArrayOfTypeSequence ~line 145,它被硬编码为 0。我有一种感觉,做你想做的事您将不得不创建自己的库类来扩展 Zend 类并将 minOccurs 添加到您需要的对象中。我已经做了很多这样的事情,只是尽量将更改保持在最低限度,这样当你更新 Zend 时,你就不太可能发生冲突。
    • 附带说明,我今天发现将公共属性设置为 NULL 也会使其为 NULLABLE。如果您在默认情况下为其设置其他任何内容,例如 0 或 '',则它在 WSDL 中将变为不可为空
    【解决方案2】:

    Zend 论坛上有一个functional patch。它包括修改 DefaultComplexType.php 以添加对 minOccurs 和 maxOccurs 属性的管理。这可以完美运行并提高与某些 Web 服务的互操作性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多