0

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

我需要在这里改变什么?我已经根据我发现的有限示例来玩弄它,但它们似乎都不起作用。

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 type: :object,
           properties: {
             collection: {
               type: :array,
               items: {
                 type: :object,
                 properties: {
                   id: { type: :integer },
                   title: { type: :string },
                   created_at: { type: :datetime},
                   updated_at: { type: :datetime}
                 }
               }
             }
           }
4

1 回答 1

1

检查你的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'. 我遇到了同样的问题,这是迄今为止我发现的唯一解决方法。

于 2021-03-13T23:39:21.093 回答