LoginSignup
26
18

More than 3 years have passed since last update.

OpenAPIドキュメントを使ってモックサーバーを起動する

Last updated at Posted at 2020-03-18

OpenAPIを使ってAPI定義を書いている場合、その定義ファイルからモックのサーバーが起動できれば便利ですよね。
実際にAPIサーバーの実装が完了する前からクライアント側の実装を行うことができるので、とても効率が良いです!

調べれば色々と方法が出てきますが、個人的に楽だった方法をまとめておきます。

Prismがめちゃくちゃ便利

stoplight社のprismというOSSのモックサーバーを使います。
今回はこのDockerイメージを使うので、Dockerを入れていない場合は先に入れておいてください。

openapi.yamlの準備

まずはAPI定義を行った openapi.yaml を準備してください。
今回は、例としてシンプルな内容の定義ファイルを用意しました。

openapi.yaml
openapi: 3.0.0
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            format: int32
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Pet:
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
          example: 1
        name:
          type: string
          example: "pochi"
        tag:
          type: string
          example: "dog"
    Pets:
      type: array
      items:
        $ref: "#/components/schemas/Pet"
    Error:
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

モックサーバーの起動

先程用意した openapi.yaml と同じディレクトリ内で、下記を実行します。
これだけです!

docker run --rm -it -p 4010:4010 -v $PWD:/tmp stoplight/prism:3 mock -h 0.0.0.0 /tmp/openapi.yaml

実際にAPIを叩いてみます。

curl 'localhost:4010/pets'

レスポンスとして、 openapi.yamlexample で定義した内容が返ってきます。

[
  {
    "id": 1,
    "name": "pochi",
    "tag": "dog"
  }
]

ちなみに、 -d オプションを使うと、レスポンスが動的に変わります。

docker run --rm -it -p 4010:4010 -v $PWD:/tmp stoplight/prism:3 mock -h 0.0.0.0 -d /tmp/openapi.yaml

レスポンスはこの様に、毎回変わります。

[
  {
    "id": 8872624476737700000,
    "name": "Duis",
    "tag": "magna in laborum"
  },
  {
    "id": 6385831628202697000,
    "name": "veniam in do fugiat irure",
    "tag": "consequat Ut Excepteur anim tempor"
  },
  {
    "id": 4943733727999074000,
    "name": "dolor dolor",
    "tag": "dolore pariatur ullam"
  },
  {
    "id": 1524702861247148000,
    "name": "irure voluptate dolor mollit ex",
    "tag": "quis"
  }
]

本当に簡単ですね!

26
18
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
26
18