首页 > 解决方案 > rails gem rswag 自定义参数

问题描述

如何在我的 rswag 规范中添加自定义参数?

Rswag 似乎只包含作为字段存在的参数

但我需要添加一个自定义参数。所以无论我做什么 - 我只能在控制器参数中看到我的模型的字段。

RSpec.describe Api::V1::LogsController, type: :request do
  path '/api/v1/logs' do
    post 'Create a Log' do
      tags 'Logs'
      security [ApiKeyAuth: {}]
      consumes 'application/json'
      produces 'application/json'
      parameter name: :log, in: :body, schema: {
        type: :object,
        properties: {
          title: { type: :string },
          description: { type: :string },
          my_custom_parameter: { type: :string }
        },
        required: %w(title description user_phone_number),
      }
      response '200', 'New Log created' do
        let(:Authorization) { "Token token=#{company.api_key}" }
        run_test!
      end
    end
  end
end

标签: ruby-on-railsrswag

解决方案


您可以使用您喜欢的任何名称添加任何参数,然后您可以在响应块中为这些参数赋值,如下所示:

parameter name: :params, in: :body, schema: {
        type: :object,
        properties: {
          profile_attributes: {
            type: :object,
            properties: {
              email: { type: :string, example: Faker::Internet.email(Faker::Name.first_name) }
            },
            required: %w[email]
          }

response('201', 'successfully') do
   let(:params) do
          {
            profile_attributes: { email: Faker::Internet.email(Faker::Name.first_name) }
          }
end

推荐阅读