Skip to content

Commit 9967992

Browse files
committed
chore: simplify max depth handling
1 parent a422d13 commit 9967992

File tree

2 files changed

+21
-62
lines changed

2 files changed

+21
-62
lines changed

packages/core/src/rules/common/__tests__/path-params-defined.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,6 @@ describe('Oas3 path-params-defined', () => {
375375
'{$request.body#/callbackUrl}':
376376
post:
377377
summary: Callback endpoint
378-
callbacks:
379-
onEvent:
380-
'{$request.body#/callbackUrl}':
381-
post:
382-
summary: Callback endpoint
383378
`,
384379
'foobar.yaml'
385380
);
@@ -394,7 +389,7 @@ describe('Oas3 path-params-defined', () => {
394389
{
395390
"location": [
396391
{
397-
"pointer": "#/paths/~1projects~1{projectId}/post/callbacks/onEvent/{$request.body#~1callbackUrl}/post/callbacks/onEvent/{$request.body#~1callbackUrl}/post/callbacks/onEvent/{$request.body#~1callbackUrl}",
392+
"pointer": "#/paths/~1projects~1{projectId}/post/callbacks/onEvent/{$request.body#~1callbackUrl}/post/callbacks/onEvent/{$request.body#~1callbackUrl}/post",
398393
"reportOnKey": false,
399394
"source": "foobar.yaml",
400395
},

packages/core/src/rules/common/path-params-defined.ts

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,14 @@ import type { Oas3Parameter } from '../../typings/openapi.js';
44
import type { UserContext } from '../../walk.js';
55

66
const pathRegex = /\{([a-zA-Z0-9_.-]+)\}+/g;
7-
const MAX_DEPTH = 2;
7+
const MAX_DEPTH = 2; // Only first callback level is supported
88

99
type PathContext = {
1010
path: string;
1111
templateParams: Set<string>;
1212
definedParams: Set<string>;
1313
};
1414

15-
type OperationHandlers = {
16-
enter: () => void;
17-
leave: (op: unknown, ctx: UserContext) => void;
18-
Parameter: (parameter: Oas2Parameter | Oas3Parameter, ctx: UserContext) => void;
19-
Callback?: {
20-
PathItem: {
21-
enter: (node: object, ctx: UserContext) => void;
22-
leave: () => void;
23-
Parameter: (parameter: Oas2Parameter | Oas3Parameter, ctx: UserContext) => void;
24-
Operation: OperationHandlers;
25-
};
26-
};
27-
};
28-
2915
export const PathParamsDefined: Oas3Rule | Oas2Rule = () => {
3016
const pathContext = { current: null as PathContext | null };
3117
const currentOperationParams = new Set<string>();
@@ -76,52 +62,30 @@ const createPathItemParameterHandler = (
7662
}
7763
};
7864

79-
const createEmptyOperationHandlers = (maxDepth: number): OperationHandlers => {
80-
let warningReported = false;
81-
82-
const reportMaxDepthWarning = (
83-
report: UserContext['report'],
84-
location: UserContext['location']
85-
) => {
86-
if (!warningReported) {
87-
warningReported = true;
88-
report({
89-
message: `Maximum callback nesting depth (${maxDepth}) reached. Path parameter validation is limited beyond this depth to prevent infinite recursion.`,
90-
location: location,
91-
});
92-
}
93-
};
94-
95-
return {
96-
enter: () => {},
97-
leave(_op: unknown, { report, location }: UserContext) {
98-
reportMaxDepthWarning(report, location);
99-
},
100-
Parameter: () => {},
101-
Callback: {
102-
PathItem: {
103-
enter(_: object, { report, location }: UserContext) {
104-
reportMaxDepthWarning(report, location);
105-
},
106-
leave: () => {},
107-
Parameter: () => {},
108-
Operation: {
109-
enter: () => {},
110-
leave: () => {},
111-
Parameter: () => {},
112-
},
113-
},
114-
},
115-
};
116-
};
117-
11865
const createOperationHandlers = (
11966
pathContext: { current: PathContext | null },
12067
currentOperationParams: Set<string>,
12168
depth = 0
122-
): OperationHandlers => {
69+
) => {
70+
const reportMaxDepthWarning = (
71+
report: UserContext['report'],
72+
location: UserContext['location'],
73+
depth: number
74+
) => {
75+
report({
76+
message: `Maximum callback nesting depth (${depth}) reached. Path parameter validation is limited beyond this depth to prevent infinite recursion.`,
77+
location: location,
78+
});
79+
};
12380
if (depth >= MAX_DEPTH) {
124-
return createEmptyOperationHandlers(MAX_DEPTH);
81+
return {
82+
enter: () => {},
83+
leave: (_op: unknown, { report, location }: UserContext) => {
84+
reportMaxDepthWarning(report, location, depth);
85+
},
86+
Parameter: () => {},
87+
Callback: undefined,
88+
};
12589
}
12690

12791
const createCallbackPathItem = () => {

0 commit comments

Comments
 (0)