Skip to content

Make params required even if all params are optional #1022

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

Merged
merged 1 commit into from
Feb 23, 2023

Conversation

sgrimm
Copy link
Contributor

@sgrimm sgrimm commented Jan 4, 2023

In 5.x, an optional query parameter's type could be referenced with an
expression such as

type myQueryType = operations['myOperation']['parameters']['query']

In 6.0, that only works if there is at least one mandatory query
parameter in addition to the optional one. This makes query parameter
types much more awkward to reference in application code if all the
parameters are optional.

Revert to the 5.x behavior: if there are any parameters, optional or
not, the parameters object is always present. If there are any query
parameters, the query object is always present, same with path and
the other parameter types.

Changes

Fixes #995

How to Review

The actual code change here is trivial (the vast majority of changed lines
are changes to the test cases). But per the discussion on #995, it's not
entirely clear why this behavior changed between 5.x and 6.0. Reverting to
the 5.x behavior will restore compatibility with existing application code,
but it may break code that relied on the 6.0 behavior depending on how that
code was constructing its type references.

Checklist

  • Unit tests updated
  • README updated - no README changes needed
  • examples/ directory updated (if applicable)

In 5.x, an optional query parameter's type could be referenced with an
expression such as

    type myQueryType = operations['myOperation']['parameters']['query']

In 6.0, that only works if there is at least one mandatory query
parameter in addition to the optional one. This makes query parameter
types much more awkward to reference in application code if all the
parameters are optional.

Revert to the 5.x behavior: if there are any parameters, optional or
not, the `parameters` object is always present. If there are any query
parameters, the `query` object is always present, same with `path` and
the other parameter types.

Fixes openapi-ts#995
@sgrimm sgrimm changed the title Make params non-nullable even if all params are optional Make params required even if all params are optional Jan 4, 2023
@mkosir
Copy link

mkosir commented Jan 25, 2023

Hey @drwpow thanks for this awesome library.
Any thoughts on this PR, if it can be merged as @sgrimm suggested, since this issues is quite blocking for many of us?

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I agree with this change.

@drwpow drwpow merged commit 5558223 into openapi-ts:main Feb 23, 2023
@jmwalsh91
Copy link

I know this has made it's way into main already, but I just wanted to thank @sgrimm for this from the bottom of my heart.

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.

Property 'query' does not exist on type
4 participants