Skip to content

Optional "parameters" field (#1335) breaks header components #1340

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

Closed
1 task done
denisw opened this issue Sep 13, 2023 · 10 comments
Closed
1 task done

Optional "parameters" field (#1335) breaks header components #1340

denisw opened this issue Sep 13, 2023 · 10 comments
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library

Comments

@denisw
Copy link

denisw commented Sep 13, 2023

Description

The change in #1335 that makes the parameters property optional if all parameters are optional (thanks!) seems to not work with reusable header definitions in the components section. Trying to use a header component leads to the following type error:

error TS2339: Property 'example' does not exist on type '{ example?: string | undefined; } | undefined'.

1382         "Example"?: components["parameters"]["example"];

Reproduction

Create an OpenAPI file which:

  • defines a header in the components.header section
  • refers to that header in the parameters of an operation

Checklist

@denisw denisw added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Sep 13, 2023
@duncanbeevers
Copy link
Contributor

I suggest reverting #1335; it appears it wasn't sufficiently tested, breaking already-working production code.
This is definitely not a trivial fix.

@drwpow
Copy link
Contributor

drwpow commented Sep 20, 2023

We can revert it, but I’m not sure if we should publish a new version just for a rollback, since that change was fairly recent. The only patch release since v6.6.0 was a discriminator edge case that hasn’t been affecting anybody as far as I know.

But if we have another fix incoming before this issue, I can rollback #1335

@denisw
Copy link
Author

denisw commented Sep 21, 2023

Now that I looked more closely at #1335, the issue is that it addresses the wrong parameters object. In #1334, I was talking about the parameters of an operation object, while the "fix" changes the components object (where the optional parameters does not help, and introduces issues as seen here).

@drwpow
Copy link
Contributor

drwpow commented Sep 23, 2023

Ah good catch @denisw—you’re right. No direct child of the components object should be potentially optional. 🤔 I’m surprised more tests didn’t throw, but I guess this just slipped through the current coverage.

@drwpow
Copy link
Contributor

drwpow commented Sep 23, 2023

I’m going to attempt another fix to this; maybe we still see issues. But I think it’s an improvement? Happy to ship another patch or just revert this entirely if not.

@drwpow
Copy link
Contributor

drwpow commented Sep 23, 2023

Also this is offtopic, but one thing I’m excited about for the v7 rewrite is what an AST lets you do:

const allOptional = paramLocType.every((node) => !!node.questionToken);

In other words, after you’ve built the AST, you can go back through and inspect what got built. Which… I know I’m just describing how ASTs work but in the context of this library I hadn’t considered how much that’d come in handy

@MellKam
Copy link

MellKam commented Sep 25, 2023

I have also encountered this problem, but in my case it is parameters.query type. Here is example

UsersController_get: {
  parameters?: {
    query?: {
      tier_id?: string;
      patreon_username?: string;
      discord_username?: string;
      page?: number;
      limit?: number;
    };
  };
  responses: { ... }
};

I am trying to access the query type, but I am getting ts error

type GetPaginatedUsersQuery =
	operations["UsersController_get"]["parameters"]["query"]
	
// Property 'query' does not exist on type '{ query?: { tier_id?: string | undefined; 
// patreon_username?: string | undefined; discord_username?: string | undefined; page?: 
// number | undefined; limit?: number | undefined; } | undefined; } | undefined'.

This worked fine in previous versions because the parameter was not an optional type.

@drwpow
Copy link
Contributor

drwpow commented Sep 25, 2023

Yeah this may just need to be reverted. Optional objects are generally pretty friendly to use, but optional nested objects are not. Will revert the top-level parameters being optional and bump a minor version.

@drwpow
Copy link
Contributor

drwpow commented Sep 25, 2023

The upcoming v6.7.0 will revert to previous behavior where parameters is never optional, but query/header/cookie may be if they have no required params (path params are always required by their nature).

@drwpow
Copy link
Contributor

drwpow commented Jun 24, 2024

I believe this issue has been fixed both in the latest version of 6.x and 7.x but please let me know if anyone else is encountering an issue.

@drwpow drwpow closed this as completed Jun 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 openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

No branches or pull requests

4 participants