【问题标题】:swagger php pass parameters as jsonswagger php将参数作为json传递
【发布时间】:2018-07-04 13:48:42
【问题描述】:

所以我在我的 php 代码中有如下评论,所以基本上我正在尝试生成我的 api 接受的以下类型的请求。

我有如下的 cmets:

/**
* Login API
*
*

*@SWG\Definition(
*   definition="login",    
*   description="Enter your username",
*)

*@SWG\Definition(
*   definition="password",
*   type="string",
*   description="Enter your Password",
*)

* @SWG\Post(
* path="/user/login",
* description="Login API.",
* operationId="Login",
* produces={"application/json"},
* tags={"login"},
* consumes={"application/json"},

*@SWG\Parameter(
*          name="params",
*          in="body",
*          type="string",
*          default="params={""username"":""abc@abc.com"",""password"":""12345678""}",
*          description="Login Detail",
*          required=true,
*          @SWG\Schema(ref="#/definitions/login")
*),
*@SWG\Response(
*   response=200,
*   description="Token overview."
*),
*@SWG\Response(
*   response=401,
*   description="Unauthorized action.",
*),    
* )
*/

预期输出

curl -X POST \
  http://localhost/project/api/web/user/login \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -H 'postman-token: 78219bf9-85a4-420f-7637-55e2f0e51b11' \
  -F 'params={"username":"abc@abc.com","password":"12345678"}'

实际输出

curl -X POST "http://localhost/project/api/web/user/login" -H "accept: application/json" -H "Content-Type: application/json" -d "params={\"username\":\"abc@abc.com\",\"password\":\"12345678\"}"

在 Swagger-ui 中,当我尝试执行它时,它给了我错误:

TypeError: 获取失败

我尝试过的

  1. 更改消耗为“multipart/form-data”(相同错误)
  2. 如果我将 in="body" 更改为 in="query" 则它可以工作,但我不能使用它。因为它需要在 POST 方法中传递参数。
  3. 可能不相关,但在控制台中出现以下错误:

root-injects.js:95 TypeError: e.get(...).entrySeq 不是函数 在 t.default (curlify.js:23) 在 t.value (curl.jsx:17) 在 t.render (root-injects.js:93) 在 u._renderValidatedComponentWithoutOwnerOrContext (ReactCompositeComponent.js:796) 在 u._renderValidatedComponent (ReactCompositeComponent.js:819) 在 u._updateRenderedComponent (ReactCompositeComponent.js:743) 在 u._performComponentUpdate (ReactCompositeComponent.js:721) 在 updateComponent (ReactCompositeComponent.js:642) 在 u.receiveComponent (ReactCompositeComponent.js:544) 在 Object.receiveComponent (ReactReconciler.js:122)

那么我应该怎么做才能解决这个问题呢?

【问题讨论】:

    标签: php swagger swagger-ui swagger-php


    【解决方案1】:

    in="body", 更改为in="formData",。瞧。

    【讨论】:

    • 它给出了类似“代码”的错误:“未记录”和“详细信息”:“类型错误:无法获取”。
    • 我可以解决,但显示为文本输入,有没有办法将其转换为文本区域?
    【解决方案2】:

    从 Swagger 参数中删除默认属性,而是在 Swagger 架构中设置它们。

    @SWG\Schema(
        @SWG\Property(property="username", type="string", example="abc@abc.com"),
        @SWG\Property(property="password", type="string", example="12345678")
    )
    

    否则,如果您希望使用 swagger 定义,请在定义中设置上述 swagger 属性。

    【讨论】:

      【解决方案3】:

      通过结合@Pusparaj 和@delboy1978uk 提供的答案来解决

      基本上我必须将in="body" 更改为in="formData"

      之后我必须更改我的评论,如下所示:

      之前

      *@SWG\Parameter(
      *          name="params",
      *          in="body",
      *          type="string",
      *          default="params={""username"":""abc@abc.com"",""password"":""12345678""}",
      *          description="Login Detail",
      *          required=true,
      *          @SWG\Schema(ref="#/definitions/login")
      *),
      

      更新

      @SWG\Parameter(
          name="params",
          in="formData",
          type="string",
          default="params={""username"":""abc@abc.com"",""password"":""12345678""}",
          description="Login Detail",
          required=true,
          @SWG\Schema(
              @SWG\Property(property="username", type="string"),
              @SWG\Property(property="password", type="string")
          )
      ),
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-15
        • 1970-01-01
        • 2011-12-04
        • 1970-01-01
        • 2017-03-04
        • 2021-03-22
        • 2022-01-26
        相关资源
        最近更新 更多