Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 5eddc96

Browse files
committed
fix(core): disable body parsing on HonoOpenAPI to fix JSON protocol (#1169)
1 parent 1505009 commit 5eddc96

File tree

5 files changed

+47
-33
lines changed

5 files changed

+47
-33
lines changed

packages/core/scripts/dump-openapi.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ function main() {
6060
driverConfig,
6161
inlineClientDriver,
6262
managerDriver,
63+
true,
6364
);
6465

6566
const openApiDoc = openapi.getOpenAPIDocument({

packages/core/src/driver-test-suite/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export async function createTestRuntime(
164164
config,
165165
inlineDriver,
166166
managerDriver,
167+
false,
167168
);
168169

169170
// Inject WebSocket

packages/core/src/manager/router.ts

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,17 @@ const OPENAPI_CONN_TOKEN = z.string().openapi({
119119
description: "Connection token",
120120
});
121121

122-
function buildOpenApiResponses<T>(schema: T) {
122+
function buildOpenApiResponses<T>(schema: T, validateBody: boolean) {
123123
return {
124124
200: {
125125
description: "Success",
126-
content: {
127-
"application/json": {
128-
schema,
129-
},
130-
},
126+
content: validateBody
127+
? {
128+
"application/json": {
129+
schema,
130+
},
131+
}
132+
: {},
131133
},
132134
400: {
133135
description: "User error",
@@ -138,11 +140,19 @@ function buildOpenApiResponses<T>(schema: T) {
138140
};
139141
}
140142

143+
/**
144+
* Only use `validateBody` to `true` if you need to export OpenAPI JSON.
145+
*
146+
* If left enabled for production, this will cause errors. We disable JSON validation since:
147+
* - It prevents us from proxying requests, since validating the body requires consuming the body so we can't forward the body
148+
* - We validate all types at the actor router layer since most requests are proxied
149+
*/
141150
export function createManagerRouter(
142151
registryConfig: RegistryConfig,
143152
runConfig: RunConfig,
144153
inlineClientDriver: ClientDriver,
145154
managerDriver: ManagerDriver,
155+
validateBody: boolean,
146156
): { router: Hono; openapi: OpenAPIHono } {
147157
const router = new OpenAPIHono({ strict: false }).basePath(
148158
runConfig.basePath,
@@ -259,17 +269,19 @@ export function createManagerRouter(
259269
path: "/actors/resolve",
260270
request: {
261271
body: {
262-
content: {
263-
"application/json": {
264-
schema: ResolveQuerySchema,
265-
},
266-
},
272+
content: validateBody
273+
? {
274+
"application/json": {
275+
schema: ResolveQuerySchema,
276+
},
277+
}
278+
: {},
267279
},
268280
headers: z.object({
269281
[HEADER_ACTOR_QUERY]: OPENAPI_ACTOR_QUERY,
270282
}),
271283
},
272-
responses: buildOpenApiResponses(ResolveResponseSchema),
284+
responses: buildOpenApiResponses(ResolveResponseSchema, validateBody),
273285
});
274286

275287
router.openapi(resolveRoute, (c) =>
@@ -374,18 +386,20 @@ export function createManagerRouter(
374386
request: {
375387
params: ActionParamsSchema,
376388
body: {
377-
content: {
378-
"application/json": {
379-
schema: ActionRequestSchema,
380-
},
381-
},
389+
content: validateBody
390+
? {
391+
"application/json": {
392+
schema: ActionRequestSchema,
393+
},
394+
}
395+
: {},
382396
},
383397
headers: z.object({
384398
[HEADER_ENCODING]: OPENAPI_ENCODING,
385399
[HEADER_CONN_PARAMS]: OPENAPI_CONN_PARAMS.optional(),
386400
}),
387401
},
388-
responses: buildOpenApiResponses(ActionResponseSchema),
402+
responses: buildOpenApiResponses(ActionResponseSchema, validateBody),
389403
});
390404

391405
router.openapi(actionRoute, (c) =>
@@ -412,11 +426,13 @@ export function createManagerRouter(
412426
path: "/actors/message",
413427
request: {
414428
body: {
415-
content: {
416-
"application/json": {
417-
schema: ConnectionMessageRequestSchema,
418-
},
419-
},
429+
content: validateBody
430+
? {
431+
"application/json": {
432+
schema: ConnectionMessageRequestSchema,
433+
},
434+
}
435+
: {},
420436
},
421437
headers: z.object({
422438
[HEADER_ACTOR_ID]: OPENAPI_ACTOR_ID,
@@ -425,7 +441,10 @@ export function createManagerRouter(
425441
[HEADER_CONN_TOKEN]: OPENAPI_CONN_TOKEN,
426442
}),
427443
},
428-
responses: buildOpenApiResponses(ConnectionMessageResponseSchema),
444+
responses: buildOpenApiResponses(
445+
ConnectionMessageResponseSchema,
446+
validateBody,
447+
),
429448
});
430449

431450
router.openapi(messageRoute, (c) =>
@@ -768,20 +787,11 @@ export function createManagerRouter(
768787
});
769788
}
770789

771-
router.doc("/openapi.json", {
772-
openapi: "3.0.0",
773-
info: {
774-
version: VERSION,
775-
title: "RivetKit API",
776-
},
777-
});
778-
779790
managerDriver.modifyManagerRouter?.(
780791
registryConfig,
781792
router as unknown as Hono,
782793
);
783794

784-
785795
// Mount on both / and /registry
786796
//
787797
// We do this because the default requests are to `/registry/*`.

packages/core/src/registry/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export class Registry<A extends RegistryActors> {
6363
config,
6464
clientDriver,
6565
managerDriver,
66+
false,
6667
);
6768

6869
// Create client

packages/core/src/test/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function serve(registry: Registry<any>, inputConfig?: InputConfig): ServerType {
3535
runConfig,
3636
inlineClientDriver,
3737
managerDriver,
38+
false,
3839
);
3940

4041
// Inject WebSocket

0 commit comments

Comments
 (0)