redux-toolkit icon indicating copy to clipboard operation
redux-toolkit copied to clipboard

Support DeepObject query parameters in codegen

Open mattoni opened this issue 1 year ago • 2 comments

I'm attempting to describe query parameters such as /tasks?page[size]=1&page[number]=1, which is done according to the OpenAPI spec using style: deepObject and explode: true. See: https://swagger.io/docs/specification/serialization/

The codegen 'works' in that there are no errors. I'm able to pass an object in the hook: useGetTasksQuery({page: {size: 1, number: 1}})

However, it turns into this in the URL: /tasks?page=[object+Object].

Here's my open api spec for the parameter:

---
name: page
in: query
required: false
description: 'In a list return, the data associated with the page number and size returned.'
explode: true
style: deepObject
schema:
  type: object
  properties: 
    number: 
      type: number 
      description: The page to jump to
    size: 
      type: number 
      description: The number of resources returned per page.

Is this supported, and if not is it easily added?

Thanks!

mattoni avatar Aug 06 '22 15:08 mattoni

Would it be using the code in here?

https://github.com/reduxjs/redux-toolkit/blob/master/packages/toolkit/src/query/defaultSerializeQueryArgs.ts

(Not affected by the issue, just curious)

Meligy avatar Aug 07 '22 21:08 Meligy

It looks like that may be the code, yes.

EDIT: Acutally, that looks like the code to serialize the key in subscriptions, which works correctly. It's just when constructing the URL.

mattoni avatar Sep 02 '22 16:09 mattoni

defaultSerializeQueryArgs seems to be for generating cache keys. Query parameters are serialized by paramsSerializer of fetchBaseQuery:

A function that can be used to apply custom transformations to the data passed into params. If you don't provide this, params will be given directly to new URLSearchParams(). With some API integrations, you may need to leverage this to use something like the query-string library to support different array types.

mandel59 avatar Jan 15 '23 07:01 mandel59

https://github.com/reduxjs/redux-toolkit/issues/3640#issuecomment-1837894377

Grawl avatar Dec 04 '23 06:12 Grawl