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

Array values cast to string when length of array is 1 #1

Closed
ndianabasi opened this issue Jul 12, 2022 · 4 comments
Closed

Array values cast to string when length of array is 1 #1

ndianabasi opened this issue Jul 12, 2022 · 4 comments

Comments

@ndianabasi
Copy link

ndianabasi commented Jul 12, 2022

Description

I noticed that when an array value is assigned to a field and the length of the array is 1, the value is cast to a string.

For example:

Within a test, userRoles is an array

      const userRoles = [globalAdminRole.id]

      const response = await client
        .post(route('global_users.store'))
        .loginAs(loginUser!)
        .field('full_name', userAttributes.fullName)
        .field('email', userAttributes.email)
        .field('role_ids', userRoles)

but in the controller, it is received as:

{
  full_name: 'Gardner Johnson',
  email: 'Gardner.Johnson@ca.com',
  role_ids: '1'
}

However, if the length of the array is more than 1, an array is received in the controller.

      const userRoles = [globalAdminRole.id, '2']

      const response = await client
        .post(route('global_users.store'))
        .loginAs(loginUser!)
        .field('full_name', userAttributes.fullName)
        .field('email', userAttributes.email)
        .field('role_ids', userRoles)
{
  full_name: 'Gardner Johnson',
  email: 'Gardner.Johnson@ca.com',
  role_ids: [ '1', '2' ]
}

Package version

"@adonisjs/core": "^5.8.3",
"@japa/api-client": "^1.4.0",
"@japa/assert": "^1.3.4",
"@japa/preset-adonis": "^1.1.0",
"@japa/run-failed-tests": "^1.0.7",
"@japa/runner": "^2.0.9",

Relevant Information

I'm also not certain if this behaviour is caused by Adonisjs Bodyparser or the Japa's API client. But I want to believe that this kind of behaviour won't occur within the Bodyparser middleware.

@thetutlage
Copy link
Contributor

Short answer
You need to change the field name to have square brackets role_ids[].

Long answer
Everything on HTTP is a string. So the value that is array in your test gets converted to a string when the request is made over HTTP.

There is no way for the server to know that you have sent an array, therefore the field name has to indicate that by suffixing the brackets.

However, when you send multiple values, they turn out to be like this in the payload role_ids=1&role_ids=2. And multiple occurrence of role_ids converts it to an array.

@ndianabasi
Copy link
Author

Thank you for the explanation. Would you like this behaviour to be documented in the API client section?

@thetutlage
Copy link
Contributor

Not sure. Coz, it is more of an HTTP thing. If you make a call using HTML forms, or Insomnia, then the behavior will be the same. So it is not specific to the API client.

@ndianabasi
Copy link
Author

Okay. Thanks.

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