Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/__tests__/zodv4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,42 @@ describe("zod v4", () => {

expect(specs).toMatchSnapshot();
});

it("defaultOptions should not cause parameter pollution between routes", async () => {
const app = new Hono()
.get(
"/hello",
describeRoute({}),
validator("query", z.object({ name: z.string() })),
(c) => {
const { name } = c.req.valid("query");
return c.text(`Hello, ${name}!`);
},
)
.get(
"/world",
describeRoute({}),
validator("query", z.object({ greeting: z.string() })),
(c) => {
const { greeting } = c.req.valid("query");
return c.text(`${greeting}, world!`);
},
);

const specs = await generateSpecs(app, {
defaultOptions: {
GET: {},
},
});

// Verify /hello only has 'name' parameter
const helloParams = specs.paths["/hello"]?.get?.parameters;
expect(helloParams).toHaveLength(1);
expect(helloParams?.[0]).toMatchObject({ name: "name", in: "query" });

// Verify /world only has 'greeting' parameter (not 'name' from /hello)
const worldParams = specs.paths["/world"]?.get?.parameters;
expect(worldParams).toHaveLength(1);
expect(worldParams?.[0]).toMatchObject({ name: "greeting", in: "query" });
});
});
7 changes: 4 additions & 3 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ async function generatePaths<
}
}

const defaultOptionsForThisMethod =
ctx.options.defaultOptions?.[routeMethod];
const defaultOptionsForThisMethod = ctx.options.defaultOptions?.[
routeMethod
] && { ...ctx.options.defaultOptions[routeMethod] };

const { schema: routeSpecs, components = {} } = await getSpec(
middlewareHandler,
Expand Down Expand Up @@ -253,7 +254,7 @@ async function getSpec(

const result = await middlewareHandler.toOpenAPISchema();
const docs: Pick<OpenAPIV3_1.OperationObject, "parameters" | "requestBody"> =
defaultOptions ?? {};
{ ...defaultOptions };

if (
middlewareHandler.target === "form" ||
Expand Down
Loading