fix(plugins): parse query params as input for GET/HEAD/DELETE routes - #2163
fix(plugins): parse query params as input for GET/HEAD/DELETE routes#2163dchaudhari7177 wants to merge 4 commits into
Conversation
Plugin route input was always read via request.json(), so a GET, HEAD, or DELETE route with an input schema always failed validation — those methods carry no body, so request.json() resolves to undefined and the Zod check rejects it (emdash-cms#2146). A new parseRouteInput helper switches on method: POST/PUT/PATCH parse the JSON body as before; GET/HEAD/DELETE parse the URL query string into an object (repeated keys become arrays). Applied to both the trusted and sandboxed plugin route dispatch paths. Fixes emdash-cms#2146
🦋 Changeset detectedLatest commit: 9a19297 The changes in this PR will be included in the next version bump. This PR includes changesets to release 16 packages
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 |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
ascorbic
left a comment
There was a problem hiding this comment.
Thanks. Can you update the docs too, because this is new behaviour
Overlapping PRsThis PR modifies files that are also changed by other open PRs:
This may cause merge conflicts or duplicated work. A maintainer will coordinate. |
oxlint --deny-warnings flagged values[0]! as an unnecessary type assertion (noUncheckedIndexedAccess is off, so values[0] is already string). Remove it; behaviour is identical.
The route dispatcher now parses bodyless-method input from the query string. Document the parsing semantics (repeated keys become arrays, single keys stay scalars, values are always strings so use z.coerce) and fix the method list to GET/HEAD/DELETE.
|
Added docs for the new behaviour in
Let me know if you'd like it anywhere else. |
|
This PR has been inactive for 14 days. It will be closed automatically in 7 days if there is no further activity. If you're still working on this, please push an update or leave a comment. |
What does this PR do?
A plugin route with an
inputschema always failed validation on GET, HEAD, and DELETE. Route input was read exclusively viarequest.json(), and those methods carry no body — sorequest.json()resolves toundefinedand the Zod check rejects every request with a 400.New
parseRouteInput(request)helper switches on method:?tag=a&tag=b) becomes an array, a single key stays a scalar; no params yields{}so all-optional/defaulted schemas still validateApplied at both plugin dispatch sites in
emdash-runtime.ts(trusted/configured plugins and the sandboxed path), replacing the duplicatedtry { request.json() }blocks.The exact repro from the issue now works:
Closes #2146
Type of change
Checklist
pnpm typecheckpasses for my change (packages/core— the one remaining error,cacheControlonManifestRouteEntryinbundle-utils.ts, is pre-existing and reproduces on a cleanmain)pnpm lintpasses for the changed files (overallpnpm lintfails on a pre-existing error inpackages/registry-verification)pnpm testpasses (targeted:packages/core tests/unit/plugins/routes.test.ts, 45/45)pnpm formathas been runemdashpatch)AI-generated code disclosure
Tests
New
parseRouteInputsuite inroutes.test.ts: GET/HEAD/DELETE query parsing, repeated-key-to-array, empty-query{}, JSON body for POST/PUT/PATCH,undefinedfor a body method with no body, and an end-to-end GET throughPluginRouteHandler.invokewith az.coerceschema returning{ limit: 20, q: "hello" }.