Skip to content

Commit c484087

Browse files
fix: improved the ref in params
1 parent 71ab48c commit c484087

File tree

11 files changed

+379
-88
lines changed

11 files changed

+379
-88
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"main": "dist/index.cjs",
77
"module": "dist/index.js",
88
"types": "dist/index.d.ts",
9-
"files": ["dist"],
9+
"files": [
10+
"dist"
11+
],
1012
"license": "MIT",
1113
"scripts": {
1214
"build": "pkgroll --clean-dist",
@@ -86,7 +88,8 @@
8688
"@valibot/to-json-schema": "^1.3.0",
8789
"pkgroll": "^2.13.1",
8890
"typescript": "^5.8.3",
89-
"vitest": "^3.2.4"
91+
"vitest": "^3.2.4",
92+
"zod-openapi": "4"
9093
},
9194
"packageManager": "pnpm@10.0.0"
9295
}

pnpm-lock.yaml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/__snapshots__/arktype.test.ts.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
exports[`arktype > basic 1`] = `
44
{
5-
"components": {
6-
"schemas": {},
7-
},
5+
"components": {},
86
"info": {
97
"description": "Development documentation",
108
"title": "Hono Documentation",

src/__tests__/__snapshots__/effect.test.ts.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
exports[`effect > basic 1`] = `
44
{
5-
"components": {
6-
"schemas": {},
7-
},
5+
"components": {},
86
"info": {
97
"description": "Development documentation",
108
"title": "Hono Documentation",

src/__tests__/__snapshots__/valibot.test.ts.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
exports[`valibot > basic 1`] = `
44
{
5-
"components": {
6-
"schemas": {},
7-
},
5+
"components": {},
86
"info": {
97
"description": "Development documentation",
108
"title": "Hono Documentation",

src/__tests__/__snapshots__/zodv3.test.ts.snap

Lines changed: 155 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
exports[`zod v3 > basic 1`] = `
44
{
5-
"components": {
6-
"schemas": {},
7-
},
5+
"components": {},
86
"info": {
97
"description": "Development documentation",
108
"title": "Hono Documentation",
@@ -63,3 +61,157 @@ exports[`zod v3 > basic 1`] = `
6361
"tags": undefined,
6462
}
6563
`;
64+
65+
exports[`zod v3 > with metadata 1`] = `
66+
{
67+
"components": {
68+
"schemas": {
69+
"SuccessResponse": {
70+
"properties": {
71+
"message": {
72+
"type": "string",
73+
},
74+
},
75+
"required": [
76+
"message",
77+
],
78+
"type": "object",
79+
},
80+
},
81+
},
82+
"info": {
83+
"description": "Development documentation",
84+
"title": "Hono Documentation",
85+
"version": "0.0.0",
86+
},
87+
"openapi": "3.1.0",
88+
"paths": {
89+
"/": {
90+
"get": {
91+
"description": "This is a test route",
92+
"operationId": "GETIndex",
93+
"requestBody": {
94+
"content": {
95+
"application/json": {
96+
"schema": {
97+
"properties": {
98+
"message": {
99+
"type": "string",
100+
},
101+
},
102+
"required": [
103+
"message",
104+
],
105+
"type": "object",
106+
},
107+
},
108+
},
109+
},
110+
"responses": {
111+
"200": {
112+
"content": {
113+
"application/json": {
114+
"schema": {
115+
"$ref": "#/components/schemas/SuccessResponse",
116+
},
117+
},
118+
},
119+
"description": "Success",
120+
},
121+
},
122+
"summary": "Test route",
123+
"tags": [
124+
"test",
125+
],
126+
},
127+
},
128+
},
129+
"tags": undefined,
130+
}
131+
`;
132+
133+
exports[`zod v3 > with reference in parameter 1`] = `
134+
{
135+
"components": {
136+
"parameters": {
137+
"Param": {
138+
"in": "path",
139+
"name": "id",
140+
"required": true,
141+
"schema": {
142+
"type": "number",
143+
},
144+
},
145+
},
146+
},
147+
"info": {
148+
"description": "Development documentation",
149+
"title": "Hono Documentation",
150+
"version": "0.0.0",
151+
},
152+
"openapi": "3.1.0",
153+
"paths": {
154+
"/{id}": {
155+
"get": {
156+
"description": "This is a test route",
157+
"operationId": "GET:id",
158+
"parameters": [
159+
{
160+
"$ref": "#/components/parameters/Param",
161+
},
162+
{
163+
"in": "query",
164+
"name": "limit",
165+
"required": true,
166+
"schema": {
167+
"default": 10,
168+
"type": "number",
169+
},
170+
},
171+
{
172+
"in": "query",
173+
"name": "offset",
174+
"required": true,
175+
"schema": {
176+
"default": 0,
177+
"type": "number",
178+
},
179+
},
180+
],
181+
"responses": {
182+
"200": {
183+
"content": {
184+
"application/json": {
185+
"schema": {
186+
"properties": {
187+
"count": {
188+
"type": "number",
189+
},
190+
"result": {
191+
"items": {
192+
"type": "string",
193+
},
194+
"type": "array",
195+
},
196+
},
197+
"required": [
198+
"result",
199+
"count",
200+
],
201+
"type": "object",
202+
},
203+
},
204+
},
205+
"description": "Success",
206+
},
207+
},
208+
"summary": "Test route",
209+
"tags": [
210+
"test",
211+
],
212+
},
213+
},
214+
},
215+
"tags": undefined,
216+
}
217+
`;

src/__tests__/__snapshots__/zodv4.test.ts.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
exports[`zod v4 > basic 1`] = `
44
{
5-
"components": {
6-
"schemas": {},
7-
},
5+
"components": {},
86
"info": {
97
"description": "Development documentation",
108
"title": "Hono Documentation",

src/__tests__/zodv3.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vitest";
33
import z from "zod";
44
import { generateSpecs } from "../handler.js";
55
import { describeRoute, resolver, validator } from "../middlewares.js";
6+
import "zod-openapi/extend";
67

78
describe("zod v3", () => {
89
it("basic", async () => {
@@ -37,4 +38,85 @@ describe("zod v3", () => {
3738

3839
expect(specs).toMatchSnapshot();
3940
});
41+
42+
it("with metadata", async () => {
43+
const app = new Hono().get(
44+
"/",
45+
describeRoute({
46+
tags: ["test"],
47+
summary: "Test route",
48+
description: "This is a test route",
49+
responses: {
50+
200: {
51+
description: "Success",
52+
content: {
53+
"application/json": {
54+
schema: resolver(
55+
z
56+
.object({
57+
message: z.string(),
58+
})
59+
.openapi({ ref: "SuccessResponse" }),
60+
),
61+
},
62+
},
63+
},
64+
},
65+
}),
66+
validator("json", z.object({ message: z.string() })),
67+
async (c) => {
68+
return c.json({ message: "Hello, world!" });
69+
},
70+
);
71+
72+
const specs = await generateSpecs(app);
73+
74+
expect(specs).toMatchSnapshot();
75+
});
76+
77+
it("with reference in parameter", async () => {
78+
const query = z.object({
79+
limit: z.coerce.number().default(10),
80+
offset: z.coerce.number().default(0),
81+
});
82+
83+
const param = z
84+
.object({
85+
id: z.coerce.number(),
86+
})
87+
.openapi({ ref: "Param" });
88+
89+
const app = new Hono().get(
90+
"/:id",
91+
describeRoute({
92+
tags: ["test"],
93+
summary: "Test route",
94+
description: "This is a test route",
95+
responses: {
96+
200: {
97+
description: "Success",
98+
content: {
99+
"application/json": {
100+
schema: resolver(
101+
z.object({
102+
result: z.array(z.string()),
103+
count: z.number(),
104+
}),
105+
),
106+
},
107+
},
108+
},
109+
},
110+
}),
111+
validator("param", param),
112+
validator("query", query),
113+
async (c) => {
114+
return c.json([]);
115+
},
116+
);
117+
118+
const specs = await generateSpecs(app);
119+
120+
expect(specs).toMatchSnapshot();
121+
});
40122
});

0 commit comments

Comments
 (0)