Skip to content

Commit d2615bb

Browse files
committed
fix: parameters defined as a reference in the path-params-defined rule
1 parent 0c96e48 commit d2615bb

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

.changeset/evil-states-fold.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@redocly/openapi-core": patch
3+
---
4+
5+
Fixed `path-params-defined` rule to correctly skip parameters defined via `$ref`.

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,30 @@ describe('Oas3 path-params-defined', () => {
402402
]
403403
`);
404404
});
405+
406+
it('should not report on undefined params in case of reference', async () => {
407+
const document = parseYamlToDocument(
408+
outdent`
409+
openapi: 3.0.0
410+
paths:
411+
/test-endpoint:
412+
get:
413+
operationId: testEndpoint
414+
tags:
415+
- Test
416+
summary: Test endpoint to reproduce bug
417+
parameters:
418+
$ref: ./test_params.yaml
419+
`,
420+
'foobar.yaml'
421+
);
422+
423+
const results = await lintDocument({
424+
externalRefResolver: new BaseResolver(),
425+
document,
426+
config: await createConfig({ rules: { 'path-params-defined': 'error' } }),
427+
});
428+
429+
expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`[]`);
430+
});
405431
});

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,19 @@ const extractTemplateParams = (path: string): Set<string> => {
151151
};
152152

153153
const collectPathParamsFromOperation = (operation: unknown, targetSet: Set<string>): void => {
154-
const op = operation as { parameters?: Array<{ in?: string; name?: string }> };
155-
op?.parameters?.forEach((param) => {
156-
if (param?.in === 'path' && param?.name) {
157-
targetSet.add(param.name);
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+
}
158165
}
159-
});
166+
}
160167
};
161168

162169
const validatePathParameter = (

0 commit comments

Comments
 (0)