Skip to content

Commit

Permalink
feat(apidom-ls): more semantic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kowalczyk-krzysztof committed Dec 15, 2023
1 parent c272193 commit 171fe97
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
56 changes: 36 additions & 20 deletions packages/apidom-ls/src/services/validation/linter-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import {
toValue,
ArraySlice,
ObjectElement,
isArrayElement,
} from '@swagger-api/apidom-core';
import { CompletionItem } from 'vscode-languageserver-types';
import { test, parse } from 'openapi-path-templating';
import { test, resolve } from 'openapi-path-templating';
import {
OperationElement,
ParameterElement,
isParameterElement,
} from '@swagger-api/apidom-ns-openapi-3-0';

// eslint-disable-next-line import/no-cycle
import {
Expand Down Expand Up @@ -1009,30 +1015,40 @@ export const standardLinterfunctions: FunctionItem[] = [
functionName: 'apilintOpenAPIPathTemplateValid',
function: (element: Element) => {
if (isStringElement(element)) {
const parseResult = parse(toValue(element));
const parts: string[] = [];
parseResult.ast.translate(parts);
const parameterElements: ParameterElement[] = [];

const templateExpressions = parts
.filter((part) => part[0] === 'template-expression')
.map((part) => part[1].slice(1, -1));
const pathItemElement = (element.parent as MemberElement).value as ObjectElement;

const httpVerbsWithParameters: {
[key: string]: {
parameters: {
name: string;
}[];
};
} = element.parent.toValue().value;
if (pathItemElement.length === 0) {
return true;
}

pathItemElement.forEach((operation) => {
const parameters = (operation as OperationElement).get('parameters');
if (isArrayElement(parameters)) {
parameters.forEach((parameter) => {
if (isParameterElement(parameter)) {
const allowedLocation = ['path', 'query'];
if (allowedLocation.includes(toValue(parameter.in))) {
parameterElements.push(parameter);
}
}
});
}
});

const allExpressionsHaveMatchingParameter = Object.values(httpVerbsWithParameters).every(
({ parameters }) => {
return templateExpressions.every((expression) =>
parameters.find(({ name }) => name === expression),
);
const pathTemplateResolveParams = parameterElements.reduce(
(params: { [key: string]: string }, parameterElement) => {
params[toValue(parameterElement.name) as string] = 'placeholder'; // eslint-disable-line no-param-reassign
return params;
},
{},
);
return allExpressionsHaveMatchingParameter;

const pathTemplate = toValue(element);
const resolvedPathTemplate = resolve(pathTemplate, pathTemplateResolveParams);

return !test(resolvedPathTemplate, { strict: true });
}

return false;
Expand Down
3 changes: 2 additions & 1 deletion packages/apidom-ls/test/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3378,7 +3378,8 @@ describe('apidom-ls-validate', function () {

languageService.terminate();
});
it('oas / yaml - every path template should be defined', async function () {
// eslint-disable-next-line
it.only('oas / yaml - every path template should be defined', async function () {
const validationContext: ValidationContext = {
comments: DiagnosticSeverity.Error,
maxNumberOfProblems: 100,
Expand Down

0 comments on commit 171fe97

Please sign in to comment.