Skip to content

Commit 98cdfad

Browse files
feat(ls): add lint rule for OpenAPI Operation Parameter defined in path template (#3629)
Refs #3546
1 parent 28b52f9 commit 98cdfad

File tree

5 files changed

+84
-4
lines changed

5 files changed

+84
-4
lines changed

packages/apidom-ls/src/services/validation/linter-functions.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1081,9 +1081,18 @@ export const standardLinterfunctions: FunctionItem[] = [
10811081
isArrayElement(element.parent) &&
10821082
includesClasses(['path-item-parameters'], element.parent);
10831083

1084-
if (!isInPathItemElement) return true;
1084+
const isInOperationElement =
1085+
isArrayElement(element.parent) &&
1086+
includesClasses(['operation-parameters'], element.parent);
1087+
1088+
if (!isInPathItemElement && !isInOperationElement) return true;
1089+
1090+
const pathItemElement: Element | undefined = isInOperationElement
1091+
? element.parent?.parent?.parent?.parent?.parent
1092+
: element.parent?.parent?.parent;
1093+
1094+
if (pathItemElement?.element !== 'pathItem') return true;
10851095

1086-
const pathItemElement = element.parent.parent.parent;
10871096
const isPathItemPartOfPathTemplating = isStringElement(pathItemElement.meta.get('path'));
10881097

10891098
if (!isPathItemPartOfPathTemplating) return true;

packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-2-0.yaml

+16-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,19 @@ paths:
2828
required: true
2929
type: string
3030
format: uuid
31-
31+
/test:
32+
get:
33+
summary: Get test
34+
operationId: getTest
35+
responses:
36+
'200':
37+
description: Successful Response
38+
parameters:
39+
- name: x_id
40+
in: path
41+
required: true
42+
type: string
43+
schema:
44+
type: string
45+
format: uuid
46+
title: X Id

packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-3-0.yaml

+18-1
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,21 @@ paths:
7171
responses:
7272
'202':
7373
description: "OK"
74-
74+
/test:
75+
get:
76+
summary: Get test
77+
operationId: getTest
78+
responses:
79+
'200':
80+
description: Successful Response
81+
content:
82+
application/json:
83+
schema: {}
84+
parameters:
85+
- name: x_id
86+
in: path
87+
required: true
88+
schema:
89+
type: string
90+
format: uuid
91+
title: X Id

packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-3-1.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,21 @@ paths:
9090
responses:
9191
'202':
9292
description: "OK"
93+
/test:
94+
get:
95+
summary: Get test
96+
operationId: getTest
97+
responses:
98+
'200':
99+
description: Successful Response
100+
content:
101+
application/json:
102+
schema: {}
103+
parameters:
104+
- name: x_id
105+
in: path
106+
required: true
107+
schema:
108+
type: string
109+
format: uuid
110+
title: X Id

packages/apidom-ls/test/validate.ts

+21
Original file line numberDiff line numberDiff line change
@@ -3586,6 +3586,13 @@ describe('apidom-ls-validate', function () {
35863586
code: 3102000,
35873587
source: 'apilint',
35883588
},
3589+
{
3590+
range: { start: { line: 38, character: 10 }, end: { line: 46, character: 0 } },
3591+
message: 'parameter is not defined within path template',
3592+
severity: 1,
3593+
code: 3102000,
3594+
source: 'apilint',
3595+
},
35893596
];
35903597
assert.deepEqual(result, expected as Diagnostic[]);
35913598

@@ -3628,6 +3635,13 @@ describe('apidom-ls-validate', function () {
36283635
code: 3102000,
36293636
source: 'apilint',
36303637
},
3638+
{
3639+
range: { start: { line: 84, character: 10 }, end: { line: 91, character: 0 } },
3640+
message: 'parameter is not defined within path template',
3641+
severity: 1,
3642+
code: 3102000,
3643+
source: 'apilint',
3644+
},
36313645
];
36323646
assert.deepEqual(result, expected as Diagnostic[]);
36333647

@@ -3670,6 +3684,13 @@ describe('apidom-ls-validate', function () {
36703684
code: 3102000,
36713685
source: 'apilint',
36723686
},
3687+
{
3688+
range: { start: { line: 103, character: 10 }, end: { line: 110, character: 0 } },
3689+
message: 'parameter is not defined within path template',
3690+
severity: 1,
3691+
code: 3102000,
3692+
source: 'apilint',
3693+
},
36733694
];
36743695
assert.deepEqual(result, expected as Diagnostic[]);
36753696

0 commit comments

Comments
 (0)