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

Malformed params creation on POST when params added to body #67

Closed
pbelouin-mpiwg opened this issue Jun 10, 2017 · 2 comments
Closed

Malformed params creation on POST when params added to body #67

pbelouin-mpiwg opened this issue Jun 10, 2017 · 2 comments

Comments

@pbelouin-mpiwg
Copy link

pbelouin-mpiwg commented Jun 10, 2017

Hello,

Thanks a lot for that gem, it's great! I have a small issue however, and cannot figure out what is going on; I have the following spec code to test a sessions controller:

  path '/api/sign_in' do
    post 'Sign In' do
      tags 'Sessions'
      produces 'application/json'
      parameter name: :credentials, in: :body, description: 'The credentials of the user', schema: {
        type: :object,
        properties: {
          email: { type: :string },
          password: { type: :string }
        },
        required: ['email', 'password']
      }
      response '200', 'User successfully logged in' do
        let(:user) { create(:user, password: 'password') }
        let(:credentials) { { email: user.email, password: 'password' } }
        run_test!
      end
    end
  end

it works as expected, however the params arrive to my app in this form:

<ActionController::Parameters {"{\"email\":\"america.braun@hanewisoky.name\",\"password\":\"password\"}"=>nil, "format"=>:json, "controller"=>"api/v1/sessions", "action"=>"create"} permitted: false>

As you can see, I end up with params in a weird form, i.e

{\"email\":\"america.braun@hanewisoky.name\",\"password\":\"password\"}"=>nil 

which makes me unable to extract them from the params to perform my sign up mechanism - what is weird is that with the generated documentation, when using the "try it" feature, the params are actually sent properly. Would you have an idea about how I could get this sorted? Thanks a lot in advance!!

This is how the params are formed when I use the "try it" feature:

<ActionController::Parameters {"email"=>"string", "password"=>"string", "format"=>:json, "controller"=>"api/v1/sessions", "action"=>"create", "session"=>{"email"=>"string", "password"=>"string"}} permitted: false>

As you can see, it's all hunky dory in this case :\

@pbelouin-mpiwg
Copy link
Author

Damn! I needed to set the "consume" value to 'application/json'! I'll close the issue but leave that here just in case somebody googles it, if that's ok.

@benebrice
Copy link

benebrice commented Mar 20, 2018

I've a similar issue but consumes is set to application/vnd.api+json (I tried application/json too). The result is quite similar to yours:

<ActionController::Parameters {"{\"data\":{\"type\":\"bank_accounts\",\"attributes\":{\"bank_id\":\"some_uuid\",\"login\":\"my_login\",\"password\":\"my_password\"}}}"=>nil, "format"=>:json, "controller"=>"bank_accounts", "action"=>"link"} permitted: false>

Here is my code. What am I missing?

path '/bank_accounts/link' do
  
    get 'Create a link between a bank and the Bank API' do
      consumes 'application/vnd.api+json'
      produces 'application/vnd.api+json'
      
      parameter name: :params,
                in: :body,
                description: 'data of the futur bank account',
                required: true,
                schema: {
                  type: :object,
                  properties: {
                    data: {
                      type: :object,
                      properties: {
                        attributes: {
                          type: :object,
                          properties: {
                            bank_id: {
                              type: :string,
                              format: 'uuid',
                              description: 'ID of a bank',
                              example: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
                            },
                            login: {
                              type: :string,
                              description: 'Login of the user to connect to the selected bank'
                            },
                            password: {
                              type: :string,
                              description: 'Password of the user to connect to the selected bank'
                            }
                          }
                        },
                        type: {
                          type: 'string',
                          description: 'bank_accounts'
                        }
                      }
                    }
                  }
                }
    
      describe 'with valid parameters' do
        response '200', 'Create a bank account linked to the bank' do
          let(:params){ { data: {type: 'bank_accounts', attributes: {bank_id: 'some-uuid',
                                                                  login: 'my_login',
                                                                   password: 'my_password'} }}}

          schema '$ref' => '#/definitions/BankAccount'
        
          run_test!
        end
end

Edit: I've switched from get to post and it's working like a charm. Thanks for your issue. It helped a lot!

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