OpenAPITools / OpenAPITools/openapi-generator

[BUG] Typescript fetch URLEncoding for nested JSON object

Open
#9,070 0 comments 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
Description

We have a requirement for passing in a JSON object in a GET url as query string. Currently if we don't provide an option of queryParamsStringify url for a nested query object such as this

{
  "name":"Jordan",
    "address": {
      	"city":"NY",
	    "state":"NY",
    	"house":1002,
	    "st": "wall st"
    }
}

the queryParamsStringify will return this
name=Jordan&address[city]=NY&address[state]=NY&address[house]=1002&address[st]=wall st

Now I am not sure if this is a desired result. I am under the impression that address json object should be percent-encoded

and response should be

%7B%22name%22%3A%22Jordan%22%2C%22address%22%3A%7B%22city%22%3A%22NY%22%2C%22state%22%3A%22NY%22%2C%22house%22%3A1002%2C%22st%22%3A%22wall+st%22%7D%7D
openapi-generator version

2.2.0

Suggest a fix

We can change the runtime.ts in our client

export default function querystring(params: any, prefix: string = ''): string {
    return Object.keys(params)
        .map((key) => {
            const fullKey = prefix + (prefix.length ? `[${key}]` : key);
            const value = params[key];
            if (value instanceof Array) {
                const multiValue = value.map(singleValue => encodeURIComponent(String(singleValue)))
                    .join(`&${encodeURIComponent(fullKey)}=`);
                return `${encodeURIComponent(fullKey)}=${multiValue}`;
            }
            if (value instanceof Object) {
                return `${encodeURIComponent(fullKey)}=${encodeURIComponent(JSON.stringify(value))}`;;
            }
            return `${encodeURIComponent(fullKey)}=${encodeURIComponent(String(value))}`;
        })
        .filter(part => part.length > 0)
        .join('&');
}

I can make a PR to change the said file

File: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache#L223

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 with modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache around the querystring function at line 223. Review how nested objects are currently serialized and compare it with the requested encoding behavior; done means the generated TypeScript fetch client's nested query parameters follow the agreed 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
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.