0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

oapi-codegenを用いたコード自動生成

Posted at

目的

oapi-codegenを使ってコード自動生成する

準備

  1. openapi.yml 作成
    openapi: 3.0.0
    info:
      title: Go API template
      version: 0.1.0
    
    servers:
      - url: http://localhost:8888/api/v1
    
    paths:
      /album/{id}:
        get:
          summary: find album by id
          parameters:
            - name: id
              in: path
              required: true
              schema:
                type: integer
          responses:
            '200':
              description: OK
              content:
                application/json:
                  schema:
                    $ref: '#/components/schemas/AlbumResponse'
    
    components:
      schemas:
        Category:
          type: object
          properties:
            id:
              type: integer
            name:
              type: string
              enum:
                - food
                - music
                - sports
          required:
            - name
      
        AlbumResponse:
          type: object
          properties:
            id:
              type: integer
            title: 
              type: string
            Category:
              $ref: '#/components/schemas/Category'
    
    
  2. oapi-codegenインストール
    go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest
    
  3. GoのバイナリをPATHに追加
    export PATH="$(go env GOPATH)/bin:$PATH"
    
  4. インストール確認
    % oapi-codegen --version
    github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen
    v2.4.1
    
  5. config.yml 作成
    package: API
    generate:
      models: true
      gin-server: true
      client: true
      embedded-spec: true
    output: api/api.gen.go
    
  6. 自動生成実行
    % oapi-codegen --config=./api/config.yml ./api/openapi.yml
    

api.gen.go が生成されていれば成功

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?