gpt4 book ai didi

ruby-on-rails - 如何按照 json api 规范使用 rswag 创建?

转载 作者:行者123 更新时间:2023-12-04 03:40:17 27 4
gpt4 key购买 nike

我没有找到任何关于如何使用 rswag 根据 json api 生成文档的示例。

规范/集成/pets_spec.rb

需要'swagger_helper'

是否可以更改代码以生成json api的格式?

describe 'Pets API' do

path '/api/v1/pets' do

post 'Creates a pet' do
tags 'Pets'
consumes 'application/json', 'application/xml'
parameter name: :pet, in: :body, schema: {
type: :object,
properties: {
name: { type: :string },
photo_url: { type: :string },
status: { type: :string }
},
required: [ 'name', 'status' ]
}

response '201', 'pet created' do
let(:pet) { { name: 'Dodo', status: 'available' } }
run_test!
end

response '422', 'invalid request' do
let(:pet) { { name: 'foo' } }
run_test!
end
end
end

path '/api/v1/pets/{id}' do

get 'Retrieves a pet' do
tags 'Pets'
produces 'application/json', 'application/xml'
parameter name: :id, :in => :path, :type => :string

response '200', 'name found' do
schema type: :object,
properties: {
id: { type: :integer, },
name: { type: :string },
photo_url: { type: :string },
status: { type: :string }
},
required: [ 'id', 'name', 'status' ]

let(:id) { Pet.create(name: 'foo', status: 'bar', photo_url: 'http://example.com/avatar.jpg').id }
run_test!
end

response '404', 'pet not found' do
let(:id) { 'invalid' }
run_test!
end
end
end
end

如果 rswag 你会给我什么提示?

最佳答案

您需要更改 swagger_helper.rb文件。将文件格式更改为 JSON。我附上了下面那个的截图:

/image/MzOR9.png

关于ruby-on-rails - 如何按照 json api 规范使用 rswag 创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58905791/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com