Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requests/responses linked examples not working #334

Closed
fadighattas100 opened this issue Oct 16, 2020 · 1 comment
Closed

requests/responses linked examples not working #334

fadighattas100 opened this issue Oct 16, 2020 · 1 comment

Comments

@fadighattas100
Copy link

fadighattas100 commented Oct 16, 2020

  • L5-Swagger Version: 8.0.2 (^8.0 | grep l5-swagger)
  • PHP Version (7.3):
  • OS: docker, ubuntu

Description:

trying to use @OA\Examples but it's not rendering in the ui correctly

first i defined the @OA\Examples

` /**

 *
 *      @OA\Examples(
 *        summary="VehicleStore",
 *        example = "VehicleStore",
 *      value = {
 *  		    "result": null,
 *  			"message": "Unauthorized, you don't have access to this content, Invalid token.",
 *  			"status": 401
 *         },
 *      )
 */`

then i added request that use this example:

`/**

 * @OA\Post(
 *     operationId="vehicleStore",
 *     tags={"vehicle"},
 *     summary="Store Vehicle - with components and trips (damages & loads)",
 *     description="Store vehicle",
 *     path="/vehicle",
 *     security={{"bearerAuth":{}}},
 *
 *     @OA\RequestBody(
 *       @OA\JsonContent(
 *               allOf={
 *                      @OA\Schema(ref="#/components/schemas/APIResponse"),
 *                      @OA\Schema(ref="#/components/schemas/CustomRequestBody")
 *              },
*       examples = {
 *          @OA\Schema( ref="#/components/examples/VehicleStore")
	*
 *     }
 *
 *          )
 *      ),
 *
 *     @OA\Response(
 *         response="200",
 *         description="Successful",
 *          @OA\JsonContent()
 *      ),
 * )
 *
 * @return JsonResponse
 *
 */`

in the api-docs.json:

`

   "examples": {
        "VehicleStore": {
            "summary": "VehicleStore",
             "value": {
                "result": null,
                "message": "Unauthorized, you don't have access to this content, Invalid token.",
                "status": 401
            }
        }
    },

`

in the ui the Example Value is empty but the VehicleStore selected in the dropdown of examples:

image

Steps To Reproduce:

pls add an example to know how to use requests/responses linked examples correctly

i think this also has something related to this problem

2651

Update :

i tried to check Swagger UI online and it seems the generated json form L5 Swager is not right for swagger ui maybe i did something wrong with example definition using L5 syntax so the generated json was wrong but the current generated json from my L5 Syntax is

`

   "examples": {
        "VehicleStore": {
            "summary": "VehicleStore",
             "value": {
                "result": null,
                "message": "Unauthorized, you don't have access to this content, Invalid token.",
                "status": 401
            }
        }
    },

`

but it should be like

`

     "examples": {
          "VehicleStore_1": {
              "summary": "VehicleStore",
              "value": {
                      "result": null,
                        "message": "Unauthorized, you don't have access to this content, Invalid token.",
                          "status": 401
                           }
                  }
    }

`

to make it work with swagger ui

@fadighattas100
Copy link
Author

fadighattas100 commented Oct 16, 2020

i found the correct L5 syntax to do multiple examples with using refs also :

so first i define the examples as:

/**
 *      @OA\Examples(
 *        summary="VehicleStoreEx1",
 *        example = "VehicleStoreEx1",
 *     	 value = {
*  		    "name": "vehicle 1"
*         },
*      )
*/

/**
 *      @OA\Examples(
 *        summary="VehicleStoreEx2",
 *        example = "VehicleStoreEx2",
 *     	 value = {
 *  		    "name": "vehicle 1",
 *  		    "model": "Tesla"
 *         },
 *      )
 */

then i defined a request body because i need multiple examples for one request i think the same thing can be done to a response also if any one need response with multiple examples so my request body was:

/**
 * @OA\RequestBody(
 *     request="VehicleStoreRequestBody",
 *     description="Pet object that needs to be added to the store",
 *     required=true,
 *     @OA\JsonContent(
 *        allOf={
 *           @OA\Schema(ref="#/components/schemas/APIResponse"),
 *           @OA\Schema(ref="#/components/schemas/CustomRequestBody")
 *        },
 *       examples = {
 *          "example_1" : @OA\Schema( ref="#/components/examples/VehicleStoreEx1"),
 *          "example_2" : @OA\Schema( ref="#/components/examples/VehicleStoreEx2"),
 *      }
 *    )
 * )
 */

then i linked the request body to the request as:

/**
* @OA\Post(
 *     operationId="vehicleStore",
 *     tags={"vehicle"},
 *     summary="Store Vehicle - with components and trips (damages & loads)",
 *     description="Store vehicle",
 *     path="/vehicle",
 *     security={{"bearerAuth":{}}},
 *    requestBody={"$ref": "#/components/requestBodies/VehicleStoreRequestBody"},
*     @OA\Response(
 *         response="200",
 *         description="Successful",
 *          @OA\JsonContent()
 *      ),
 * )
 *
 * @return JsonResponse
*
 */

and that generated the flowing objects in api-docs.json

        "examples": {
            "VehicleStoreEx1": {
                "summary": "VehicleStoreEx1",
                "value": {
                    "name": "vehicle 1"
                }
            },
            "VehicleStoreEx2": {
                "summary": "VehicleStoreEx2",
                "value": {
                    "name": "vehicle 1",
                    "model": "Tesla"
                }
            }
        },
        "requestBodies": {
            "VehicleStoreRequestBody": {
                "description": "Pet object that needs to be added to the store",
                "required": true,
                "content": {
                    "application/json": {
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/components/schemas/APIResponse"
                                },
                                {
                                    "$ref": "#/components/schemas/CustomRequestBody"
                                }
                            ]
                        },
                        "examples": {
                            "example_1": {
                                "$ref": "#/components/examples/VehicleStoreEx1"
                            },
                            "example_2": {
                                "$ref": "#/components/examples/VehicleStoreEx2"
                            }
                        }
                    }
                }
            }
        },

hope this will help any one searching how to do response or request with multiple examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant