Skip to content

Commit d8ef889

Browse files
committed
chore: update types and simplify nested parameters collection
1 parent 6c96db7 commit d8ef889

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ describe('Oas3 path-params-defined', () => {
415415
- Test
416416
summary: Test endpoint to reproduce bug
417417
parameters:
418-
$ref: ./test_params.yaml
418+
- $ref: ./test_params.yaml
419419
`,
420420
'foobar.yaml'
421421
);

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,9 @@ const createOperationHandlers = (
112112
enter() {
113113
currentOperationParams = new Set();
114114
},
115-
leave(_op: unknown, { report, location }: UserContext) {
115+
leave(_operation: unknown, { report, location }: UserContext) {
116116
if (!pathContext.current || !currentOperationParams) return;
117117

118-
collectPathParamsFromOperation(_op, currentOperationParams);
119-
120118
validateRequiredPathParams(
121119
pathContext.current.templateParams,
122120
currentOperationParams,
@@ -127,6 +125,8 @@ const createOperationHandlers = (
127125
);
128126
},
129127
Parameter(parameter: Oas2Parameter | Oas3Parameter, { report, location }: UserContext) {
128+
collectPathParamsFromOperation(parameter, currentOperationParams);
129+
130130
if (parameter.in === 'path' && parameter.name && pathContext.current) {
131131
currentOperationParams.add(parameter.name);
132132
validatePathParameter(
@@ -150,18 +150,14 @@ const extractTemplateParams = (path: string): Set<string> => {
150150
return new Set(Array.from(path.matchAll(pathRegex)).map((m) => m[1]));
151151
};
152152

153-
const collectPathParamsFromOperation = (operation: unknown, targetSet: Set<string>): void => {
154-
const op = operation as { parameters?: unknown };
155-
const params = op?.parameters;
156-
157-
if (Array.isArray(params)) {
158-
for (const param of params) {
159-
if (param && typeof param === 'object' && 'in' in param && 'name' in param) {
160-
const p = param as { in?: string; name?: string };
161-
if (p.in === 'path' && p.name) {
162-
targetSet.add(p.name);
163-
}
164-
}
153+
const collectPathParamsFromOperation = (
154+
parameter: Oas2Parameter | Oas3Parameter,
155+
targetSet: Set<string>
156+
): void => {
157+
if (parameter && typeof parameter === 'object' && 'in' in parameter && 'name' in parameter) {
158+
const p = parameter as { in?: string; name?: string };
159+
if (p.in === 'path' && p.name) {
160+
targetSet.add(parameter.name);
165161
}
166162
}
167163
};

0 commit comments

Comments
 (0)