Skip to content

rtk-query-codegen-openapi - TypeError: Cannot read properties of undefined - query queryArg params undefined - query parameters should be optional, not required #5018

@kdevan

Description

@kdevan

When the hook is called like this: useGetAnalyticsSourcesQuery()

And parameters in the Open API specification look like this:

"parameters": [
    {
        "name": "sort",
        "in": "query",
        "description": "Available sorts are `display_name`. You can sort by multiple options by separating them with a comma. To sort in descending order, use `-` sign in front of the sort, for example: `-display_name`.",
        "schema": {
            "type": "string",
            "default": "display_name"
        }
    },
    {
        "name": "fields",
        "in": "query",
        "description": "Available fields are `display_name`. You can include multiple options by separating them with a comma.",
        "schema": {
            "type": "string"
        }
    }
],

The generated output in the javascript client looks like this:

    getAnalyticsSources: build.query({
      query: queryArg => ({
        url: `/themes/manage/analytics/sources`,
        params: {
          sort: queryArg.sort,
          fields: queryArg.fields
        }
      })
    }),

An error shows up in console like this: TypeError: Cannot read properties of undefined (reading 'sort').

If I call the hook like this: useGetAnalyticsSourcesQuery({'sort': '', 'filter': ''}) then the TypeError does not show up and it works.

It seems that every query parameter must be included with the hook but for endpoints with many query parameters this becomes burdensome. Is there a way to make all query parameters optional in the generated output?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions