Search code examples
laravelswaggermiddlewareswagger-uicontent-type

swagger try it out redirect to login


I mount my swagger api documentation (with login to protect my route of "api/documentation"). The documentation UI it's ok but when i want execute "try it out"... Ups! Something going wrong...

I found that the class "Authenticate" (extends Middleware) redirect to login if not find the header this.headers['Content-Type'] = 'application/json';. Then, i gone to the view (resources/view/vendor/l5-swagger/index.blade.php) and set the correct headers:

    requestInterceptor: function() {
      this.headers['Authorization'] = 'Bearer '+token; 
      this.headers['Content-Type'] = 'application/json';
      this.headers['Accept'] = 'application/json';
      return this;
    }

The problem it's that when i "retry it out" the result it's a redirect to login again. And i pass the headers on the curl:

 curl -X GET "http://127.0.0.1:8000/api/mediador/xxx" -H "accept: */*" -H "Authorization: Bearer tokenGenerated" -H "Content-Type: application/json" -H "Accept: application/json"

Here an example that how i documentate my calls at the comments:

/**
 * Insertamos un nuevo mediador
 * @param Request $request
 * @return \Illuminate\Http\JsonResponse Respuesta formateada para la api del create
 *
 * @OA\Post(
 *     path="/api/mediador/",
 *     tags={"Mediador"},
 *     description="Insert data from mediador profile",
 *     @OA\RequestBody(
 *       required=true,
 *       @OA\MediaType(
 *           mediaType="application/json",
 *           @OA\Schema(
 *               type="object",
 *               required={"id_user_crm","email","nombre_usuario"},
 *               @OA\Property(
 *                   property="id_user_crm",
 *                   description="ID from sugar ",
 *                   type="string"
 *               ),
 *               @OA\Property(
 *                   property="email",
 *                   description="Email from profile",
 *                   type="string"
 *               ),
 *               @OA\Property(
 *                   property="nombre_usuario",
 *                   description="Name of user",
 *                   type="string"
 *               )
 *           )
 *       )
 *   ),
 *   @OA\Response(response="200", description="This is ok"),
 *   @OA\Response(response="401", description="Not logged with OAUTH token"),
 *   @OA\Response(response="403", description="Wrong data or duplicated fields in DataBase"),
 * )

Why redirect to login yet? How can i set the data


Solution

  • i found the solution from my problem:

    Swagger try read as html if you doesn't give the Response with correct "mediaType". Here put a complete comment to work with Swagger ui:

     /** 
     * @OA\Post(
     *     path="/api/mediador/",
     *     tags={"Mediador"},
     *     description="Insert data from mediador profile",
     *     @OA\RequestBody(
     *       required=true,
     *       @OA\MediaType(
     *           mediaType="application/json",
     *           @OA\Schema(
     *               type="object",
     *               required={"id_user_crm","email","nombre_usuario"},
     *               @OA\Property(
     *                   property="id_user_crm",
     *                   description="ID outside of this platform",
     *                   type="string"
     *               ),
     *               @OA\Property(
     *                   property="email",
     *                   description="Email from profile",
     *                   type="string"
     *               ),
     *               @OA\Property(
     *                   property="nombre_usuario",
     *                   description="Name of user",
     *                   type="string"
     *               )
     *           )
     *       )
     *   ),
     *   @OA\Response(
     *     response="200", description="Inserted data from mediador profiles",
     *     content={
     *          @OA\MediaType(
     *              mediaType="application/json",
     *              @OA\Schema(
     *                  @OA\Property(
     *                           property="id",
     *                           type="integer",
     *                           description="The id generated"
     *                   ),
     *                  @OA\Property(
     *                         property="id_user_crm",
     *                         type="integer",
     *                         description="The reference id with the other platform"
     *                   )
     *              )
     *          )
     *     }
     *   ),
     *   @OA\Response(response="401", description="Not logged with OAUTH token"),
     *   @OA\Response(response="403", description="Wrong data or duplicated fields in DataBase"),
     * )
     */
    

    With this comment before the function, read the @OA\Response and search @OA\MediaType to know what type of response returns the API REST.