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

[typescript-fetch] fix double parsing deep object parameters #6706

Conversation

Vrtak-CZ
Copy link

@Vrtak-CZ Vrtak-CZ commented Jun 18, 2020

Fixing double parsing query parameters for deep object.

Currently it generates something like this:

...&filter=filter%255Bstatus%255D%3Denabled%26

It's because there is double parsing one of it is what i removing and another one here:

export function querystring(params: HTTPQuery, 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 querystring(value as HTTPQuery, fullKey);
}
return `${encodeURIComponent(fullKey)}=${encodeURIComponent(String(value))}`;
})
.filter(part => part.length > 0)
.join('&');
}
but deep object can be handled by this one:
if (value instanceof Object) {
return querystring(value as HTTPQuery, fullKey);
}

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project beforehand.
  • Run the shell script ./bin/generate-samples.shto update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/config/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov

@macjohnny
Copy link
Member

this would revert #6075
@haraldF can you help here?

@haraldF
Copy link
Contributor

haraldF commented Jun 19, 2020

@macjohnny thanks for the heads-up, I'll write some unit tests to make sure the feature doesn't regress, or break because of other side effects

@haraldF
Copy link
Contributor

haraldF commented Jun 19, 2020

@Vrtak-CZ thanks for the patch, but doesn't it mean that a deepobject parameter is now never escaped after your change?

@haraldF
Copy link
Contributor

haraldF commented Jun 19, 2020

Looks like my change was done in 4.x and something must have changed in 5.x which changed behavior. I can reproduce the double escaping with today's master branch.

@macjohnny is there any way to test the generated TypeScript code? E.g. do a tsc --noEmit or something?

So here's what I got so far, a dummy yaml:

openapi: 3.0.0
info:
  title: Sample API
  description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
  version: 0.1.9
servers:
  - url: http://api.example.com/v1
    description: Optional server description, e.g. Main (production) server
  - url: http://staging-api.example.com
    description: Optional server description, e.g. Internal staging server for testing
paths:
  /users:
    get:
      summary: Returns a list of users.
      description: Optional extended description in CommonMark or HTML.
      parameters:
        - in: query
          name: someDeepObject
          style: deepObject
          required: true
          schema:
            type: object
      responses:
        '200':    # status code
          description: A JSON array of user names
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string

Unfortunately, when I apply @Vrtak-CZ 's patch, I'm getting:

apis/DefaultApi.ts:48:13 - error TS2322: Type 'Foo' is not assignable to type 'string | number | boolean | HTTPQuery | (string | number | boolean | null)[] | null'.
  Type 'Foo' is not assignable to type 'HTTPQuery'.
    Index signature is missing in type 'Foo'.

48             queryParameters['someDeepObject'] = requestParameters.someDeepObject;

e.g. we cannot just assign an object to queryParameters. When I 'hack' it to convert to any, it works.

So let's gather some opinions - fix it by extending runtime.HttpQuery or convert deep objects to any?

@Vrtak-CZ
Copy link
Author

@haraldF thanks for testing.

e.g. we cannot just assign an object to queryParameters. When I 'hack' it to convert to any, it works.

that's what we do too because of this. But I do not know better solution and also there is already handler for object inside of querystring function.

@haraldF
Copy link
Contributor

haraldF commented Jul 4, 2020

@haraldF thanks for testing.

e.g. we cannot just assign an object to queryParameters. When I 'hack' it to convert to any, it works.

that's what we do too because of this. But I do not know better solution and also there is already handler for object inside of querystring function.

Thanks for the info and sorry for the delay. I did some more digging and the as any is already fixed in d9a6f4d

@haraldF
Copy link
Contributor

haraldF commented Jul 4, 2020

@Vrtak-CZ I believe it's better to revert the entire change, also removing the unnecessary extra flag from the model. I've created a new PR: #6860

@macjohnny
Copy link
Member

closing this one in favor of #6860

@macjohnny macjohnny closed this Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants