Skip to content

fix: allow route handlers to return plain objects as JSON responses#159

Open
sucloudflare wants to merge 1 commit into
tscircuit:mainfrom
sucloudflare:fix/allow-plain-object-return-from-route
Open

fix: allow route handlers to return plain objects as JSON responses#159
sucloudflare wants to merge 1 commit into
tscircuit:mainfrom
sucloudflare:fix/allow-plain-object-return-from-route

Conversation

@sucloudflare
Copy link
Copy Markdown

Closes #30

Problem

When a route handler returns a plain object (e.g. return { ok: true }) instead of return new WinterSpecJsonResponse({ ok: true }), the runtime fell through to an unhandled path and either threw or failed silently.

Fix

In serializeToResponse, added a guard that detects when the return value is a plain object (not a Response, not a WinterSpecResponse) and automatically wraps it in WinterSpecJsonResponse, including validation against routeSpec.jsonResponse when present.

Tests added

3 new e2e tests in tests/endpoints/plain-object-return.test.ts:

  • GET route returning { ok: true } → 200 with JSON body ✅
  • POST route returning multiple fields ✅
  • Plain object validates against jsonResponse schema ✅

Closes tscircuit#30

When a route returns a plain object (e.g. `return { ok: true }`) instead
of `return new WinterSpecJsonResponse({ ok: true })`, the runtime now
automatically wraps it in a WinterSpecJsonResponse and serializes it,
including validation against the routeSpec.jsonResponse schema when present.

Tests: 3 new e2e tests in tests/endpoints/plain-object-return.test.ts
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.

When a user returns return { ... } from a route instead of return ctx.json({ ... }), throw an error telling them to use ctx.json

1 participant