Skip to content

Commit 76a3eb5

Browse files
feat: migrate unique parameter name
1 parent 7bcbda0 commit 76a3eb5

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ enum ApilintCodes {
740740
OPENAPI2_PARAMETER_FIELD_ENUM_TYPE = 3101800,
741741
OPENAPI2_PARAMETER_FIELD_MULTIPLE_OF_TYPE = 3101900,
742742
OPENAPI2_PARAMETER_FIELD_IN_PATH_TEMPLATE = 3102000,
743+
OPENAPI2_PARAMETER_FIELD_NAME_UNIQUE = 3103000,
743744

744745
OPENAPI2_ITEMS = 3110000,
745746
OPENAPI2_ITEMS_FIELD_TYPE_EQUALS = 3110100,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import uniqueItemsTypeLint from './unique-items--type.ts';
4242
import enumTypeLint from './enum--type.ts';
4343
import multipleOfTypeLint from './multiple-of--type.ts';
4444
import inPathTemplateLint from './in-path-template.ts';
45+
import uniqueNameLint from './unique-name.ts';
4546

4647
const lints = [
4748
nameTypeLint,
@@ -81,6 +82,7 @@ const lints = [
8182
maxLengthTypeLint,
8283
minLengthTypeLint,
8384
uniqueItemsTypeLint,
85+
uniqueNameLint,
8486
enumTypeLint,
8587
multipleOfTypeLint,
8688
requiredFields3_0__3_1Lint,
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 { OpenAPI2, OpenAPI3 } from '../../target-specs.ts';
6+
7+
const parametersTypeLint: LinterMeta = {
8+
code: ApilintCodes.OPENAPI2_PARAMETER_FIELD_NAME_UNIQUE,
9+
source: 'apilint',
10+
message: 'Name must be unique among all parameters',
11+
severity: DiagnosticSeverity.Error,
12+
linterFunction: 'apilintPropertyUniqueValue',
13+
linterParams: [['parameters'], 'name'],
14+
marker: 'key',
15+
markerTarget: 'name',
16+
target: 'name',
17+
data: {},
18+
targetSpecs: [...OpenAPI2, ...OpenAPI3],
19+
};
20+
21+
export default parametersTypeLint;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: 'test'
5+
paths:
6+
/pets:
7+
get:
8+
responses:
9+
parameters:
10+
- name: pathLevel
11+
in: query
12+
description: tags to filter by
13+
schema:
14+
type: string
15+
- name: pathLevel
16+
in: query
17+
description: tags to filter by
18+
schema:
19+
type: string

packages/apidom-ls/test/validate.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3651,6 +3651,69 @@ describe('apidom-ls-validate', function () {
36513651
languageService.terminate();
36523652
});
36533653

3654+
it('oas 3.0 / yaml - parameter name should be unique', async function () {
3655+
const validationContext: ValidationContext = {
3656+
comments: DiagnosticSeverity.Error,
3657+
maxNumberOfProblems: 100,
3658+
relatedInformation: false,
3659+
};
3660+
3661+
const spec = fs
3662+
.readFileSync(
3663+
path.join(__dirname, 'fixtures', 'validation', 'oas', 'parameter-unique-name-3-0.yaml'),
3664+
)
3665+
.toString();
3666+
const doc: TextDocument = TextDocument.create(
3667+
'foo://bar/parameter-unique-name-3-0.yaml',
3668+
'yaml',
3669+
0,
3670+
spec,
3671+
);
3672+
3673+
const languageService: LanguageService = getLanguageService(contextNoSchema);
3674+
3675+
const result = await languageService.doValidation(doc, validationContext);
3676+
const expected: Diagnostic[] = [
3677+
{
3678+
code: 3103000,
3679+
data: {},
3680+
message: 'Name must be unique among all parameters',
3681+
range: {
3682+
end: {
3683+
character: 14,
3684+
line: 9,
3685+
},
3686+
start: {
3687+
character: 10,
3688+
line: 9,
3689+
},
3690+
},
3691+
severity: 1,
3692+
source: 'apilint',
3693+
},
3694+
{
3695+
code: 3103000,
3696+
data: {},
3697+
message: 'Name must be unique among all parameters',
3698+
range: {
3699+
end: {
3700+
character: 14,
3701+
line: 14,
3702+
},
3703+
start: {
3704+
character: 10,
3705+
line: 14,
3706+
},
3707+
},
3708+
severity: 1,
3709+
source: 'apilint',
3710+
},
3711+
];
3712+
assert.deepEqual(result, expected as Diagnostic[]);
3713+
3714+
languageService.terminate();
3715+
});
3716+
36543717
it('oas 3.1 / yaml - parameter object should be defined within path template', async function () {
36553718
const validationContext: ValidationContext = {
36563719
comments: DiagnosticSeverity.Error,

0 commit comments

Comments
 (0)