DarkaOnLine / L5-Swagger

OpenApi or Swagger integration to Laravel
https://github.com/DarkaOnLine/L5-Swagger
MIT License
2.6k stars 390 forks source link

Specify Two Responses with the Same Response Code #609

Closed AlvaroGata closed 1 week ago

AlvaroGata commented 3 months ago

Description

I have some endpoints that return different bodies for the same response code. Specifically, when the status code is 400, I want to return different responses with an internal code, a message, and details.

@OA\Response( response=400, description="Authorization empty",
     *         @OA\JsonContent(
     *             @OA\Property( property="code", type="integer", example=4001 ),
     *             @OA\Property( property="message", type="string", example="Bad request" ),
     *             @OA\Property( property="details", type="string", example="Authorization empty" ),
     *         ),
     *     ),

I have seen in this link that schemas can be defined in OpenAPI 2.0, but I don't see how to do it with L5-Swagger. Link

eelco2k commented 1 month ago

it would be something like this in attributes:

use OpenApi\Attributes as OA;

#[OA\Get(
...
responses: [
   new OA\Response(
         response: 400,
         description: 'Authorization empty',
         content:  new OA\JsonContent(
            oneOf: [
                new OA\Schema(
                    items: new OA\Items(
                        properties: [
                            new OA\Property(property: "code", type: "integer", example: 4001),
                            new OA\Property(property: "message", type: "string", example: "Bad request"),
                            new OA\Property(property: "details", type: "string", example: "Authorization empty"),
                        ]
                    )
                ),
                // or with ...
                new OA\Schema(ref: "#/components/schemas/YourSchema"), // reference to existing Schema
            ]
        )
    )]
)]