Laravel Darkaonline/Swagger 验证请求不起作用

时间:2021-03-09 08:36:22

标签: laravel swagger laravel-8

我正在尝试为控制器中创建的 sendOTP 函数创建 API。我使用的软件包是 DarkaOnLine/L5-Swagger,它基于 OpenApi 3.0 生成 swagger UI。我是 Laravel Swagger API 的新手,问题在于验证。我有 2 个应该被验证的字段 country_codemobile。我将 $request 参数传递给 LoginRequest 以验证数据。下面是我的控制器和请求代码。

登录控制器

/**
 * @OA\POST(
 *      path="/api/sendLoginOTP",
 *      operationId="sendLoginOTP",
 *      tags={"LoginviaOTP"},
 *      summary="Send Otp to mobile lstest",
 *      description="Sends Otp to Mobile and Returns mobile number and country code with default country code",
 *
 *      @OA\Parameter(
 *          name="country_code",
 *          description="country code",
 *          required=true,
 *          in="path",
 *          @OA\Schema(
 *              type="string"
 *          )
 *      ),
 *     @OA\Parameter(
 *          name="mobile",
 *          description="mobile number",
 *          required=true,
 *          in="path",
 *          @OA\Schema(
 *              type="integer"
 *          )
 *      ),
 *      @OA\Response(
 *          response=200,
 *          description="successful operation"
 *       ),
 *      @OA\Response(response=400, description="Bad request"),
 *      @OA\Response(
 *          response=401,
 *          description="Unauthenticated",
 *      ),
 *      @OA\Response(response=404, description="Resource Not Found"),
 *      security={
 *         {
 *             "oauth2_security_example": {"write:projects", "read:projects"}
 *         }
 *     },
 * )
 */
public function sendOTP(LoginRequest $request)
{
    return response()->json('Validated');
}

登录请求

public function authorize()
{
    return true;
}

public function rules()
{
    return [
        'country_code' => [
            'required',
            'exists:countries,iso',
            'exists:users,country_code',
        ],
        'mobile' => [
            'required',
            'numeric',
            'exists:users,mobile',
        ],
    ];
}

Swagger 界面 Swagger UI

输入凭据后得到的输出 Output I get

JSFIDDLE 我得到的整个输出

期待输出 expected output

我的问题:每当我尝试验证参数时,它都没有得到验证。我不确定我哪里出错了。我不应该将 $request 传递给 LoginRequest 吗?如果不是这种方法,那么我应该如何验证参数中提供的数据?

1 个答案:

答案 0 :(得分:0)

我刚刚将参数的值从 path 更改为 query 并且对我来说效果很好

/**
  *     @OA\Parameter(
  *          ....
  *          in="query",
  *          ....
  *      ),
  *     @OA\Parameter(
  *          ....
  *          in="query",
  *          ....
  *      ),
  */