Repository navigation
fix(api): correct select/populate REST format docs + tests (closes #419) - #420
Merged
Merged
Conversation
PR #294 (closes #199) required API clients to declare select + populate on every read. The hook in src/lib/usage/hooks.ts is correct, but the underlying issue spec, PR tests, and in-repo docs all assumed the wrong REST query-param format (comma-separated strings instead of PayloadCMS's bracket-notation nested objects). Real clients following the documented format hit 400 "select required" because qs-esm parses the string as a plain value, failing the hook's typeof === 'object' check. Changes: - Fix misleading JSDoc example in hooks.ts (?select=title -> ?select[title]=true) - Add diagnostic WARN logging on hook rejection — emits type, top-level keys, and a 100-char string preview so production 400s are traceable from wrangler tail without leaking full payloads - Add REST-format integration tests using qs-esm + Payload's exported sanitize* utilities. Tests the full URL -> qs.parse -> sanitize -> args pipeline, including the reporter's exact failing URLs as a contract lock (populate[collection]=true and populate[collection][field]=true MUST be accepted) - Add [email protected] as devDependency (was transitive via payload) - Update .claude/rules/api-clients.md with correct/wrong format examples and a pointer to the diagnostic logging - New src/lib/openapi/clientReadParametersDocs.ts with reusable parameter definitions for select / populate / depth / limit / page, including the bracket-notation format in their descriptions - New injectClientReadParameters() in specFilter.ts that walks the spec post-merge and adds $refs to those parameters on every collection list + findByID GET (skipping x-internal ops, /api/globals/*, and custom subpath endpoints which have hand-authored param surfaces) - Update .claude/rules/openapi.md to document the new injection - Add api-explorer.int.spec.ts coverage for the parameter injection This PR does NOT change validation semantics — the hook's "presence check" logic in src/lib/usage/hooks.ts:131-174 is unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Ardnived
added a commit
that referenced
this pull request
May 28, 2026
Fixes 17 failing integration tests, all stale relative to intentional source changes that landed without updating their tests (no source changes): - API client reads (role-based-access, pages): add `depth: 0` to reads that only assert ids/access. Since #420, omitting `depth` defaults to server depth 2 and requires `populate`; these reads don't need relationship population. - wm-app-config / wemeditateAppStatus: the global now has 3 tabs (Pages/First Meditation/Misc) with 14 required pages. Rebuild the required-pages helpers from APP_REQUIRED_PAGE_FIELDS (drift-proof) and rewrite the field-structure assertions for the live layout. - translations-field: assert the #414 group-wrapped nested structure (group named after the tab slug; leaf JSON fields keep the sub-slug name). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2 of 3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #419
Summary
PR #294 shipped the
select/populatevalidation hook for API clients, but the underlying issue spec, PR tests, and in-repo docs all assumed the wrong REST query-param format — comma-separated strings instead of PayloadCMS's bracket-notation nested objects. Real clients following the documented format hit400 "select required"becauseqs-esmparses the string as a plain value, failing the hook'stypeof === 'object'check.This PR is docs + tests + observability — the hook's validation logic (
src/lib/usage/hooks.ts:131-174) is byte-for-byte unchanged.What changed
req.payload.logger.warn(...)calls added to both rejection branches in the hook. Logs type, top-level keys, and a 100-char preview when the value is a string — enough to identify malformed shapes fromwrangler tailwithout leaking full payloads.describe('REST URL format (via qs.parse + sanitize)', ...)block inclient-query-validation.int.spec.ts. Usesqs-esm+ Payload's exportedsanitizeSelectParam/sanitizePopulateParamto exercise the full URL → args parse pipeline. Includes the reporter's exact failing URLs as a contract lock (single-levelpopulate[collection]=trueand two-levelpopulate[collection][field]=trueMUST be accepted).[email protected]added as devDependency (was a transitive dep viapayload)..claude/rules/api-clients.md): new "Expected REST format" subsection with correct ✅ vs wrong ❌ URL examples, pointer to the diagnostic logging, andasTrustedReq()helper reference.src/lib/openapi/clientReadParametersDocs.tswith reusable definitions forselect/populate/depth/limit/page(each carrying the bracket-notation format in its description). NewinjectClientReadParameters()inspecFilter.tswalks the spec post-merge and adds\$refs to those parameters on every collection list + findByID GET — skippingx-internalops,/api/globals/*, and custom subpath endpoints (which have hand-authored param surfaces)..claude/rules/openapi.mdupdated to document the new injection. Test coverage inapi-explorer.int.spec.ts(describe('Client read parameter injection (#419)', ...)).src/lib/usage/hooks.ts— wrong?select=titleexample corrected to?select[title]=true.Production investigation
The reporter retested in Postman and found that bracket-notation populate URLs also 400 in production — specifically
?populate[pages][title]=trueand?populate[images]=true.I ran all three failing URLs through
qs.parse(..., { depth: 10 })+ Payload's exportedsanitizePopulateParam(the exact pipelinefindHandleruses) and every one produceshasPopulate: true— the hook should accept them. Both bracket-decoded and percent-encoded variants parse identically.This means the hook code on
mainis correct; the production rejection traces to one of:The diagnostic logging in this PR makes (2) and (3) self-diagnosing on the next failure — the WARN entry will print the offending populate shape (type, keys, short preview), and we can file a concrete follow-up ticket if a real server-side bug surfaces.
Test plan
pnpm lint— cleanpnpm exec vitest run tests/int/client-query-validation.int.spec.ts— 19/19 pass (existing 10 + 9 new REST-format cases including reporter's URLs)pnpm exec vitest run tests/int/api-explorer.int.spec.ts— 61/61 pass (existing 54 + 7 new parameter-injection cases)pnpm test— 742 passed, 6 pre-existing failures unrelated to this PR (wm-app-config.int.spec.tsandwemeditateAppStatus.int.spec.ts), confirmed reproducible onmainTest Results
tests/int/wm-app-config.int.spec.ts(5) andtests/int/wemeditateAppStatus.int.spec.ts(1) — all unrelated to this PR (verified by checking out main and reproducing)Post-deploy verification (reporter)
https://cloud.sydevelopers.com/api/openapi.json?project=wemeditate-appand confirm a collection GET (e.g. onmeditations) listsselect,populate,depth,limit,pageparameters.wrangler tail sahajcloud --format prettyfiltered by the reporter'sclientId— the new WARN log will print the actualpopulateshape the hook saw, and we open a follow-up ticket with that as evidence.🤖 Generated with Claude Code