Skip to content

Commit

Permalink
feat(apidom-ls): remove Operation.parameter handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kowalczyk-krzysztof committed Jan 3, 2024
1 parent 4192c5c commit 12223e3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 87 deletions.
47 changes: 18 additions & 29 deletions packages/apidom-ls/src/services/validation/linter-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1073,46 +1073,35 @@ export const standardLinterfunctions: FunctionItem[] = [
functionName: 'apilintOpenAPIParameterFieldIsDefinedWithinPathTemplate',
function: (element: Element) => {
if (element.element === 'parameter') {
const parameterName = toValue((element as ObjectElement).get('name'));
const parameterLocation = toValue((element as ObjectElement).get('in'));
const isInPathItemElement =
isArrayElement(element.parent) &&
includesClasses(['path-item-parameters'], element.parent);
const isChildOfOperationElement = element.parent.parent.parent.element === 'operation';

const getAST = (pathTemplate: string) => {
const parseResult = parse(pathTemplate);
const parts: [string, string][] = [];
parseResult.ast.translate(parts);
return parts;
};

const pathTemplateASTIncludesParameter = (ast: [string, string][]) =>
ast.findIndex(
([name, value]) => name === 'template-expression-param-name' && value === parameterName,
) > -1;
if (!isInPathItemElement || parameterLocation !== 'path') {
return true;
}

const getPathTemplate = (): string => {
if (isInPathItemElement) {
return toValue(element.parent.parent.parent.meta.get('path'));
}
if (isChildOfOperationElement) {
return toValue(
(element.parent.parent.parent.parent.parent.parent as MemberElement).key,
);
}
return '';
};
const pathItemElement = element.parent.parent.parent;
const isPathItemPartOfPathTemplating = isStringElement(pathItemElement.meta.get('path'));

if (parameterLocation !== 'path') {
if (!isPathItemPartOfPathTemplating) {
return true;
}

if (isInPathItemElement || isChildOfOperationElement) {
return pathTemplateASTIncludesParameter(getAST(getPathTemplate()));
}
const pathTemplate = toValue(pathItemElement.meta.get('path'));
const parameterName = toValue((element as ObjectElement).get('name'));

return true;
const parseResult = parse(pathTemplate);
const parts: [string, string][] = [];
parseResult.ast.translate(parts);

const pathTemplateASTIncludesParameter = (ast: [string, string][]) =>
ast.findIndex(
([name, value]) => name === 'template-expression-param-name' && value === parameterName,
) > -1;

return pathTemplateASTIncludesParameter(parts);
}
return true;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ paths:
delete:
summary: Delete foo bar id
operationId: deleteFooBar
parameters:
- name: test_id
in: path
required: true
type: string
schema:
type: string
format: uuid
title: Test Id
responses:
'200':
description: Successful Response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ paths:
delete:
summary: Delete foo bar id
operationId: deleteFooBar
parameters:
- name: test_id
in: path
required: true
schema:
type: string
format: uuid
title: Test Id
responses:
'200':
description: Successful Response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ paths:
delete:
summary: Delete foo bar id
operationId: deleteFooBar
parameters:
- name: test_id
in: path
required: true
schema:
type: string
format: uuid
title: Test Id
responses:
'200':
description: Successful Response
Expand Down
36 changes: 3 additions & 33 deletions packages/apidom-ls/test/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3580,14 +3580,7 @@ describe('apidom-ls-validate', function () {
const result = await languageService.doValidation(doc, validationContext);
const expected: Diagnostic[] = [
{
range: { start: { line: 10, character: 8 }, end: { line: 17, character: 24 } },
message: 'parameter is not defined within path template',
severity: 1,
code: 3102000,
source: 'apilint',
},
{
range: { start: { line: 25, character: 8 }, end: { line: 32, character: 23 } },
range: { start: { line: 16, character: 8 }, end: { line: 23, character: 23 } },
message: 'parameter is not defined within path template',
severity: 1,
code: 3102000,
Expand Down Expand Up @@ -3629,14 +3622,7 @@ describe('apidom-ls-validate', function () {
const result = await languageService.doValidation(doc, validationContext);
const expected: Diagnostic[] = [
{
range: { start: { line: 10, character: 8 }, end: { line: 16, character: 24 } },
message: 'parameter is not defined within path template',
severity: 1,
code: 3102000,
source: 'apilint',
},
{
range: { start: { line: 24, character: 8 }, end: { line: 30, character: 23 } },
range: { start: { line: 16, character: 8 }, end: { line: 22, character: 23 } },
message: 'parameter is not defined within path template',
severity: 1,
code: 3102000,
Expand Down Expand Up @@ -3678,28 +3664,12 @@ describe('apidom-ls-validate', function () {
const result = await languageService.doValidation(doc, validationContext);
const expected: Diagnostic[] = [
{
range: { start: { line: 10, character: 8 }, end: { line: 16, character: 24 } },
range: { start: { line: 16, character: 8 }, end: { line: 22, character: 23 } },
message: 'parameter is not defined within path template',
severity: 1,
code: 3102000,
source: 'apilint',
},
{
code: 3102000,
message: 'parameter is not defined within path template',
range: {
end: {
character: 23,
line: 30,
},
start: {
character: 8,
line: 24,
},
},
severity: 1,
source: 'apilint',
},
];
assert.deepEqual(result, expected as Diagnostic[]);

Expand Down

0 comments on commit 12223e3

Please sign in to comment.