Skip to content

Commit 7adb956

Browse files
refactor: add reference header rule
1 parent 2742a4f commit 7adb956

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,7 @@ enum ApilintCodes {
10741074
OPENAPI3_0_REFERENCE_FIELD_$REF_REQUEST_BODIES = 5260200,
10751075
OPENAPI3_0_REFERENCE_FIELD_$REF_REQUEST_BODIES_NAMING,
10761076
OPENAPI3_0_REFERENCE_FIELD_$REF_REQUEST_BODIES_NAMING_SCHEMA,
1077+
OPENAPI3_0_REFERENCE_FIELD_$REF_HEADER = 5260300,
10771078

10781079
OPENAPI3_0_LINK = 5270000,
10791080
OPENAPI3_0_LINK_FIELD_OPERATION_REF_FORMAT_URI = 5270100,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 $ref3HeaderNamingLint: LinterMeta = {
8+
code: ApilintCodes.OPENAPI3_0_REFERENCE_FIELD_$REF_HEADER,
9+
source: 'apilint',
10+
message: 'OAS3 header $Ref should point to Header Object',
11+
severity: DiagnosticSeverity.Error,
12+
linterFunction: 'parentExistFields',
13+
linterParams: [['header']],
14+
conditions: [
15+
{
16+
targets: [{ path: '$ref' }],
17+
function: 'apilintValueRegex',
18+
params: ['^(?!.*#/components/headers).*$'],
19+
},
20+
],
21+
marker: 'value',
22+
target: '$ref',
23+
data: {},
24+
targetSpecs: OpenAPI3,
25+
};
26+
27+
export default $ref3HeaderNamingLint;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ import minItemsTypeLint from './min-items--type.ts';
3535
import uniqueItemsTypeLint from './unique-items--type.ts';
3636
import enumTypeLint from './enum--type.ts';
3737
import multipleOfTypeLint from './multiple-of--type.ts';
38+
import $ref3HeaderNamingLint from './$ref-3-0--header.ts';
3839

3940
const lints = [
41+
$ref3HeaderNamingLint,
4042
descriptionTypeLint,
4143
requiredTypeLint,
4244
deprecatedTypeLint,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: 'test'
5+
paths:
6+
/foo:
7+
get:
8+
responses:
9+
'200':
10+
description: OK
11+
headers:
12+
X-MyHeader:
13+
$ref: '#/components/schemas/MyHeader'
14+
components:
15+
headers:
16+
MyHeader:
17+
description: ID of the user
18+
required: true
19+
schema:
20+
type: string
21+
schemas:
22+
MyHeader:
23+
description: ID of the user

packages/apidom-ls/test/validate.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3189,6 +3189,72 @@ describe('apidom-ls-validate', function () {
31893189
languageService.terminate();
31903190
});
31913191

3192+
it('oas 3.0 / yaml - OAS3 header $Ref should point to Header Object', async function () {
3193+
const validationContext: ValidationContext = {
3194+
comments: DiagnosticSeverity.Error,
3195+
maxNumberOfProblems: 100,
3196+
relatedInformation: false,
3197+
};
3198+
3199+
const spec = fs
3200+
.readFileSync(path.join(__dirname, 'fixtures', 'validation', 'oas', 'ref-header.yaml'))
3201+
.toString();
3202+
const doc: TextDocument = TextDocument.create('foo://bar/ref-header.yaml', 'yaml', 0, spec);
3203+
3204+
const languageService: LanguageService = getLanguageService(contextNoSchema);
3205+
const result = await languageService.doValidation(doc, validationContext);
3206+
3207+
result[0].code = 'test';
3208+
3209+
const expected: Diagnostic[] = [
3210+
{
3211+
code: 'test',
3212+
data: {
3213+
quickFix: [
3214+
{
3215+
action: 'updateValue',
3216+
functionParams: ['#/components/headers/MyHeader'],
3217+
message: 'update to #/components/headers/MyHeader',
3218+
},
3219+
],
3220+
},
3221+
message: 'local reference not found',
3222+
range: {
3223+
end: {
3224+
character: 51,
3225+
line: 12,
3226+
},
3227+
start: {
3228+
character: 20,
3229+
line: 12,
3230+
},
3231+
},
3232+
severity: 1,
3233+
source: 'apilint',
3234+
},
3235+
{
3236+
code: 5260300,
3237+
data: {},
3238+
message: 'OAS3 header $Ref should point to Header Object',
3239+
range: {
3240+
end: {
3241+
character: 51,
3242+
line: 12,
3243+
},
3244+
start: {
3245+
character: 20,
3246+
line: 12,
3247+
},
3248+
},
3249+
severity: 1,
3250+
source: 'apilint',
3251+
},
3252+
];
3253+
assert.deepEqual(result, expected as Diagnostic[]);
3254+
3255+
languageService.terminate();
3256+
});
3257+
31923258
it('oas / yaml - test editor issue 3626 / inidrect ref', async function () {
31933259
const validationContext: ValidationContext = {
31943260
comments: DiagnosticSeverity.Error,

0 commit comments

Comments
 (0)