3

我有一个 rails 6 项目,正在尝试使用 swagger 和 rswag gem 记录 API。

我对控制器的 rspec 规范如下:

require 'swagger_helper'

RSpec.describe 'api/v1/books', type: :request do

  let!(:book1) { create :book }
  let!(:book2) { create :book }
  let!( :account) { create :account }

  let!(:access_token) { Auth::JsonWebToken.encode(account_id: account.id) }
  let!(:Authorization) { access_token.to_s }

  path '/api/v1/books' do

    get('list books') do
      parameter name: :Authorization, in: :header, type: :string
      produces 'application/json'

      response(200, 'successful') do

        after do |example|
          example.metadata[:response][:content] = {
            'application/json' => {
              example: JSON.parse(response.body, symbolize_names: true)
            }
          }
        end
        run_test! do |response|
          data = JSON.parse(response.body)
          expect(data['books'].count).to eq(2)
        end
      end
    end
  end

  path '/api/v1/books/{id}' do
    parameter name: 'id', in: :path, type: :string, description: 'id'
    parameter name: :Authorization, in: :header, type: :string

    get('show book') do
      response(200, 'successful') do
        let(:id) { book1.id }

        after do |example|
          example.metadata[:response][:content] = {
            'application/json' => {
              example: JSON.parse(response.body, symbolize_names: true)
            }
          }
        end

        run_test! do |response|
          data = JSON.parse(response.body)
          expect(data['id']).to eq(book1['id'])
        end
      end
    end
  end

  path 'api/v1/books' do
    post 'Creates a book' do
      consumes 'application/json'

      parameter name: :book, in: :body, schema: {
        type: :object,
        properties: {
          title: { type: :string },
          author: { type: :string },
          publisher: { type: :string },
          editor: { type: :string }
        }
      }

      response '200', 'book created' do
        let(:book) { { title: 'New Book', author: 'New Author'}}

        after do |example|
          example.metadata[:response][:content] = {
            'application/json' => {
              example: JSON.parse(response.body, symbolize_names: true)
            }
          }
        end

        run_test! do |response|
          data = JSON.parse(response.body)
          expect(data['title']).to eq('New Book')
          new_books_in_db = Book.where(title: 'New Book').count
          expect(new_books_in_db).to eq(1)
        end
      end
    end
  end
end

生成的 swagger.json 文件如下


  "openapi": "3.0.1",
  "info": {
    "title": "API V1",
    "version": "v1"
  },
  "paths": {
    "/api/v1/books": {
      "get": {
        "summary": "list books",
        "parameters": [
          {
            "name": "Authorization",
            "in": "header",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "successful"
          }
        }
      }
    },
    "/api/v1/books/{id}": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "description": "id",
          "required": true,
          "schema": {
            "type": "string"
          }
        },
        {
          "name": "Authorization",
          "in": "header",
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "show book",
        "responses": {
          "200": {
            "description": "successful"
          }
        }
      }
    },
    "api/v1/books": {
      "post": {
        "summary": "Creates a book",
        "parameters": [

        ],
        "responses": {
          "200": {
            "description": "book created"
          }
        },
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "title": {
                    "type": "string"
                  },
                  "author": {
                    "type": "string"
                  },
                  "publisher": {
                    "type": "string"
                  },
                  "editor": {
                    "type": "string"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "servers": [
    {
      "url": "https://{defaultHost}",
      "variables": {
        "defaultHost": {
          "default": "www.example.com"
        }
      }
    }
  ]
}

该项目的完整代码位于https://github.com/marksack/rails_swagger_test(主分支)。

我有两个问题/问题。

  1. 我有根据 rswag 文档自动生成示例的代码。但是自动生成不起作用。我需要做什么才能使自动生成功能正常工作?

  2. 对于我们的用例,如果我们可以为请求正文自动生成模式,我们将获得最大的好处。rswag 文档没有说明如何做到这一点。如何为请求正文自动生成架构?

更新

对于#1,我通过禁用空运行来使其工作,即命令需要SWAGGER_DRY_RUN=0 RAILS_ENV=test rails rswag代替RAILS_ENV=test rails rswag.

对于#2,我在我的问题中犯了一个错误。我已经更新了上面的问题以引用请求正文而不是响应正文。

4

0 回答 0