orval icon indicating copy to clipboard operation
orval copied to clipboard

fix(fetch): properly append array values to `normalizedParams` object

Open n2k3 opened this issue 1 year ago • 5 comments

Status

WIP

Description

I experienced a problem with array values. For example I want to send this: Foo: [0, 1] this will be added by this line of code to the URLSearchParams instance, which in turn will by stringified to Foo=0,1 and appended to the API url in generated code.

Shouldn't this be done according to this example in the MDN docs? Resulting in Foo=0&Foo=1 as stringified value.

This PR addresses that. Let me know if I need to anything else within this PR.

Thanks @soartec-lab for the great work on the fetch client 🎉

Related PRs

List related PRs against other branches:

  • https://github.com/orval-labs/orval/issues/1387

Todos

  • [ ] Tests
  • [ ] Documentation
  • [ ] Changelog Entry (unreleased)

n2k3 avatar Aug 16 '24 10:08 n2k3

Looks like the build is failing?

melloware avatar Aug 16 '24 12:08 melloware

Looks like the build is failing?

I can now reproduce locally, I'm working on a fix 👍

n2k3 avatar Aug 19 '24 13:08 n2k3

Thanks! I'm wondering if I should make it optional and selectable. Because this is currently working.

I've found the cause of the error, it's because the strong typed pet store example /samples/**/models/listPetsParams.ts doesn't contain any parameters that have an array type. In the generated example it only is

export type ListPetsParams = {
  /**
   * How many items to return at one time (max 100)
   */
  limit?: string;
};

this is based on the /samples/**/petstore.yaml

      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: string

The error claims that the code value instanceof Array is never true, because it is inferred that value can only be of type string not of any kind of Array

So then the proposed change of the PR would have to be dynamically included based on whether the type is an array or not. I haven't figured out how / where to do this best. If you have any idea on how to do this properly, don't hesitate to do so 😉

n2k3 avatar Aug 22 '24 11:08 n2k3

This means changing the conditions for constructing query parameters depending on the type of OpenAPI, right? I will also look for ways to change it 👍

soartec-lab avatar Aug 22 '24 11:08 soartec-lab

Hi, @n2k3 I looked into this. A reasonable way to accomplish this is to parse the generated query params string and verify whether it contains an array. Also, consider cases where there are only array-type fields.

soartec-lab avatar Aug 28 '24 00:08 soartec-lab

@n2k3 I created #1604 based on this PR. Does control with explode property solve your problem?

soartec-lab avatar Aug 31 '24 01:08 soartec-lab

I'll close this PR. Because, this issue fixed by #1604.

soartec-lab avatar Sep 01 '24 08:09 soartec-lab