Skip to content

Commit 780d234

Browse files
matzehechtMatthias Hecht
andauthored
fix: generateOperationId not handling path params (#213)
Co-authored-by: Matthias Hecht <matthias.hecht@boehringer-ingelheim.com>
1 parent 31c5bf3 commit 780d234

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ exports[`zod v3 > describeResponse 1`] = `
8686
"/{id}": {
8787
"get": {
8888
"description": "This is a test route",
89-
"operationId": "get:id",
89+
"operationId": "getById",
9090
"parameters": [
9191
{
9292
"$ref": "#/components/parameters/Param",
@@ -238,7 +238,7 @@ exports[`zod v3 > with reference in parameter 1`] = `
238238
"/{id}": {
239239
"get": {
240240
"description": "This is a test route",
241-
"operationId": "get:id",
241+
"operationId": "getById",
242242
"parameters": [
243243
{
244244
"$ref": "#/components/parameters/Param",

src/utils.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,32 @@ export const ALLOWED_METHODS = [
2020

2121
export type AllowedMethods = (typeof ALLOWED_METHODS)[number];
2222

23-
const toOpenAPIPath = (path: string) =>
24-
path
25-
.split("/")
26-
.map((x) => {
27-
let tmp = x;
28-
29-
// Example - ":id"
30-
if (tmp.startsWith(":")) {
31-
const match = tmp.match(/^:([^{?]+)(?:{(.+)})?(\?)?$/);
32-
if (match) {
33-
const paramName = match[1];
34-
tmp = `{${paramName}}`;
35-
} else {
36-
// Remove the leading colon ":"
37-
tmp = tmp.slice(1, tmp.length);
23+
const toOpenAPIPathSegment = (segment: string) => {
24+
let tmp = segment;
25+
26+
// Example - ":id"
27+
if (tmp.startsWith(":")) {
28+
const match = tmp.match(/^:([^{?]+)(?:{(.+)})?(\?)?$/);
29+
if (match) {
30+
const paramName = match[1];
31+
tmp = `{${paramName}}`;
32+
} else {
33+
// Remove the leading colon ":"
34+
tmp = tmp.slice(1, tmp.length);
3835

39-
// If it ends with "?", remove it
40-
// This is for optional parameters
41-
if (tmp.endsWith("?")) tmp = tmp.slice(0, -1);
36+
// If it ends with "?", remove it
37+
// This is for optional parameters
38+
if (tmp.endsWith("?")) tmp = tmp.slice(0, -1);
4239

43-
tmp = `{${tmp}}`;
44-
}
45-
}
40+
tmp = `{${tmp}}`;
41+
}
42+
}
4643

47-
return tmp;
48-
})
49-
.join("/");
44+
return tmp;
45+
};
46+
47+
const toOpenAPIPath = (path: string) =>
48+
path.split("/").map(toOpenAPIPathSegment).join("/");
5049

5150
const capitalize = (word: string) =>
5251
word.charAt(0).toUpperCase() + word.slice(1);
@@ -57,10 +56,11 @@ const generateOperationId = (route: RouterRoute) => {
5756
if (route.path === "/") return `${operationId}Index`;
5857

5958
for (const segment of route.path.split("/")) {
60-
if (segment.charCodeAt(0) === 123) {
61-
operationId += `By${capitalize(segment.slice(1, -1))}`;
59+
const openApiPathSegment = toOpenAPIPathSegment(segment);
60+
if (openApiPathSegment.charCodeAt(0) === 123) {
61+
operationId += `By${capitalize(openApiPathSegment.slice(1, -1))}`;
6262
} else {
63-
operationId += capitalize(segment);
63+
operationId += capitalize(openApiPathSegment);
6464
}
6565
}
6666

0 commit comments

Comments
 (0)