Skip to content

Commit cb725df

Browse files
feat: add reference parameter rule
1 parent 7adb956 commit cb725df

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

packages/apidom-ls/src/config/codes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@ enum ApilintCodes {
10751075
OPENAPI3_0_REFERENCE_FIELD_$REF_REQUEST_BODIES_NAMING,
10761076
OPENAPI3_0_REFERENCE_FIELD_$REF_REQUEST_BODIES_NAMING_SCHEMA,
10771077
OPENAPI3_0_REFERENCE_FIELD_$REF_HEADER = 5260300,
1078+
OPENAPI3_0_REFERENCE_FIELD_$REF_PARAMETER = 5260400,
10781079

10791080
OPENAPI3_0_LINK = 5270000,
10801081
OPENAPI3_0_LINK_FIELD_OPERATION_REF_FORMAT_URI = 5270100,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { DiagnosticSeverity } from 'vscode-languageserver-types';
2+
3+
import ApilintCodes from '../../../codes.ts';
4+
import { LinterMeta } from '../../../../apidom-language-types.ts';
5+
import { OpenAPI3 } from '../../target-specs.ts';
6+
7+
const $ref3ParameterNamingLint: LinterMeta = {
8+
code: ApilintCodes.OPENAPI3_0_REFERENCE_FIELD_$REF_PARAMETER,
9+
source: 'apilint',
10+
message: 'OAS3 parameter $Ref should point to Parameter Object',
11+
severity: DiagnosticSeverity.Error,
12+
linterFunction: 'apilintValueRegex',
13+
linterParams: ['^(.*#/components/parameters).*$'],
14+
marker: 'value',
15+
target: '$ref',
16+
targetFields: true,
17+
data: {},
18+
targetSpecs: OpenAPI3,
19+
};
20+
21+
export default $ref3ParameterNamingLint;

packages/apidom-ls/src/config/openapi/parameter/lint/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ import inAuthorizationLint from './in--authorization.ts';
4747
import inContentTypeLint from './in--content-type.ts';
4848
import inAcceptLint from './in--accept.ts';
4949
import inMultipleBody from './in--multiple-body.ts';
50+
import $ref3ParameterNamingLint from './$ref-3-0--parameter.ts';
5051

5152
const lints = [
53+
$ref3ParameterNamingLint,
5254
nameTypeLint,
5355
nameRequiredLint,
5456
inEquals2_0Lint,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: 'test'
5+
paths:
6+
/foo:
7+
parameters:
8+
- $ref: '#/components/headers/MyHeader'
9+
components:
10+
headers:
11+
MyHeader:
12+
description: ID of the user
13+
required: true
14+
schema:
15+
type: string
16+

packages/apidom-ls/test/validate.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3255,6 +3255,66 @@ describe('apidom-ls-validate', function () {
32553255
languageService.terminate();
32563256
});
32573257

3258+
it('oas 3.0 / yaml - OAS3 parameter $Ref should point to Parameter Object', async function () {
3259+
const validationContext: ValidationContext = {
3260+
comments: DiagnosticSeverity.Error,
3261+
maxNumberOfProblems: 100,
3262+
relatedInformation: false,
3263+
};
3264+
3265+
const spec = fs
3266+
.readFileSync(path.join(__dirname, 'fixtures', 'validation', 'oas', 'ref-parameter.yaml'))
3267+
.toString();
3268+
const doc: TextDocument = TextDocument.create('foo://bar/ref-parameter.yaml', 'yaml', 0, spec);
3269+
3270+
const languageService: LanguageService = getLanguageService(contextNoSchema);
3271+
const result = await languageService.doValidation(doc, validationContext);
3272+
3273+
result[0].code = 'test';
3274+
3275+
const expected: Diagnostic[] = [
3276+
{
3277+
code: 'test',
3278+
data: {
3279+
quickFix: [],
3280+
},
3281+
message: 'local reference not found',
3282+
range: {
3283+
end: {
3284+
character: 45,
3285+
line: 7,
3286+
},
3287+
start: {
3288+
character: 14,
3289+
line: 7,
3290+
},
3291+
},
3292+
severity: 1,
3293+
source: 'apilint',
3294+
},
3295+
{
3296+
code: 5260400,
3297+
data: {},
3298+
message: 'OAS3 parameter $Ref should point to Parameter Object',
3299+
range: {
3300+
end: {
3301+
character: 45,
3302+
line: 7,
3303+
},
3304+
start: {
3305+
character: 14,
3306+
line: 7,
3307+
},
3308+
},
3309+
severity: 1,
3310+
source: 'apilint',
3311+
},
3312+
];
3313+
assert.deepEqual(result, expected as Diagnostic[]);
3314+
3315+
languageService.terminate();
3316+
});
3317+
32583318
it('oas / yaml - test editor issue 3626 / inidrect ref', async function () {
32593319
const validationContext: ValidationContext = {
32603320
comments: DiagnosticSeverity.Error,

0 commit comments

Comments
 (0)