๐Ÿ“Framework/๐ŸŽต Laravel

[Laravel] ๋ผ๋ผ๋ฒจ์— swagger ์ ์šฉํ•˜๊ธฐ

yujch 2023. 4. 21. 11:09
๋ฐ˜์‘ํ˜•

๊ธฐ๋ณธ ์„ธํŒ…

1. ํ”„๋กœ์ ํŠธ์— swagger ์„ค์น˜ํ•˜๊ธฐ

composer require "darkaonline/l5-swagger"

 

2. config ํŒŒ์ผ ์ƒ์„ฑ

php artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider"

 

3. provider ์„ค์ •

config/app.php ํŒŒ์ผ ์ˆ˜์ •

'providers' => [
	/*
    * Laravel Framework Service Providers...*/
    L5Swagger\L5SwaggerServiceProvider::class,
]

 

4. config ์„ค์ •

config/I5-swagger.php ํŒŒ์ผ ์ˆ˜์ •

'api' => [
	'title' => {์›ํ•˜๋Š” ์ œ๋ชฉ}
],
'routes' => [
	/*
    * Route for accessing api documentation interface
    */
    'api' => 'api/documentation',
],
'defaults' => [
	'routes' => [
    	/*
         * Route for accessing parsed swagger annotations.
         */
         'docs' => 'docs',

 

5. ์ปจํŠธ๋กค๋Ÿฌ ํŒŒ์ผ์ค‘ ํ•˜๋‚˜์— Annotation์œผ๋กœ ๊ธฐ์ˆ 

 /**
 * @OA\Info(
 *     title="TEST API",
 *     version="1.0",
 *     description="TEST API Document"
 * )
 **/

 

6. ์ ์šฉ (ํ„ฐ๋ฏธ๋„)

php artisan l5-swagger:generate

 

7. http://127.0.0.1:8000/api/documentation# ์œผ๋กœ ์ ‘์†

 


 

๋‚ด์šฉ ์ž‘์„ฑํ•˜๊ธฐ

- ์ปจํŠธ๋กค๋Ÿฌ ์•ˆ์˜ public function ์œ„์— ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

 

@OA\{Get | Post | Put | Delete}

* @OA\Get (
*     path="/api/v1/test/{id}",
*     operationId="testAPI",
*     tags={"ํ…Œ์ŠคํŠธ"},
*     summary="ํ…Œ์ŠคํŠธ api",
*     description="ํ…Œ์ŠคํŠธ์šฉ ์ž‘์„ฑ",
*     security={{"bearer_token":{}}},
*     @OA\Parameter(
*          name="id",
*          description="ํŒŒ๋ผ๋ฏธํ„ฐ",
*          in="path",
*          required=true,
*          example="test"
*          @OA\Schema(type="string")
*     ),
*     @OA\Response(
*          response="200",
*          description= "์„ฑ๊ณต"
*      ),
*     @OA\Response(
*          response="400",
*          description= "์‹คํŒจ"
*      ),
*     security={{"api_key_security":{}}}
* )

 

@OA\Parameter

- Get ๋ฐฉ์‹ ์ „์†ก

*     @OA\Parameter(
*          name="test_parameter",
*          description="ํŒŒ๋ผ๋ฏธํ„ฐ",
*          in="path",
*          required=true,
*          example="test"
*          @OA\Schema(type="string")
*     )

- Header๋กœ ์ „์†ก

*     @OA\Parameter(
*          name="test_parameter",
*          description="ํŒŒ๋ผ๋ฏธํ„ฐ",
*          in="header",
*          required=true,
*          example="test"
*          @OA\Schema(type="string")
*     )

 

@OA\RequestBody

- jsonํ˜•์‹

*     @OA\RequestBody(
*          @OA\MediaType(e
*              mediaType="application/json",
*              @OA\Schema(
*                  @OA\Property(property="image_file", type="int", description="ํšŒ์› ๋ฒˆํ˜ธ", example=1),
*                  @OA\Property(property="name", type="string", description="ํšŒ์› ์ด๋ฆ„", example="๊น€๊น€๊น€")
*              )
*          )
*      )

- form-data ํ˜•์‹

 *     @OA\RequestBody(
 *          @OA\MediaType(
 *              mediaType="multipart/form-data",
 *              @OA\Schema(
 *                  @OA\Property(property="image_file", type="file", description="์ด๋ฏธ์ง€ ํŒŒ์ผ", format="file")
 *              )
 *          )
 *      )

 

@OA\Response

- ๋ฌธ์ž ์ถœ๋ ฅ

 *     @OA\Response(
 *          response="400",
 *          description= "์‹คํŒจ"
 *      )

- ๋ฐฐ์—ด ์ถœ๋ ฅ

 *     @OA\Response(
 *          response="200",
 *          description= "์„ฑ๊ณต",
 *          @OA\JsonContent(
 *              @OA\Property(property="result", type="array",
 *                  @OA\Items(
 *                      @OA\Property(property="state", type="string", description="์ƒํƒœ", example=true),
 *                      @OA\Property(property="code", type="int", description="์ฝ”๋“œ", example=2000),
 *                      @OA\Property(property="descMsg", type="string", description="๋ฉ”์„ธ์ง€", example="SUCCESS"),
 *                  ),
 *              ),
 *              @OA\Property(property="data", type="string", description="๋ฐ์ดํ„ฐ", example="์ด๋ฏธ์ง€ url"),
 *          )
 *      )

- ๊ฐ์ฒด ์ถœ๋ ฅ

 *     @OA\Response(
 *          response="200",
 *          description= "์„ฑ๊ณต",
 *          @OA\JsonContent(
 *              @OA\Property(property="result", type="object",
 *                      @OA\Property(property="state", type="string", description="์ƒํƒœ", example=true),
 *                      @OA\Property(property="code", type="int", description="์ฝ”๋“œ", example=2000),
 *                      @OA\Property(property="descMsg", type="string", description="๋ฉ”์„ธ์ง€", example="SUCCESS"),
 *              ),
 *              @OA\Property(property="data", type="string", description="๋ฐ์ดํ„ฐ", example="์ด๋ฏธ์ง€ url"),
 *          )
 *      )

 


 

laravel bearer_token ์ธ์ฆ

comfig/I5-swagger.php ์ˆ˜์ •

'securityDefinitions' => [
    'securitySchemes' => [
    	'bearer_token' => [ // Unique name of security
        	'type' => 'apiKey', // Valid values are "basic", "apiKey" or "oauth2".
            'description' => 'Enter token in format (Bearer <token>)',
            'name' => 'Authorization', // The name of the header or query parameter to be used.
            'in' => 'header', // The location of the API key. Valid values are "query" or "header".
        ]
    ]
]

 

@OA\Info ์ž‘์„ฑํ•œ ๊ณณ์— ์ถ”๊ฐ€

 * @OAS\SecurityScheme(
 *      securityScheme="bearer_token",
 *      type="http",
 *      scheme="bearer"
 * )

 

@OA\{Get | Post | Put | Delete} ์•ˆ์— ์ถ”๊ฐ€

 *     security={{"bearer_token":{}}},

- Beareer ์“ฐ๊ณ  ํ† ํฐ ์ž…๋ ฅ

๋ฐ˜์‘ํ˜•