Skip to content

Commit 8e48979

Browse files
Fix/160 (#162)
* fix: corrected the hide implmentation * chore: added tests for hide function
1 parent 8bb6786 commit 8e48979

File tree

4 files changed

+107
-7
lines changed

4 files changed

+107
-7
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`basic > hide with a boolean value 1`] = `
4+
{
5+
"components": {},
6+
"info": {
7+
"description": "Development documentation",
8+
"title": "Hono Documentation",
9+
"version": "0.0.0",
10+
},
11+
"openapi": "3.1.0",
12+
"paths": {
13+
"/": {
14+
"get": undefined,
15+
},
16+
},
17+
"tags": undefined,
18+
}
19+
`;
20+
21+
exports[`basic > hide with a function 1`] = `
22+
{
23+
"components": {},
24+
"info": {
25+
"description": "Development documentation",
26+
"title": "Hono Documentation",
27+
"version": "0.0.0",
28+
},
29+
"openapi": "3.1.0",
30+
"paths": {
31+
"/": {
32+
"get": undefined,
33+
},
34+
},
35+
"tags": undefined,
36+
}
37+
`;

src/__tests__/basic.test.ts

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import { Hono } from "hono";
22
import { describe, expect, it, vi } from "vitest";
33
import z from "zod";
44
import { generateSpecs } from "../handler.js";
5-
import { validator } from "../middlewares.js";
5+
import { describeRoute, resolver, validator } from "../middlewares.js";
66
import "zod-openapi/extend";
77

88
describe("basic", () => {
99
it("operationId", async () => {
10-
1110
const operationId = vi.fn(() => "hello");
1211

1312
const app = new Hono().get(
@@ -21,12 +20,74 @@ describe("basic", () => {
2120
const specs = await generateSpecs(app, {
2221
defaultOptions: {
2322
GET: {
24-
operationId
25-
}
26-
}
23+
operationId,
24+
},
25+
},
2726
});
2827

2928
expect(operationId).toBeCalled();
30-
expect(operationId).toBeCalledTimes(1)
29+
expect(operationId).toBeCalledTimes(1);
30+
});
31+
32+
it("hide with a boolean value", async () => {
33+
const app = new Hono().get(
34+
"/",
35+
describeRoute({
36+
hide: true,
37+
description: "This is a test route",
38+
responses: {
39+
200: {
40+
description: "Success",
41+
content: {
42+
"application/json": {
43+
schema: resolver(
44+
z.object({
45+
message: z.string(),
46+
}),
47+
),
48+
},
49+
},
50+
},
51+
},
52+
}),
53+
async (c) => {
54+
return c.json({ message: "Hello, world!" });
55+
},
56+
);
57+
58+
const specs = await generateSpecs(app);
59+
60+
expect(specs).toMatchSnapshot();
61+
});
62+
63+
it("hide with a function", async () => {
64+
const app = new Hono().get(
65+
"/",
66+
describeRoute({
67+
hide: () => true,
68+
description: "This is a test route",
69+
responses: {
70+
200: {
71+
description: "Success",
72+
content: {
73+
"application/json": {
74+
schema: resolver(
75+
z.object({
76+
message: z.string(),
77+
}),
78+
),
79+
},
80+
},
81+
},
82+
},
83+
}),
84+
async (c) => {
85+
return c.json({ message: "Hello, world!" });
86+
},
87+
);
88+
89+
const specs = await generateSpecs(app);
90+
91+
expect(specs).toMatchSnapshot();
3192
});
3293
});

src/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ function mergeComponentsObjects(
343343
) {
344344
return components.reduce<OpenAPIV3_1.ComponentsObject>(
345345
(prev, component, index) => {
346-
if (!component || index === 0) return prev;
346+
if (component == null || index === 0) return prev;
347347

348348
if (
349349
(prev.schemas && Object.keys(prev.schemas).length > 0) ||

src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ export function removeExcludedPaths(
221221
for (const method of Object.keys(value)) {
222222
const schema = value[method];
223223

224+
if (schema == null) continue;
225+
224226
if (key.includes("{")) {
225227
if (!schema.parameters) schema.parameters = [];
226228

0 commit comments

Comments
 (0)