Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jsmestad opened this issue Dec 5, 2014 · 0 comments

Comments

@jsmestad
Copy link
Contributor

jsmestad commented Dec 5, 2014

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
@jsmestad jsmestad changed the title config.post_body_formatter = :json and no params causes error with example_request config.post_body_formatter = :json and no params causes bad payload with example_request Dec 5, 2014
@oestrich oestrich closed this as completed Dec 9, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants