Skip to content

config.post_body_formatter = :json and no params causes bad payload with example_request #183

Closed
@jsmestad

Description

@jsmestad

The following example will cause a failure due to sending parameters as:

Processing by Api::PostsController#show as application/json
  Parameters: {"{}"=>nil, "id"=>"0ae45670-7beb-4ff5-aac8-3df1dd4ce601"}
RspecApiDocumentation.configure do |config|
  config.post_body_formatter = :json
end

resource 'Post' do
  delete '/posts/:id' do
    let(:id) { '0ae45670-7beb-4ff5-aac8-3df1dd4ce601' }

    example_request 'delete a post' do
      expect(status).to eql(204)
    end
  end
end

This does not occur when using example alongside do_request, only with example_request due to setting the params as default to {}.


Where this happens

I dug into this, and the {"{}"=>nil} portion occurs all the way down in Rack::Utils.parse_query.

irb(main):002:0> require 'rack'
=> true
irb(main):003:0> Rack::Utils.parse_nested_query("{}")
=> {"{}"=>nil}
irb(main):004:0> Rack::Utils.parse_query("{}")
=> {"{}"=>nil}

Workaround

    def do_request(extra_params = {})
      @extra_params = extra_params

      params_or_body = nil
      path_or_query = path

      if method == :get && !query_string.blank?
        path_or_query += "?#{query_string}"
      else
        if respond_to?(:raw_post)
          params_or_body = raw_post
        elsif !params.blank? # NOTE: workaround for Rack::Utils.normalize_params bug
          formatter = RspecApiDocumentation.configuration.post_body_formatter
          case formatter
          when :json
            params_or_body = params.to_json
          when :xml
            params_or_body = params.to_xml
          when Proc
            params_or_body = formatter.call(params)
          else
            params_or_body = params
          end
        end
      end

      rspec_api_documentation_client.send(method, path_or_query, params_or_body, headers)
    end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions