rspec_api_documentation icon indicating copy to clipboard operation
rspec_api_documentation copied to clipboard

Fix Schema Extraction for Arrays

Open smridge opened this issue 4 years ago • 0 comments

In attempting to use response_field to generate schema properties, errors occurred when using arrays.

Error Message:

../rspec_api_documentation-6.1.0/lib/rspec_api_documentation/writers/open_api_writer.rb:147:in `block in extract_schema': undefined method `[]' for nil:NilClass (NoMethodError)

After binding.pry my way through, this solved and the 201 schema properties generated as expected. Not sure if this is something I misconfigured? However, I would think it should pass in the array (field[:value]) and not the first array element. Additional reference for method being called: https://github.com/smridge/rspec_api_documentation/blob/d3892cc7388460a98476734963face5a7a2ac158/lib/rspec_api_documentation/open_api/helper.rb#L18-L19 .

Definitely open to thoughts.

Example Setup:

# frozen_string_literal: true

require 'rails_helper'
require 'rspec_api_documentation/dsl'

describe '/v2/bazz/foos', type: :request do
  resource 'Foos' do
    header 'Content-Type', 'application/json'
    explanation 'Requests for Foos'

    # create
    post '/v2/:slug/foos' do
      with_options scope: :foo, with_example: true do
        parameter :title, required: true
        parameter :description
        parameter :bars
      end

      with_options scope: :resource do
        response_field :token, 'foo token', type: :string
        response_field :title, 'foo title', type: :string
        response_field :description, 'foo description', type: :string
        response_field :bars, 'list of foo bars', type: :array
      end

      let(:slug) { 'foobar' }

      let(:title) { 'Foo Title' }
      let(:description) { 'Some Description' }
      let(:bars) { %w[anything something] }

      context 'when valid' do
        example_request 'creates foo' do
          expect(response_status).to eq(201)
        end
      end
    end
  end
end

smridge avatar Nov 12 '20 03:11 smridge