Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravel や Symfony で手っ取り早く
OpenAPI のドキュメントを作成する

SAW
November 14, 2024

Laravel や Symfony で手っ取り早く
OpenAPI のドキュメントを作成する

第40回関西PHP勉強会 の発表資料です。

SAW

November 14, 2024
Tweet

More Decks by SAW

Other Decks in Programming

Transcript

  1. $(whoami) ࢯ໊Ճ౻फҰ࿠ ࡀ  ϋϯυϧωʔϜ4"8 9 چ5XJUUFS !B[VLJ@FBUFS ؔ੢ͷ*5ΤϯδχΞίϛϡχςΟͷ೐΍͔͠୲౰ ࣗশ

     େࡕࡏॅɾѪ஌ग़਎ ಘҙ෼໺8FCΞϓϦέʔγϣϯ։ൃ -BSBWFM 7VF  ྉཧͷՃ೤࣌ؒΛ୹ॖ͢ΔͨΊʹ ڧՐͰௐཧͨ͜͠ͱ͕͋Δͷ͸ ࣗ෼͚ͩͰ͸ͳ͍͸ͣ ࠓ೔ͷ໎ݴ
  2. 0QFO"1*ͷ࢓༷ॻͷྫ :".-ܗࣜ  openapi: 3.0.0 info: title: Sample description: 'Sample

    API' version: 1.0.0 paths: '/api/hoge/{id}': get: parameters: - name: id in: path description: 'ID of hoge' required: true schema: type: string responses: 200: description: 'hoge response body' content: application/json: schema: properties: id: type: integer message: type: string type: object
  3. /FMNJP0QFO"QJ#VOEMFͷΠϯετʔϧͱ࣮૷ྫ  # NelmioOpenApiBundle のインストール composer require nelmio/api-doc-bundle # Swagger

    UI に必要な依存パッケージのインストール composer require symfony/twig symfony/asset /FMNJP0QFO"QJ#VOEMFͷΠϯετʔϧखॱ use OpenApi\Attributes as OA; class SampleController extends AbstractController { #[Route('/hoge/{id}', methods: ['GET'])] #[OA\Response( response: 200, description: 'Get specified hoge data', content: new OA\JsonContent( ref: new Model(type: Hoge::class), ) )] public function get(int $id): JsonRespnose { // 略 } } ࣮૷ྫ app.swagger_ui: path: /api/doc method: GET defaults: { _controller: nelmio_api_doc.controller.swagger_ui } 4XBHHFS6*Λ༗ޮԽ͢Δઃఆͷྫ config/routes/nelmio_api_doc.yaml
  4. -4XBHHFSͷΠϯετʔϧͱ࣮૷ྫ  # L5 Swagger のインストール composer require darkaonline/l5-swagger -4XBHHFSͷΠϯετʔϧखॱ

    use OpenApi\Attributes as OA; class SampleController extends Controller { #[OA\Get( path: '/api/hoge/{id}', summary: 'Get specified hoge data', responses: [ new OA\Response( response: Response::HTTP_OK, description: 'hoge response body', ), ) )] public function get(int $id): JsonRespnose { // 略 } ࣮૷ྫ # ServiceProvider の登録 php artisan vendor:publish --provider \ "L5Swagger\L5SwaggerServiceProvider" # Swagger UI の 生 成 php artisan l5-swagger:generate -4XBHHFSͷઃఆͱυΩϡϝϯτੜ੒