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
4 changes: 2 additions & 2 deletions src/__tests__/__snapshots__/zodv3.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ exports[`zod v3 > describeResponse 1`] = `
"/{id}": {
"get": {
"description": "This is a test route",
"operationId": "get:id",
"operationId": "getById",
"parameters": [
{
"$ref": "#/components/parameters/Param",
Expand Down Expand Up @@ -238,7 +238,7 @@ exports[`zod v3 > with reference in parameter 1`] = `
"/{id}": {
"get": {
"description": "This is a test route",
"operationId": "get:id",
"operationId": "getById",
"parameters": [
{
"$ref": "#/components/parameters/Param",
Expand Down
54 changes: 27 additions & 27 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,32 @@ export const ALLOWED_METHODS = [

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

const toOpenAPIPath = (path: string) =>
path
.split("/")
.map((x) => {
let tmp = x;

// Example - ":id"
if (tmp.startsWith(":")) {
const match = tmp.match(/^:([^{?]+)(?:{(.+)})?(\?)?$/);
if (match) {
const paramName = match[1];
tmp = `{${paramName}}`;
} else {
// Remove the leading colon ":"
tmp = tmp.slice(1, tmp.length);
const toOpenAPIPathSegment = (segment: string) => {
let tmp = segment;

// Example - ":id"
if (tmp.startsWith(":")) {
const match = tmp.match(/^:([^{?]+)(?:{(.+)})?(\?)?$/);
if (match) {
const paramName = match[1];
tmp = `{${paramName}}`;
} else {
// Remove the leading colon ":"
tmp = tmp.slice(1, tmp.length);

// If it ends with "?", remove it
// This is for optional parameters
if (tmp.endsWith("?")) tmp = tmp.slice(0, -1);
// If it ends with "?", remove it
// This is for optional parameters
if (tmp.endsWith("?")) tmp = tmp.slice(0, -1);

tmp = `{${tmp}}`;
}
}
tmp = `{${tmp}}`;
}
}

return tmp;
})
.join("/");
return tmp;
};

const toOpenAPIPath = (path: string) =>
path.split("/").map(toOpenAPIPathSegment).join("/");

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

for (const segment of route.path.split("/")) {
if (segment.charCodeAt(0) === 123) {
operationId += `By${capitalize(segment.slice(1, -1))}`;
const openApiPathSegment = toOpenAPIPathSegment(segment);
if (openApiPathSegment.charCodeAt(0) === 123) {
operationId += `By${capitalize(openApiPathSegment.slice(1, -1))}`;
} else {
operationId += capitalize(segment);
operationId += capitalize(openApiPathSegment);
}
}

Expand Down
Loading