Skip to content

Commit a422d13

Browse files
committed
chore: changes after review
1 parent 1c40b9a commit a422d13

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ describe('Oas3 path-params-defined', () => {
351351
`);
352352
});
353353

354-
it('should fail on too deep callback nesting with 4 levels', async () => {
354+
it('should fail on too deep callback nesting with 2 levels', async () => {
355355
const document = parseYamlToDocument(
356356
outdent`
357357
openapi: 3.1.1
@@ -380,11 +380,6 @@ describe('Oas3 path-params-defined', () => {
380380
'{$request.body#/callbackUrl}':
381381
post:
382382
summary: Callback endpoint
383-
callbacks:
384-
onEvent:
385-
'{$request.body#/callbackUrl}':
386-
post:
387-
summary: Callback endpoint
388383
`,
389384
'foobar.yaml'
390385
);
@@ -399,12 +394,12 @@ describe('Oas3 path-params-defined', () => {
399394
{
400395
"location": [
401396
{
402-
"pointer": "#/paths/~1projects~1{projectId}/post/callbacks/onEvent/{$request.body#~1callbackUrl}/post/callbacks/onEvent/{$request.body#~1callbackUrl}/post/callbacks/onEvent/{$request.body#~1callbackUrl}/post/callbacks/onEvent/{$request.body#~1callbackUrl}/post",
397+
"pointer": "#/paths/~1projects~1{projectId}/post/callbacks/onEvent/{$request.body#~1callbackUrl}/post/callbacks/onEvent/{$request.body#~1callbackUrl}/post/callbacks/onEvent/{$request.body#~1callbackUrl}",
403398
"reportOnKey": false,
404399
"source": "foobar.yaml",
405400
},
406401
],
407-
"message": "Maximum callback nesting depth (4) reached. Path parameter validation is limited beyond this depth to prevent infinite recursion.",
402+
"message": "Maximum callback nesting depth (2) reached. Path parameter validation is limited beyond this depth to prevent infinite recursion.",
408403
"ruleId": "path-params-defined",
409404
"severity": "error",
410405
"suggest": [],

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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 = 4;
7+
const MAX_DEPTH = 2;
88

99
type PathContext = {
1010
path: string;
@@ -16,7 +16,7 @@ type OperationHandlers = {
1616
enter: () => void;
1717
leave: (op: unknown, ctx: UserContext) => void;
1818
Parameter: (parameter: Oas2Parameter | Oas3Parameter, ctx: UserContext) => void;
19-
Callback: {
19+
Callback?: {
2020
PathItem: {
2121
enter: (node: object, ctx: UserContext) => void;
2222
leave: () => void;
@@ -28,7 +28,7 @@ type OperationHandlers = {
2828

2929
export const PathParamsDefined: Oas3Rule | Oas2Rule = () => {
3030
const pathContext = { current: null as PathContext | null };
31-
const operationParams = { current: null as Set<string> | null };
31+
const currentOperationParams = new Set<string>();
3232

3333
return {
3434
PathItem: {
@@ -41,7 +41,7 @@ export const PathParamsDefined: Oas3Rule | Oas2Rule = () => {
4141
Parameter(parameter: Oas2Parameter | Oas3Parameter, { report, location }: UserContext) {
4242
createPathItemParameterHandler(parameter, pathContext, report, location);
4343
},
44-
Operation: createOperationHandlers(pathContext, operationParams),
44+
Operation: createOperationHandlers(pathContext, currentOperationParams),
4545
},
4646
};
4747
};
@@ -109,16 +109,15 @@ const createEmptyOperationHandlers = (maxDepth: number): OperationHandlers => {
109109
enter: () => {},
110110
leave: () => {},
111111
Parameter: () => {},
112-
Callback: {},
113112
},
114113
},
115114
},
116-
} as unknown as OperationHandlers;
115+
};
117116
};
118117

119118
const createOperationHandlers = (
120119
pathContext: { current: PathContext | null },
121-
operationParams: { current: Set<string> | null },
120+
currentOperationParams: Set<string>,
122121
depth = 0
123122
): OperationHandlers => {
124123
if (depth >= MAX_DEPTH) {
@@ -140,23 +139,23 @@ const createOperationHandlers = (
140139
createPathItemParameterHandler(parameter, pathContext, report, location);
141140
},
142141
get Operation() {
143-
return createOperationHandlers(pathContext, operationParams, depth + 1);
142+
return createOperationHandlers(pathContext, currentOperationParams, depth + 1);
144143
},
145144
};
146145
};
147146

148147
return {
149148
enter() {
150-
operationParams.current = new Set();
149+
currentOperationParams = new Set();
151150
},
152151
leave(_op: unknown, { report, location }: UserContext) {
153-
if (!pathContext.current || !operationParams.current) return;
152+
if (!pathContext.current || !currentOperationParams) return;
154153

155-
collectPathParamsFromOperation(_op, operationParams.current);
154+
collectPathParamsFromOperation(_op, currentOperationParams);
156155

157156
validateRequiredPathParams(
158157
pathContext.current.templateParams,
159-
operationParams.current,
158+
currentOperationParams,
160159
pathContext.current.definedParams,
161160
pathContext.current.path,
162161
report,
@@ -165,10 +164,10 @@ const createOperationHandlers = (
165164
},
166165
Parameter(parameter: Oas2Parameter | Oas3Parameter, { report, location }: UserContext) {
167166
if (parameter.in === 'path' && parameter.name && pathContext.current) {
168-
if (!operationParams.current) {
169-
operationParams.current = new Set();
167+
if (!currentOperationParams) {
168+
currentOperationParams = new Set();
170169
}
171-
operationParams.current.add(parameter.name);
170+
currentOperationParams.add(parameter.name);
172171
validatePathParameter(
173172
parameter.name,
174173
pathContext.current.templateParams,

0 commit comments

Comments
 (0)