首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails rswag自动生成的示例不起作用

Rails rswag自动生成的示例不起作用
EN

Stack Overflow用户
提问于 2021-10-27 22:51:21
回答 1查看 3K关注 0票数 5

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

控制器的rspec规范如下:

代码语言:javascript
复制
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文件如下所示

代码语言:javascript
复制
  "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文档所示。但是自动生成不起作用。要实现自动生成功能,我需要做些什么?

  1. 对于我们的用例来说,如果我们能够自动为请求体生成模式,我们将得到最大的好处。rswag文档并没有指明如何做到这一点。如何自动生成请求体的架构?

更新

对于#1,我让它通过停止试运行来工作,即命令需要是SWAGGER_DRY_RUN=0 RAILS_ENV=test rails rswag而不是RAILS_ENV=test rails rswag

第二,我的问题犯了一个错误。我更新了上面的问题,以引用请求体而不是响应体。

EN

回答 1

Stack Overflow用户

发布于 2022-11-12 09:58:56

几个星期以来,我一直在努力应对自动生成的反应。但是,将rswag宝石更新到最新版本解决了我的许多问题。希望这能帮到一些人

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69746336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档