question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[typescript-axios] doesn't serialize deepObject query params

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What’s the actual output vs expected output? input: {day: 20, year: {min: 1990, max: 2000}} actual output: ?day=20&year expected output: ?day=20&year[min]=1990&year[max]=2000
Description

I’m using [typescript-axios] api client generator. Some of the properties I pass to it specified as deepObject. It looks that it doesn’t support this type of args at all. Is there any possible ways to fix it?

openapi-generator version

v4.3.1 v5.0.0-beta

OpenAPI declaration file content or url
YearRange:
      in: query
      name: year
      required: false
      style: deepObject
      schema:
        type: object
        properties:
          min:
            type: string
            example: 2010
          max:
            type: string
            example: 2019
Generation Details

[typescript-axios] generator

Steps to reproduce
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/pull/6075

Suggest a fix

Replace old deprecated url API with URLSearchParams.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

2reactions
wamujlbcommented, Feb 16, 2021

I have made a workaround but I think that it will be good to add it to the generator.

export const setSearchParams = function (url: URL, ...objects: any[]) {
    const searchParams = new URLSearchParams(url.search);
    for (const object of objects) {
        for (const key in object) {
            /**
             * @workaround
             * Convert object value to the string
             * {sort: {date: 'desc', order: 'asc'}} => 'sort[date]=desc&sort[order]=asc'
             */
            const value = object[key];

            if(typeof value === 'object'){
                Object.entries(value).forEach(([entryKey, entryValue]) => {
                    searchParams.set(`${key}[${entryKey}]`, String(entryValue));
                })
            } else {
                searchParams.set(key, value);
            }
        }
    }
    url.search = searchParams.toString();
}
1reaction
talss89commented, May 29, 2022

I’ve just had a similar problem. Loopback 4 expects filter parameters as serialised JSON.

It can be solved in a ‘nice’ way using template overrides.

I modified the template to serialise any object passed as a parameter to JSON:

if(typeof {{paramName}} === 'object' && {{paramName}} !== null)
    localVarQueryParameter['{{baseName}}'] = JSON.stringify({{paramName}});
else
    localVarQueryParameter['{{baseName}}'] = {{paramName}};

@wamujlb - In your case, you could probably add your code to the template.

Read more comments on GitHub >

github_iconTop Results From Across the Web

swagger-codegen wont generate query parameter objects
1) Your query parameter is defined using content.application-json , which means the parameter is serialized as a JSON string i.e. /plans?filter ...
Read more >
serialize-query-params - npm
A library for simplifying encoding and decoding URL query parameters.. Latest version: 2.0.2, last published: 10 days ago.
Read more >
Parameter Serialization - Swagger
Parameter Serialization · style defines how multiple values are delimited. Possible styles depend on the parameter location – path, query, header or cookie....
Read more >
OpenAPI JSON Objects as Query Parameters - Baeldung
OpenAPI 2 doesn't support objects as query parameters; only primitive values and arrays of primitives are supported.
Read more >
Does Stoplight support query parameters?
... Stoplight supports query params but not all serialization options. This means that common ways of doing query params, like deepObject, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found