如何用招摇包裹Laravel资源

发布于 2025-01-24 06:41:47 字数 2387 浏览 0 评论 0原文

我正在学习如何用招摇使用laravel,我有一个问题:

我有用户控制器:

usercontroller.php

class UserController extends Controller
{
    /**
     * Shows authenticated user information
     * 
     * @OA\Get(
     *     tags={"Authorize"},
     *     path="/user",
     *     summary="get user detail",
     *     security={{ "AuthBearer":{} }},
     *     @OA\Response(
     *          response="200", 
     *          description="success",
     *          @OA\JsonContent(
     *              ref="#/components/schemas/UserResource"
     *          )
     *      ),
     *     @OA\Response(response="401", description="Unauthenticated")
     * )
     *     
     * @return \Illuminate\Http\Response
     */  
    public function user()
    {                
        return new UserResource(auth()->user());
    }
}

userresource.php,

/**
 * Class UserResource
 * 
 * @OA\Schema(
 *  @OA\Xml(name="UserResource")
 * )
 */
class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     * 
     * @OA\Property(format="int64", title="ID", default=1, description="ID", property="id"),
     * @OA\Property(format="string", title="name", default="Demo", description="Name", property="name"),
     * @OA\Property(format="string", title="username", default="demo", description="Username", property="username"),
     * @OA\Property(format="string", title="avatar_path", default="https://via.placeholder.com/640x480.png/0000bb?text=avatar", description="Avatar Path", property="avatar_path")
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {        
        return [
            'id' => $this->id,
            'name' => $this->name,
            'username' => $this->username,
            'avatar_path' => $this->avatar_path            
        ];
    }
}

这很好,但是在Swagger文档中,此路线的示例值是这样的:

{
  "id": 1,
  "name": "demo",
  "username": "demo",
  "avatar_path": "https://via.placeholder.com/640x480.png/0000bb?text=avatar"
}

但是当我以夸张的方式执行路线,它返回以下:

{
  "data": {
    "id": 1,
    "name": "demo",
    "username": "demo",
    "avatar_path": "https://via.placeholder.com/640x480.png/0000bb?text=avatar"
  }
}

这是在数据属性中包裹的,我希望该示例具有相同的格式,如何实现这一目标?谢谢。

I am learning how to use laravel with swagger, and I have this issue:

I have the user controller:

UserController.php

class UserController extends Controller
{
    /**
     * Shows authenticated user information
     * 
     * @OA\Get(
     *     tags={"Authorize"},
     *     path="/user",
     *     summary="get user detail",
     *     security={{ "AuthBearer":{} }},
     *     @OA\Response(
     *          response="200", 
     *          description="success",
     *          @OA\JsonContent(
     *              ref="#/components/schemas/UserResource"
     *          )
     *      ),
     *     @OA\Response(response="401", description="Unauthenticated")
     * )
     *     
     * @return \Illuminate\Http\Response
     */  
    public function user()
    {                
        return new UserResource(auth()->user());
    }
}

UserResource.php

/**
 * Class UserResource
 * 
 * @OA\Schema(
 *  @OA\Xml(name="UserResource")
 * )
 */
class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     * 
     * @OA\Property(format="int64", title="ID", default=1, description="ID", property="id"),
     * @OA\Property(format="string", title="name", default="Demo", description="Name", property="name"),
     * @OA\Property(format="string", title="username", default="demo", description="Username", property="username"),
     * @OA\Property(format="string", title="avatar_path", default="https://via.placeholder.com/640x480.png/0000bb?text=avatar", description="Avatar Path", property="avatar_path")
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {        
        return [
            'id' => $this->id,
            'name' => $this->name,
            'username' => $this->username,
            'avatar_path' => $this->avatar_path            
        ];
    }
}

this is working fine, but in the swagger docs, the example value for this route is this:

{
  "id": 1,
  "name": "demo",
  "username": "demo",
  "avatar_path": "https://via.placeholder.com/640x480.png/0000bb?text=avatar"
}

But when I execute the route in swagger, it returns this:

{
  "data": {
    "id": 1,
    "name": "demo",
    "username": "demo",
    "avatar_path": "https://via.placeholder.com/640x480.png/0000bb?text=avatar"
  }
}

This is wrapped inside a data property, I would like that the example has the same format, how could achieve that? thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌逼全场 2025-01-31 06:41:47

您可以在userresource.php中写入:

/**
 * @OA\Schema(
 *   @OA\Xml(name="UserResource"),
 *   @OA\Property(property="data", type="array",
 *      @OA\Items(ref="#/components/schemas/User"))
 *   ),
 * )
 */

以及在model user.php中编写

/**
 * @OA\Schema(
 *   @OA\Xml(name="User"),
 *   @OA\Property(format="int64", title="ID", default=1, description="ID", property="id"),
 *   @OA\Property(format="string", title="name", default="Demo", description="Name", property="name"),
 *   @OA\Property(format="string", title="username", default="demo", description="Username", property="username"),
 *   @OA\Property(format="string", title="avatar_path", default="https://via.placeholder.com/640x480.png/0000bb?text=avatar", description="Avatar Path", property="avatar_path")
 * )
 */

You can write in UserResource.php:

/**
 * @OA\Schema(
 *   @OA\Xml(name="UserResource"),
 *   @OA\Property(property="data", type="array",
 *      @OA\Items(ref="#/components/schemas/User"))
 *   ),
 * )
 */

And in model User.php

/**
 * @OA\Schema(
 *   @OA\Xml(name="User"),
 *   @OA\Property(format="int64", title="ID", default=1, description="ID", property="id"),
 *   @OA\Property(format="string", title="name", default="Demo", description="Name", property="name"),
 *   @OA\Property(format="string", title="username", default="demo", description="Username", property="username"),
 *   @OA\Property(format="string", title="avatar_path", default="https://via.placeholder.com/640x480.png/0000bb?text=avatar", description="Avatar Path", property="avatar_path")
 * )
 */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文