我有一个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文档的要求。但是自动生成不起作用。我需要做什么来让自动生成功能起作用?
1.对于我们的用例,如果我们可以自动生成请求主体的模式,我们将获得最大的好处。rswag文档没有说明如何做到这一点。我如何自动生成请求主体的模式?
更新
对于#1,我通过禁用空运行使其工作,即命令需要是SWAGGER_DRY_RUN=0 RAILS_ENV=test rails rswag
而不是RAILS_ENV=test rails rswag
。
对于第二个问题,我在问题中犯了一个错误。我已经更新了上面的问题,引用了请求正文而不是响应正文。
1条答案
按热度按时间zkure5ic1#
几周来我一直在为自动生成响应而挣扎。然而,将rswag gems更新到最新版本修复了我的很多问题。希望这能帮助一些人