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

Query parameters are not getting properly encoded #617

Open
point-source opened this issue Apr 11, 2024 · 3 comments
Open

Query parameters are not getting properly encoded #617

point-source opened this issue Apr 11, 2024 · 3 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@point-source
Copy link

I am encountering an error where my generated library is not properly encoding query parameters. This may be an issue with the swagger/openapi spec but I'm not totally sure. Here is an example of a request generated by oazapfts:

export function getApiV1Rentalinventory({ pageno, pagesize, sort, filter }: {
    pageno?: number;
    pagesize?: number;
    sort?: string;
    filter?: FwStandardModelsFwQueryFilter[];
} = {}, opts?: Oazapfts.RequestOpts) {
    return oazapfts.ok(oazapfts.fetchJson<{
        status: 200;
        data: FwStandardModelsFwQueryResponseWebApiModulesInventoryRentalInventoryRentalInventoryLogicRead;
    } | {
        status: 400;
        data: FwCoreApiSwashbuckleBadRequestResponse;
    } | {
        status: 401;
    } | {
        status: 403;
    } | {
        status: 500;
        data: FwStandardModelsFwApiException;
    }>(`/api/v1/rentalinventory${QS.query(QS.explode({
        pageno,
        pagesize,
        sort,
        filter
    }))}`, {
        ...opts
    }));
}

Specifically, the failure is occurring on the filter model (FwStandardModelsFwQueryFilter):

export type FwStandardModelsFwQueryFilter = {
    /** The DataField name to filter on. */
    Field: string;
    /** Comparison operator: = (equals), <> (not equals) */
    Op: string;
    /** The value of the DataField to filter on. */
    Value?: string | null;
};

Here is the problem:

const filter = [{
    "Field": "ICode",
    "Op": "=",
    "Value": "11754"
}];

// This fails with "Bad Request" from the server
// Returns: ?filter=Field,ICode,Op,%3D,Value,11754
console.log(QS.query(QS.explode({ filter: filter })));

// This succeeds
// Returns: ?filter=%5B%7B%22Field%22%3A%22ICode%22%2C%22Op%22%3A%22%3D%22%2C%22Value%22%3A%2211754%22%7D%5D
console.log(QS.query(QS.explode({ filter: JSON.stringify(filter) })));

So it would seem that "filter" should be stringified before encoding. Unfortunately, when I actually try to do it this way in the function call, it causes a typescript warning that Type 'string' is not assignable to type 'FwStandardModelsFwQueryFilter[]' which is technically correct because the function wants to see that type of object, not a pre-stringified string.

What is the right way to work around this?

@Xiphe
Copy link
Collaborator

Xiphe commented Apr 23, 2024

Hi @point-source thanks for bringing this up.

In general the parameter encoding is (unfortunately) currently not fully compliant with the spec. I have this somewhere on my radar and have tried to tackle this in the past but until now wasn't able to come up with a solution better then what we currently have.

It might very well be that you're encountering an edge-case here that this library should™ handle better.

In order to bring this forward, could you share the swagger-file / oas-spec file you use to generate the code as well as an example of what you'd expect the codegen to produce?

@Xiphe
Copy link
Collaborator

Xiphe commented Apr 23, 2024

ref #628

@point-source
Copy link
Author

point-source commented Apr 23, 2024

Thanks for getting back to me! The swagger file comes from here:

https://demo.rentalworksweb.com/swagger/home-v1/swagger.json

@Xiphe Xiphe added bug Something isn't working help wanted Extra attention is needed labels Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants