我有一個專案,它使用 Laravel 作為 api 和 swagger 作為檔案
我的登錄控制器中有這個方法:
/**
* Handle an incoming authentication request.
*
*
* @OA\Post(
* tags={"UnAuthorize"},
* path="/login",
* summary="User Login",
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="object",
* ref="#/components/schemas/LoginRequest",
* )
* )
* ),
* @OA\Response(
* response="200",
* description="An example resource",
* @OA\JsonContent(
* type="object",
* @OA\Property(
* format="string",
* default="20d338931e8d6bd9466edeba78ea7dce7c7bc01aa5cc5b4735691c50a2fe3228",
* description="token",
* property="token"
* )
* ),
* @OA\JsonContent(ref="#/components/schemas/UserResource")
* ),
* @OA\Response(response="401", description="fail"),
* )
*
* @param \App\Http\Requests\Auth\LoginRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function store(LoginRequest $request)
{
$request->authenticate();
return response()->json(
[
"token" => $request->user()->createToken($request->email)->plainTextToken,
"user" => $request->user();
]
);
}
然后,當我運行此命令時: php artisan l5-swagger:generate ,它顯示此錯誤:
c:\myproject\vendor\zircote\swagger-php\src\Logger.php:40 36▕ $this->log = function ($entry, $type) { 37▕ if ($entry instanceof Exception) { 38▕ $ entry = $entry->getMessage(); 39▕ } ? 40▕ trigger_error($entry, $type); 41▕}; 42▕}
當我洗掉這個
@OA\JsonContent(ref="#/components/schemas/UserResource")
它可以作業,但不顯示用戶資料,也許我應該只回傳令牌,并使用令牌發出另一個請求以獲取有關登錄用戶的資訊。我能做什么?,謝謝
編輯:
@OA\Response(
* response="200",
* description="An example resource",
* @OA\JsonContent(
* type="object",
* @OA\Property(
* format="string",
* default="20d338931e8d6bd9466edeba78ea7dce7c7bc01aa5cc5b4735691c50a2fe3228",
* description="token",
* property="token"
* ),
* @OA\Property(
* format="application/json",
* property="user",
* @OA\JsonContent(ref="#/components/schemas/UserResource")
* )
* ),
* )
我試過這個,但它顯示錯誤
uj5u.com熱心網友回復:
非常接近,除了...
- 使用“格式”而不是“型別”
- 如果您的屬性已經包含在其中,則不必指定內容型別
@OA\JsonContent - 你需要小心多余的尾隨',';教義可能對此很挑剔
這是我的觀點,它可以獨立作業(除了缺少的@OA\Info):
<?php
use OpenApi\Annotations\OpenApi as OA;
/**
* @OA\Schema
*/
class LoginRequest{}
/**
* @OA\Schema
*/
class UserResource{}
/**
* Handle an incoming authentication request.
*
*
* @OA\Post(
* tags={"UnAuthorize"},
* path="/login",
* summary="User Login",
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="object",
* ref="#/components/schemas/LoginRequest"
* )
* )
* ),
* @OA\Response(
* response="200",
* description="An example resource",
* @OA\JsonContent(
* type="object",
* @OA\Property(
* type="string",
* default="20d338931e8d6bd9466edeba78ea7dce7c7bc01aa5cc5b4735691c50a2fe3228",
* description="token",
* property="token"
* ),
* @OA\Property(
* property="user",
* ref="#/components/schemas/UserResource"
* )
* )
* ),
* @OA\Response(response="401", description="fail")
* )
*
*/
class Controller {}
uj5u.com熱心網友回復:
您洗掉的 JsonContent 應該是我認為的第一個 JsonContent 的屬性
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/465358.html
下一篇:相同的路由不同的控制器
