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

#1662 - Add a spec for sending single element to an array #1675

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ramkumar-kr
Copy link
Contributor

No description provided.

@grape-bot
Copy link

1 Warning
⚠️ Unless you’re refactoring existing code, please update CHANGELOG.md.
1 Message
📖 We really appreciate pull requests that demonstrate issues, even without a fix. That said, the next step is to try and fix the failing tests!

Here's an example of a CHANGELOG.md entry:

* [#1675](https://github.com/ruby-grape/grape/pull/1675): #1662 - add a spec for sending single element to an array - [@ramkumar-kr](https://github.com/ramkumar-kr).

Generated by 🚫 danger

@dblock
Copy link
Member

dblock commented Aug 10, 2017

I tried to fix this at no avail. Also tried requires :products, type: Array, coerce_with: ->(val) { Array(val) } but it gets overwritten somehow. I would focus on that one first, because it sounds that it could be an acceptable work-around.

@dblock
Copy link
Member

dblock commented Aug 10, 2017

You can work-around/hack it with a before block. This passes:

  context 'array params with XML content type' do
    let(:single_element_request) do
      '<?xml version="1.0" encoding="UTF-8" ?>
       <admin>
         <products>
           <price>100</price>
         </products>
       </admin>'
    end

    let(:multiple_element_request) do
      '<?xml version="1.0" encoding="UTF-8" ?>
       <admin>
         <products>
           <price>100</price>
         </products>
         <products>
           <price>200</price>
         </products>
       </admin>'
    end

    before do
      subject.format :xml
      subject.content_type :xml, 'application/xml; charset=utf-8'
      subject.before do
        params[:admin][:products] = [params[:admin][:products]] unless params[:admin][:products].is_a?(Array)
      end
      subject.params do
        requires :admin, type: Hash do
          requires :products, type: Array do
            requires :price, type: Integer
          end
        end
      end
      subject.post do
        params[:admin][:products].map do |product|
          { price: product[:price] }
        end
      end
    end

    context 'with one element' do
      it 'returns a successful response' do
        post '/', single_element_request, 'CONTENT_TYPE' => 'application/xml'
        expect(last_response.status).to eq(201)
        expect(MultiXml.parse(last_response.body)).to eq('objects' => [{ 'price' => 100 }])
      end
    end

    context 'with multiple elements' do
      it 'returns a successful response' do
        post '/', multiple_element_request, 'CONTENT_TYPE' => 'application/xml'
        expect(last_response.status).to eq(201)
        expect(MultiXml.parse(last_response.body)).to eq('objects' => [{ 'price' => 100 }, { 'price' => 200 }])
      end
    end
  end

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

Successfully merging this pull request may close these issues.

3 participants