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

Customize serialization of query parameter lists #7345

Closed
hb9hnt opened this issue Oct 3, 2020 · 5 comments
Closed

Customize serialization of query parameter lists #7345

hb9hnt opened this issue Oct 3, 2020 · 5 comments

Comments

@hb9hnt
Copy link

hb9hnt commented Oct 3, 2020

Background

If we specify a list as query parameters in the query method of the RESTAdapter (or with the JSONAPIAdapter as it inherits the query method), such as

this.store.query('book', {"filter: {"authors": [1,2]}})

we have a situation, which is not specified in the standard defined on jsonapi.org.

Situation in Ember Data (RESTAdapter and JSONAPIAdapter)

To serialize the query parameters we use a function, which was copied from jQuery and therefore get the same result as with jquery:

book?filter[authors][]=1&filter[authors][]=2

Source: https://github.com/emberjs/data/blob/v3.21.0/packages/adapter/addon/-private/utils/serialize-query-params.ts#L11

Since it is all happening in private methods, this behaviour cannot be easily changed for a backend which behaves differently - such as Django Rest Framework Json API.

Situation in Django DJA

Django Rest Framework JSON Api (the Django implementation of jsonapi.org) decided to implement query parameter lists as repeated query parameter like this:

book?filter[authors]=1&filter[authors]=2

Source: django-json-api/django-rest-framework-json-api#718

Solutions

There should definitely be a way to easily change this behaviour to adapt it to different back ends, especially since it is not defined in the standard.

I'm not sure how this can be resolved. Maybe the function serializeQueryParams(queryParamsObject) could be made a public method on the RESTAdapter.

@cah-brian-gantzler
Copy link

cah-brian-gantzler commented Sep 9, 2022

Bump, just ran into this need. While mine isnt nested, I get the following

this.store.query('book', {"authors": [1,2]})
book?authors[]=1&authors[]=2

and spring java wants

book?authors=1&authors=2
or
book?authors=1,2

@runspired
Copy link
Contributor

@cah-brian-gantzler it's unlikely we ever consider this. Those adapters have too many poorly designed hooks and methods as it is. It is much simpler to implement your own adapter.

Eventually we will provide url-building util functions, even in that case the answer wouldn't be a hook but to write your own function.

@cah-brian-gantzler
Copy link

I believe when I looked into this the functions that performed this was so way down or in utils that could not be overridden. If I could have overridden a method on the base adapter I would have. Im sorry I dont remember where I saw the code needed to be changed, but I dont think writing my own adapter would solve the problem either, I think it was in the build url utilities.

Any idea on the time frame of eventually 😄

Thanks for your consideration

@runspired
Copy link
Contributor

@cah-brian-gantzler eventually is more or less come :) emberjs/rfcs#860

My goal is to land the implementation of this in the next 2 weeks.

@cah-brian-gantzler
Copy link

Thanks for the update. My current endpoint is only being called manually until I could figure out what to do. Looking forward to it. Please keep me informed

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

3 participants