Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Typescript fetch URLEncoding for nested JSON object #9070

Open
1 task done
tusharchutani opened this issue Mar 24, 2021 · 0 comments
Open
1 task done

[BUG] Typescript fetch URLEncoding for nested JSON object #9070

tusharchutani opened this issue Mar 24, 2021 · 0 comments

Comments

@tusharchutani
Copy link

tusharchutani commented Mar 24, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant