Skip to content

fix(openapi-react-query): reject undeclared query and path parameters - #2865

Open
isaacriehm wants to merge 1 commit into
openapi-ts:mainfrom
isaacriehm:fix/exact-query-params
Open

fix(openapi-react-query): reject undeclared query and path parameters#2865
isaacriehm wants to merge 1 commit into
openapi-ts:mainfrom
isaacriehm:fix/exact-query-params

Conversation

@isaacriehm

Copy link
Copy Markdown

Changes

Fixes #2864.

An undeclared query or path parameter type-checked and was sent, as long as the object also held
one declared parameter. Init is inferred from the init argument, so the parameter type is
derived from the argument itself and nothing can be excess against it.

The init argument is now contextually typed by the operation's own init
(MaybeOptionalInit<Paths[Path], Method>) instead of by the inferred Init, which makes the
undeclared key ordinary excess-property checking:

Object literal may only specify known properties, and 'undeclared' does not exist in
type '{ string?: string; number?: number; boolean?: boolean; array?: string[]; ... }'

parseAs is the only part of init the response type depends on (FetchResponse reads it through
ParseAsResponse), so it is now inferred on its own type parameter rather than by inferring the
whole init. ParseAsOpt defaults to never, which ParseAsInit maps to unknown so the
no-parseAs case still resolves to the JSON response. It is declared last because a defaulted
type parameter cannot precede non-defaulted ones.

Init is left in place on UseMutationMethod, where it is still the inferred mutate variable
type. The four query-side signatures no longer need it, so RequiredKeysOf now reads the
declared init directly, which is the same value it resolved to before.

Runtime is untouched: rebuilding released 0.5.4 with this change emits index.mjs and
index.cjs byte-identical to the published files.

How to Review

pnpm run lint:ts is the real gate. The @ts-expect-error assertions are the test.

  • Two assertions added to queryOptions > has correct parameter types (undeclared query param,
    undeclared path param). Reverting src/index.ts turns both into
    TS2578: Unused '@ts-expect-error' directive, so they do test this change.
  • A new infers the response type from parseAs test asserts parseAs: "blob" still yields Blob
    and the default still yields string[]. That case was uncovered, and it is the one that fails
    under the obvious alternative fix: NoInfer on the init parameter passes the entire existing
    suite while silently widening every parseAs response back to the default. The same NoInfer
    change in openapi-fetch's InitParam fails its own test/common/response.test.ts on this.
  • pnpm run test:js 38 passed, pnpm run lint:js clean, pnpm run build succeeds.
  • Also exercised against a private 592-operation schema (~500 useQuery/queryOptions call
    sites): one real 400-producing bug found, no legitimate call shape regressed.

Behaviour change worth a changelog note: code currently passing undeclared parameters stops
compiling. Extra top-level init keys stay legal, since InitWithUnknowns is unchanged, so this does
not address #2642.

Checklist

  • Unit tests updated
  • docs/ updated (if necessary): no documented behaviour changes
  • pnpm run update:examples run (only applicable for openapi-typescript)

Changeset: .changeset/exact-query-params.md, openapi-react-query: patch.

Init was inferred from the init argument, so the init parameter type was
derived from the argument itself and no property could be excess. The
operation's shape only participated as a constraint, which is ordinary
assignability.

Contextually type init by the operation's own init instead. parseAs is the
only part of init the response type reads, so it is inferred on its own type
parameter rather than by inferring the whole init.
@isaacriehm
isaacriehm requested a review from a team as a code owner September 6, 2026 17:14
@isaacriehm
isaacriehm requested a review from gzm0 September 6, 2026 17:14
@netlify

netlify Bot commented Sep 6, 2026

Copy link
Copy Markdown

👷 Deploy request for openapi-ts pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 97ec99f

@changeset-bot

changeset-bot Bot commented Sep 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 97ec99f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openapi-react-query Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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.

Query and path parameters the schema does not declare are accepted without a type error

1 participant