【问题标题】:Restler: Complex Object Types as ParametersRestler:作为参数的复杂对象类型
【发布时间】:2013-05-10 09:04:48
【问题描述】:

你好 Restler 朋友,

我目前正在尝试切换到 Restler 作为我们的主要 Rest-Framework。真正促使我做出决定的是大摇大摆的合规性。我发现为不断增长的系统提供一个很好的自动生成文档非常重要。

所以,我的问题是我似乎无法找到一种方法来使用“复杂对象”作为帖子参数,如在此处 swagger 中指定的那样:https://github.com/wordnik/swagger-core/wiki/Parameters

当然,您可以从“post assoc 数组”中检索所有内容,然后针对对象结构进行验证,但这不会记录在案,客户也不知道他期望什么结构。因此,我将不得不编写一个规范。手动...

例子:

/**
 * Create a new Account, based on the structure of the Account Object
 *
 * @param Account $account {@from body}
 *
 * @return string
 *
 */
protected function post(Account $account){...}

这将简单地作为未定义的“对象”放在 resource.json 中,而不是作为链接到 Account 对象的“复杂类型”(顺便说一句,这对于返回的对象非常有效)

资源.json

"parameters": [
    {
    "name": "REQUEST_BODY",
    "description": "Paste JSON data here with the following property.<hr/><mark>account</mark> <i>(required)</i>: add <mark>@param {type} $account {comment}</mark> to describe here",
    "paramType": "body",
    "required": true,
    "allowMultiple": false,
    "dataType": "Object",
    "defaultValue": "{\n    \"account\": \"\"\n}"
    }
    ],

是我遗漏了什么还是没有实现该功能?

提前感谢您帮助我!

更新:我设法直接从 post 方法中直接获取序列化对象,但我认为这是不可能的。这并不能解决自动文档问题,但仍然非常有价值。

【问题讨论】:

  • V3 分支(Restler 3.0 RC4)在这方面有了一些进展,但是Resources 类仍然没有扩展或解析参数类。我们将很快添加该功能
  • 感谢您的回复。有时间我去看看分行。
  • 附加问题:客户端应该如何发送请求体中的参数?我尝试传递一个简单的 JSON 对象,但没有成功。
  • 刚刚添加了对 V3 分支 (Restler 3.0 RC4) 参数的自定义类类型的支持。接下来我将改进资源类
  • Restler 3.0 RC5 中是否添加了此项?我在更新日志中没有看到任何提及它的内容。

标签: php restler swagger


【解决方案1】:

Restler 3 RC4 昨天发布,带有自定义类参数功能

阅读http://restler3.luracast.com/param.html#type 获取示例和测试代码

【讨论】:

    【解决方案2】:

    @igoru 对于您在评论中的问题 在函数文档之前使用 PHPDOC

     * @param {type} $variable {comment} **{@from body}**
    

    那是**{@from body}** 将使变量由请求正文发送。

    如果您想从 php 发送,请使用以下内容:

    <?php
    $data = array("name" => "Another", "email" => "another@email.com");
    $data_string = json_encode($data);
    
    $ch = curl_init("http://restler3.luracast.com/examples/_007_crud/index.php/authors");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
    );
    
    $result = curl_exec($ch);
    echo($result);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-20
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      相关资源
      最近更新 更多