Skip to content

Commit

Permalink
feat(apidom-ls): after comment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kowalczyk-krzysztof committed Jan 2, 2024
1 parent 8946e5f commit ccf0fcf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
31 changes: 17 additions & 14 deletions packages/apidom-ls/src/services/validation/linter-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
isArrayElement,
} from '@swagger-api/apidom-core';
import { CompletionItem } from 'vscode-languageserver-types';
import { test, resolve } from 'openapi-path-templating';
import { test, resolve, parse } from 'openapi-path-templating';

// eslint-disable-next-line import/no-cycle
import {
Expand Down Expand Up @@ -1072,26 +1072,29 @@ export const standardLinterfunctions: FunctionItem[] = [
functionName: 'apilintOpenAPIParameterFieldIsDefinedWithinPathTemplate',
function: (element: Element) => {
if (element.element === 'parameter') {
const allowedLocations = ['path', 'query'];
const parameterToValue: ObjectElement = toValue(element);
const parameterName = parameterToValue.get('name');
const parameterLocation = parameterToValue.get('in');
const parameterName = toValue((element as ObjectElement).get('name'));
const parameterLocation = toValue((element as ObjectElement).get('in'));

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

const isChildOfOperationElement = (el: Element): boolean =>
el.parent.parent.parent.element === 'operation';

if (isChildOfOperationElement(element)) {
const pathTemplate: string = toValue(
(element.parent.parent.parent.parent.parent.parent as MemberElement).key,
);
const pathTemplateASTIncludesParameter = (ast: [string, string][]) =>
ast.findIndex(
([name, value]) => name === 'template-expression-param-name' && value === parameterName,
) > -1;

if (!allowedLocations.includes(parameterLocation)) {
return true;
}
if (isChildOfOperationElement(element)) {
const pathTemplate = toValue(element.parent.parent.parent.parent.parent.meta.get('path'));
const parseResult = parse(pathTemplate);
const parts: [string, string][] = [];

return pathTemplate.includes(parameterName);
parseResult.ast.translate(parts);

// TODO: handle case when parameter is not child of operation element
return pathTemplateASTIncludesParameter(parts);
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ parameters:
name: test_id
in: path
required: true
schema:
type: string
format: uuid
title: Test Id
type: string
paths:
/foo/{bar_id}:
delete:
Expand All @@ -20,17 +17,11 @@ paths:
- name: foo_id
in: path
required: true
schema:
type: string
format: uuid
title: Foo Id
type: string
- name: bar_id
in: path
required: true
schema:
type: string
format: uuid
title: Bar Id
type: string
responses:
'200':
description: Successful Response
Expand Down
8 changes: 4 additions & 4 deletions packages/apidom-ls/test/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3580,10 +3580,10 @@ describe('apidom-ls-validate', function () {
const result = await languageService.doValidation(doc, validationContext);
const expected: Diagnostic[] = [
{
range: { start: { line: 19, character: 10 }, end: { line: 25, character: 25 } },
range: { start: { line: 16, character: 10 }, end: { line: 19, character: 22 } },
message: 'parameter is not defined within path template',
severity: 1,
code: 3090100,
code: 3102000,
source: 'apilint',
},
];
Expand Down Expand Up @@ -3625,7 +3625,7 @@ describe('apidom-ls-validate', function () {
range: { start: { line: 20, character: 10 }, end: { line: 26, character: 25 } },
message: 'parameter is not defined within path template',
severity: 1,
code: 3090100,
code: 3102000,
source: 'apilint',
},
];
Expand Down Expand Up @@ -3667,7 +3667,7 @@ describe('apidom-ls-validate', function () {
range: { start: { line: 20, character: 10 }, end: { line: 26, character: 25 } },
message: 'parameter is not defined within path template',
severity: 1,
code: 3090100,
code: 3102000,
source: 'apilint',
},
];
Expand Down

0 comments on commit ccf0fcf

Please sign in to comment.