在 Rails 中使用 rswag - 索引响应(数组)的语法是什么?

Using rswag in Rails - what is the syntax for index responses (arrays)?

不幸的是,rswag 的“文档”似乎非常缺乏,并且没有给出如何实现索引操作的示例。我的“创建”规范在 Swagger UI 中显示架构和示例值,但我的“索引”方法未在 UI.

中显示任何一个

我需要在这里更改什么?我已经根据我发现的有限示例进行了尝试,其中 none 似乎有效。

path '/api/v1/users' do

get('list users') do
  tags 'Users'

  response(200, 'successful') do
    schema type: :array,
           properties: {
             id: { type: :integer },
             title: { type: :string },
             created_at: { type: :datetime},
             updated_at: { type: :datetime}
           }
    after do |example|
      example.metadata[:response][:content] = {
        'application/json' => {
          example: JSON.parse(response.body, symbolize_names: true)
        }
      }
    end
    run_test!
  end
end

post('create user') do
  tags 'Users'
  consumes 'application/json'
  parameter name: :user, in: :body, schema: {
    type: :object,
    properties: {
      title: { type: :string }
    },
  required: [ 'title', 'description' ]
}
  response(200, 'successful') do

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

我也试过像这样格式化模式,基于我发现的另一个例子,它也没有做任何事情(schema/example 只是没有显示):-

    schema type: :object,
           properties: {
             collection: {
               type: :array,
               items: {
                 type: :object,
                 properties: {
                   id: { type: :integer },
                   title: { type: :string },
                   created_at: { type: :datetime},
                   updated_at: { type: :datetime}
                 }
               }
             }
           }

检查您的 swagger_helper 文件。如果您按照文档进行操作,可能是这样的:

RSpec.configure do |config|
  config.swagger_root = Rails.root.to_s + '/swagger'

  config.swagger_docs = {
    'v1/swagger.json' => {
      openapi: '3.0.1',
      info: {
        title: 'API V1',
        version: 'v1',
        description: 'This is the first version of my API'
      },
      servers: [
        {
          url: 'https://{defaultHost}',
          variables: {
            defaultHost: {
                default: 'www.example.com'
            }
          }
        }
      ]
    }
  }
end

只需将 opeanapi: '3.0.1' 替换为 swagger: '2.0'。我也遇到过同样的问题,这是迄今为止我找到的唯一解决方法。

对我有用的是使用 produces 方法,例如:

get 'Retrieves the lists' do
  tags 'Lists'
  produces 'application/json'

  response '200', 'Lists found' do
    schema type: :array,
           items: {
             type: :object,
             properties: {
               id: { type: :integer },
               title: { type: :string },
               created_at: { type: :datetime},
               updated_at: { type: :datetime}
             }
           }
    run_test!
  end
end

@s89_