Skip to content

Commit 497b223

Browse files
committed
fix(files): reflect the content-version guard outcome in the dimensions response
The route returned success:true even when updateWorkspaceFileDimensions matched 0 rows (the CAS rejected a write whose measured key no longer matches the row). Return success:<whether a row was written> and widen the contract response to { success: boolean }. Not an error path — the client's next measurement persists once its file list has the new key; this just stops the API claiming a persist that did not happen.
1 parent 94348f0 commit 497b223

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

apps/sim/app/api/workspaces/[id]/files/[fileId]/dimensions/route.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ describe('PATCH /api/workspaces/[id]/files/[fileId]/dimensions', () => {
5656
expect(mockUpdateWorkspaceFileDimensions).toHaveBeenCalledOnce()
5757
})
5858

59+
it('reports success:false when the content-version guard rejects the write (key changed)', async () => {
60+
mockUpdateWorkspaceFileDimensions.mockResolvedValue(false)
61+
const res = await PATCH(buildRequest({ key: KEY, width: 10, height: 20 }), routeContext)
62+
expect(res.status).toBe(200)
63+
expect(await res.json()).toEqual({ success: false })
64+
})
65+
5966
it('rejects an unauthenticated caller before touching the DB', async () => {
6067
authMockFns.mockGetSession.mockResolvedValue(null)
6168
const res = await PATCH(buildRequest({ key: KEY, width: 10, height: 10 }), routeContext)

apps/sim/app/api/workspaces/[id]/files/[fileId]/dimensions/route.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,15 @@ export const PATCH = withRouteHandler(
3636
}
3737

3838
try {
39-
await updateWorkspaceFileDimensions(workspaceId, fileId, { key, width, height })
40-
return NextResponse.json({ success: true as const })
39+
// `written` is false when the content-version guard rejected the write (the row's storage key no
40+
// longer matches the key the client measured — the content was replaced since). That is not an
41+
// error; the client's next measurement, once its file list has the new key, persists correctly.
42+
const written = await updateWorkspaceFileDimensions(workspaceId, fileId, {
43+
key,
44+
width,
45+
height,
46+
})
47+
return NextResponse.json({ success: written })
4148
} catch (error) {
4249
logger.error('Failed to backfill workspace file dimensions', {
4350
workspaceId,

apps/sim/lib/api/contracts/workspace-files.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ export const updateWorkspaceFileDimensionsContract = defineRouteContract({
137137
body: updateWorkspaceFileDimensionsBodySchema,
138138
response: {
139139
mode: 'json',
140-
schema: z.object({ success: z.literal(true) }),
140+
// `success` reflects whether the row was actually written: false when the content-version guard
141+
// rejected the write (the storage key changed since the client measured), not just on error.
142+
schema: z.object({ success: z.boolean() }),
141143
},
142144
})
143145

0 commit comments

Comments
 (0)