OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-axios] map-like objects as query params are not serialized correctly

Open
#12,329 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

typescript-axios generator (v5.3.0, v5.3.1) is not serializing map-like query parameters as expected (...&someMapping={"key1":"value1"},{"key2":"value2"}&...)

Given:

Definition of map-like param as in Openapi spec:

- name: someMapping
  in: query        
  description: Map string2string
  schema:
    type: string
    additionalProperties: 
      type: string 
Result:

Method param is generated as ...someMapping: { [key: string]: string; }... which is ok.
Serialization to URL is done in common.ts#setSearchParams() and is producing not the expected ...&someMapping={"key1":"value1"},{"key2":"value2"}&... but the result of toString().

Remark: ...&someMapping={"key1":"value1"}&someMapping={"key2":"value2"}&... would IMHO also be a valid representation of a map but our server-side (spring, generated from the same YML) did not receive all the entries but only the first. As we switched to one URL parameter using a comma separated list of entries all the entries were put to the map.

Possible fix:

A possible fix (that is working for us) could be adding the marked block for serialization of objects into a query param (common.mustache#setSearchParams()):

export const setSearchParams = function (url: URL, ...objects: any[]) {
    const searchParams = new URLSearchParams(url.search);
    for (const object of objects) {
        for (const key in object) {
            if (Array.isArray(object[key])) {
                searchParams.delete(key);
                for (const item of object[key]) {
                    searchParams.append(key, item);
                }
// -> added
            } else if (object[key] instanceof Object) {
                let paramValues = [];
                for (const [propName, value] of Object.entries(object[key])) {
                    const paramValue: {[k:string]:string} = {};
                    paramValue[propName] = <string>value;
                    paramValues.push(JSON.stringify(paramValue));
                }
                searchParams.set(key, paramValues.join());
// <- added
            } else {
                searchParams.set(key, object[key]);
            }
        }
    }
    url.search = searchParams.toString();
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in the typescript-axios template at common.mustache#setSearchParams(), then inspect the generated common.ts serialization for object-valued query parameters. Reproduce the issue with the map-like OpenAPI query parameter shown here and verify that the generated URL preserves all entries in the expected comma-separated representation.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.