Commit 7dfea5e
Ehindero Israel
fix(server): allow omitted optional memory query params (#15969)
This fixes a memory query validation regression that shows up when the
runtime resolves Zod 4.4.0 or newer. Zod 4.4.0 tightened
missing-key/undefined behavior for object properties, which is called
out in the release notes:
https://github.com/colinhacks/zod/releases/tag/v4.4.0
Our memory schemas were expressing optional preprocessed query fields as
`z.preprocess(fn, inner.optional())`. With newer Zod, omitted params
like `orderBy`, `metadata`, `include`, `filter`, and
`includeSystemReminders` can then fail with `expected nonoptional,
received undefined`, which breaks memory thread/message loading in
Studio and the underlying API routes.
This changes those fields to the safer equivalent `z.preprocess(fn,
inner).optional()` and keeps the preprocess functions passing through
`undefined`. That preserves the intended behavior: omitted query params
stay optional, but present JSON values still get parsed and validated.
Before:
```ts
z.preprocess(fn, inner.optional())
```
After:
```ts
z.preprocess(fn, inner).optional()
```
I also added regression coverage for the exact omitted-parameter cases
on `listThreadsQuerySchema` and `listMessagesQuerySchema`.
I verified this in the `apr-30` demo by linking the local
`@mastra/server` package and hitting the same routes that were
previously 400ing. After the change, both `/api/memory/threads` and
`/api/memory/threads/:threadId/messages` return normal JSON again
instead of validation errors.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## ELI5 (Explain Like I'm 5)
When you ask an API for information without providing some optional
details, the server was incorrectly rejecting your request as invalid
(because of a newer version of the validation tool it uses). This PR
fixes that by reorganizing how the validation rules work so that
optional details are actually treated as optional—if you don't send
them, that's perfectly fine.
## Changes Overview
This PR fixes a validation regression in the memory query API endpoints
that was introduced by Zod 4.4.0+. The issue occurred when optional
query parameters were omitted from requests to `/api/memory/threads` and
`/api/memory/threads/:threadId/messages`, causing validation errors with
"expected nonoptional, received undefined" even though those parameters
were intended to be optional.
## Root Cause
The memory query schemas were using `z.preprocess(fn,
schema.optional())`, which caused Zod 4.4.0+ to reject `undefined`
values during the preprocessing step. The stricter validation in newer
Zod versions exposed this improper ordering of the optional modifier,
preventing the preprocessor from handling undefined values before the
optional check.
## Solution
Refactored multiple Zod query schemas to move optionality outside the
preprocess call, changing from `z.preprocess(fn, schema.optional())` to
`z.preprocess(fn, schema).optional()`. This ensures that:
- Omitted query parameters (orderBy, metadata, include, filter,
includeSystemReminders, resourceId) pass through preprocessing without
triggering validation errors, and remain `undefined` in the parsed
result
- Present JSON values are still properly parsed and validated through
the preprocess functions
- The intended optional behavior is preserved for backward compatibility
## Files Modified
- **packages/server/src/server/schemas/memory.ts**: Refactored
preprocess callbacks in `storageOrderBySchema`, `messageOrderBySchema`,
`includeSchema`, `filterSchema`, `memoryConfigSchema`, and related
metadata/system reminder fields to use the corrected pattern (+122/-103
lines)
- **packages/server/src/server/schemas/memory.test.ts**: Added
regression tests verifying that `listThreadsQuerySchema` and
`listMessagesQuerySchema` successfully parse when optional query
parameters are omitted, with those parameters remaining `undefined` in
the result (+28 lines)
- **.changeset/four-owls-flow.md**: Patch-level version bump for
@mastra/server (+5 lines)
## Testing & Verification
- Added regression test coverage confirming that omitted optional
parameters (`resourceId`, `orderBy`, `metadata`, `include`, `filter`)
don't cause validation errors and correctly resolve to `undefined`
- Verified locally by linking the @mastra/server package and confirming
`/api/memory/threads` and `/api/memory/threads/:threadId/messages`
return normal JSON responses instead of 400 validation errors
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent 030e805 commit 7dfea5e
3 files changed
Lines changed: 155 additions & 103 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
17 | 32 | | |
18 | 33 | | |
19 | 34 | | |
| |||
251 | 266 | | |
252 | 267 | | |
253 | 268 | | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
254 | 282 | | |
255 | 283 | | |
256 | 284 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
35 | 38 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | | - | |
44 | | - | |
45 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
58 | 62 | | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
64 | 66 | | |
65 | 67 | | |
66 | | - | |
67 | | - | |
68 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
69 | 71 | | |
70 | 72 | | |
71 | 73 | | |
72 | 74 | | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
81 | 86 | | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
87 | 90 | | |
88 | 91 | | |
89 | 92 | | |
90 | 93 | | |
91 | 94 | | |
92 | 95 | | |
93 | | - | |
94 | | - | |
95 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
96 | 99 | | |
97 | 100 | | |
98 | 101 | | |
99 | 102 | | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
108 | 114 | | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
114 | 118 | | |
115 | 119 | | |
116 | 120 | | |
| |||
120 | 124 | | |
121 | 125 | | |
122 | 126 | | |
123 | | - | |
124 | | - | |
125 | | - | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
126 | 130 | | |
127 | 131 | | |
128 | 132 | | |
129 | 133 | | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
136 | 146 | | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
141 | 151 | | |
142 | 152 | | |
143 | 153 | | |
| |||
190 | 200 | | |
191 | 201 | | |
192 | 202 | | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
201 | 214 | | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
207 | 220 | | |
208 | 221 | | |
209 | 222 | | |
| |||
226 | 239 | | |
227 | 240 | | |
228 | 241 | | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
234 | 250 | | |
235 | 251 | | |
236 | 252 | | |
| |||
278 | 294 | | |
279 | 295 | | |
280 | 296 | | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
289 | 308 | | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
295 | 314 | | |
296 | 315 | | |
297 | 316 | | |
| |||
0 commit comments