Skip to content

Commit bc7b6c6

Browse files
fix: minor improvements
1 parent a83140a commit bc7b6c6

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

src/__tests__/zodv3.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,64 @@ describe("zod v3", () => {
228228

229229
expect(specs).toMatchSnapshot();
230230
});
231+
232+
it("operation id for path with underscore", async () => {
233+
const app = new Hono().get(
234+
"/api_v1/users",
235+
describeRoute({
236+
tags: ["test"],
237+
summary: "Test route",
238+
description: "This is a test route",
239+
}),
240+
async (c) => {
241+
return c.json({ message: "Hello" });
242+
},
243+
);
244+
245+
const specs = await generateSpecs(app);
246+
247+
expect(specs.paths?.["/api_v1/users"]?.get?.operationId).toBe(
248+
"getApiV1Users",
249+
);
250+
});
251+
252+
it("operation id for path param with dash", async () => {
253+
const app = new Hono().get(
254+
"/users/:user-id",
255+
describeRoute({
256+
tags: ["test"],
257+
summary: "Test route",
258+
description: "This is a test route",
259+
}),
260+
async (c) => {
261+
return c.json({ message: "Hello" });
262+
},
263+
);
264+
265+
const specs = await generateSpecs(app);
266+
267+
expect(specs.paths?.["/users/{user-id}"]?.get?.operationId).toBe(
268+
"getUsersByUserId",
269+
);
270+
});
271+
272+
it("operation id for multiple dashed segments", async () => {
273+
const app = new Hono().get(
274+
"/api-v1/user-profile",
275+
describeRoute({
276+
tags: ["test"],
277+
summary: "Test route",
278+
description: "This is a test route",
279+
}),
280+
async (c) => {
281+
return c.json({ message: "Hello" });
282+
},
283+
);
284+
285+
const specs = await generateSpecs(app);
286+
287+
expect(specs.paths?.["/api-v1/user-profile"]?.get?.operationId).toBe(
288+
"getApiV1UserProfile",
289+
);
290+
});
231291
});

src/utils.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@ const toOpenAPIPath = (path: string) =>
4949

5050
const toPascalCase = (text: string) =>
5151
text
52-
// capitalize the first letter of each word
53-
.replaceAll(/(\w)(\w*)/g, (_, firstChar: string, rest: string) => {
54-
return `${firstChar.toUpperCase()}${rest}`;
55-
})
56-
// replace all non-word characters except the first character
57-
.replaceAll(/(?<!^)\W/g, "");
52+
.split(/[\W_]+/)
53+
.filter(Boolean)
54+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
55+
.join("");
5856

5957
const generateOperationId = (route: RouterRoute) => {
6058
let operationId = route.method.toLowerCase();

0 commit comments

Comments
 (0)