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
5 changes: 5 additions & 0 deletions .changeset/evil-states-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@redocly/openapi-core": patch
---

Fixed `path-params-defined` rule to correctly skip parameters defined through `$ref`.
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,30 @@ describe('Oas3 path-params-defined', () => {
]
`);
});

it('should not report on undefined params in case of reference', async () => {
const document = parseYamlToDocument(
outdent`
openapi: 3.0.0
paths:
/test-endpoint:
get:
operationId: testEndpoint
tags:
- Test
summary: Test endpoint to reproduce bug
parameters:
- $ref: ./test_params.yaml
`,
'foobar.yaml'
);

const results = await lintDocument({
externalRefResolver: new BaseResolver(),
document,
config: await createConfig({ rules: { 'path-params-defined': 'error' } }),
});

expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`[]`);
});
});
20 changes: 11 additions & 9 deletions packages/core/src/rules/common/path-params-defined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@ const createOperationHandlers = (
enter() {
currentOperationParams = new Set();
},
leave(_op: unknown, { report, location }: UserContext) {
leave(_operation: unknown, { report, location }: UserContext) {
if (!pathContext.current || !currentOperationParams) return;

collectPathParamsFromOperation(_op, currentOperationParams);

validateRequiredPathParams(
pathContext.current.templateParams,
currentOperationParams,
Expand All @@ -127,6 +125,8 @@ const createOperationHandlers = (
);
},
Parameter(parameter: Oas2Parameter | Oas3Parameter, { report, location }: UserContext) {
collectPathParamsFromOperation(parameter, currentOperationParams);

if (parameter.in === 'path' && parameter.name && pathContext.current) {
currentOperationParams.add(parameter.name);
validatePathParameter(
Expand All @@ -150,13 +150,15 @@ const extractTemplateParams = (path: string): Set<string> => {
return new Set(Array.from(path.matchAll(pathRegex)).map((m) => m[1]));
};

const collectPathParamsFromOperation = (operation: unknown, targetSet: Set<string>): void => {
const op = operation as { parameters?: Array<{ in?: string; name?: string }> };
op?.parameters?.forEach((param) => {
if (param?.in === 'path' && param?.name) {
targetSet.add(param.name);
const collectPathParamsFromOperation = (
parameter: Oas2Parameter | Oas3Parameter,
targetSet: Set<string>
): void => {
if (parameter && typeof parameter === 'object' && 'in' in parameter && 'name' in parameter) {
if (parameter.in === 'path' && parameter.name) {
targetSet.add(parameter.name);
}
});
}
};

const validatePathParameter = (
Expand Down
Loading