【问题标题】:How create with rswag following the json api specification?如何按照 json api 规范使用 rswag 创建?
【发布时间】:2020-03-13 07:19:02
【问题描述】:

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

spec/integration/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 你会给我什么建议?

【问题讨论】:

    标签: ruby-on-rails api swagger rswag


    【解决方案1】:

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

    https://i.stack.imgur.com/MzOR9.png

    【讨论】:

    • 这可能是一个误解:问题是关于测试+文档和遵循此处找到的 json.api 规范的 API:jsonapi.org,而不是关于 swagger.yml 或 swagger.json 文件跨度>
    猜你喜欢
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 2021-07-21
    • 2015-08-25
    相关资源
    最近更新 更多