如何用招摇包裹Laravel资源
我正在学习如何用招摇使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在userresource.php中写入:
以及在model user.php中编写
You can write in UserResource.php:
And in model User.php