Home > Blockchain >  How to combine a property and json response in Laravel Swagger?
How to combine a property and json response in Laravel Swagger?

Time:04-27

I have this a project that is using Laravel for the api and swagger for documentation

I have this method in my login controller:

/**
 * 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();  

        ]
    );
        
    
}

then, when I run this command: php artisan l5-swagger:generate ,it displays this error:

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▕ }

When I remove this

@OA\JsonContent(ref="#/components/schemas/UserResource")

it works, but the user data is not displayed, maybe I should only return the token, and make another request with the token to get the information about the logged user. What can I do?, thanks

EDIT:

@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") 
     *              )
     *          ),     
     *     )

I tried this, but it shows errors

CodePudding user response:

Pretty close except...

  • use of 'format' instead of 'type'
  • you do not have to specify the content type if your property is already wrapped in @OA\JsonContent
  • you need to be careful with surplus trailing ','; doctrine can be picky with that

Here is my take which does work stand-alone (except for the missing @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 {}

CodePudding user response:

The JsonContent you removed should be a property on the first JsonContent I think

  • Related