data icon indicating copy to clipboard operation
data copied to clipboard

Customize serialization of query parameter lists

Open hb9hnt opened this issue 5 years ago • 3 comments

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: https://github.com/django-json-api/django-rest-framework-json-api/issues/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.

hb9hnt avatar Oct 03 '20 14:10 hb9hnt

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

cah-brian-gantzler avatar Sep 09 '22 19:09 cah-brian-gantzler

@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.

runspired avatar Sep 13 '22 16:09 runspired

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

cah-brian-gantzler avatar Sep 13 '22 16:09 cah-brian-gantzler

@cah-brian-gantzler eventually is more or less come :) https://github.com/emberjs/rfcs/pull/860

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

runspired avatar Nov 17 '22 02:11 runspired

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

cah-brian-gantzler avatar Nov 17 '22 13:11 cah-brian-gantzler