Rswag: How to mark required fields in a nested param object

Created on 27 Jul 2018  路  2Comments  路  Source: rswag/rswag

I'm using rswag to document my API-only Rails app that uses Strong Parameters.

model:

class Call < ApplicationRecord
  belongs_to :interview

  validates :to, :from, :sid, presence: true, allow_blank: false
end

controller:

 # POST /calls
  def create
    @call = Call.create!(call_params)
    json_response(@call, :created)
  end

  private

    def call_params
      # whitelist params
      params.require(:call).permit(
        :duration, :from, :sid, { statuses: [] }, :to, :interview_id
      )
    end

spec:

  path "/v1/calls" do
    post "Creates a call" do
      tags "Calls"
      consumes "application/json"
      produces "application/json"
      parameter name: :call, in: :body, schema: {
        type: :object,
        properties: {
          interview_id: { type: :integer },
          duration: { type: :number, format: :float },
          from: { type: :string },
          sid: { type: :string },
          statuses: { type: :array, items: { type: :string } },
          to: { type: :string }
        },
        required: %w[interview_id from sid to]
      }

When I swaggerize it, though, there's no indication which fields are required (I'd expect something like an asterisk or comment in the model example). Am I missing something?

screen shot 2018-07-26 at 5 15 21 pm

Most helpful comment

See above the JSON example, there's a tab bar that allows you to toggle between "Example Value" and "Model". If you click on the model tab, you'll see a full description of the object (as opposed to example JSON) and that should indicate which fields are required.

It's also worth noting that, if you're using a recent version, you can default to the "model" view instead of the "example" by setting the defaultModelRendering config value in the rswag-ui.rb:

Rswag::Ui.configure do |c|
  c.swagger_endpoint '/api-docs/v1/swagger.json', 'API V1 Docs'
  c.config_object[:defaultModelRendering] = 'model'
end

All 2 comments

See above the JSON example, there's a tab bar that allows you to toggle between "Example Value" and "Model". If you click on the model tab, you'll see a full description of the object (as opposed to example JSON) and that should indicate which fields are required.

It's also worth noting that, if you're using a recent version, you can default to the "model" view instead of the "example" by setting the defaultModelRendering config value in the rswag-ui.rb:

Rswag::Ui.configure do |c|
  c.swagger_endpoint '/api-docs/v1/swagger.json', 'API V1 Docs'
  c.config_object[:defaultModelRendering] = 'model'
end

Fantastic, thanks. In case anyone follows me in here on this issue; if you add "required: true" to the parameter definition as below:

parameter name: :call, in: :body, required: true, schema: {...}

you will also be able to see the parent param indicated as required. Thanks @domaindrivendev !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kutomore picture Kutomore  路  3Comments

douglasrlee picture douglasrlee  路  4Comments

vittoriabitton picture vittoriabitton  路  5Comments

mcoms picture mcoms  路  4Comments

hannesstruss picture hannesstruss  路  6Comments