[BUG] Typescript fetch URLEncoding for nested JSON object
Created by: tusharchutani
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