Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apidom-ls): path template match parameter object lint #3538

Merged
merged 13 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions packages/apidom-ls/src/services/validation/linter-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ObjectElement,
} from '@swagger-api/apidom-core';
import { CompletionItem } from 'vscode-languageserver-types';
import { test } from 'openapi-path-templating';
import { test, parse } from 'openapi-path-templating';

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

const templateExpressions = parts
char0n marked this conversation as resolved.
Show resolved Hide resolved
.filter((part) => part[0] === 'template-expression')
.map((part) => part[1].slice(1, -1));
char0n marked this conversation as resolved.
Show resolved Hide resolved

const httpVerbsWithParameters: {
[key: string]: {
parameters: {
name: string;
}[];
};
} = element.parent.toValue().value;

const allExpressionsHaveMatchingParameter = Object.values(httpVerbsWithParameters).every(
({ parameters }) => {
return templateExpressions.every((expression) =>
parameters.find(({ name }) => name === expression),
);
},
);
return allExpressionsHaveMatchingParameter;
}
return true;

return false;
kowalczyk-krzysztof marked this conversation as resolved.
Show resolved Hide resolved
},
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
openapi: 3.0.0
char0n marked this conversation as resolved.
Show resolved Hide resolved
info:
title: Foo
version: 0.1.0
paths:
char0n marked this conversation as resolved.
Show resolved Hide resolved
/foo/bar/{baz}/test/{foo_id}/baz/{bar_id}:
delete:
summary: Delete foo bar baz
operationId: >-
deleteFooBarBaz
parameters:
- name: foo_id
in: path
required: true
schema:
type: string
format: uuid
title: Foo Id
- name: bar_id
in: path
required: true
schema:
type: string
format: uuid
title: Bar Id
responses:
'200':
description: Successful Response
content:
application/json:
schema: {}
/test/{foo_id}/{bar_id}:
get:
summary: Get test foo bar
operationId: >-
getTestFooBar
parameters:
- name: foo_id
in: path
required: true
schema:
type: string
format: uuid
title: Foo Id
responses:
'200':
description: Successful Response
content:
application/json:
schema: {}
42 changes: 42 additions & 0 deletions packages/apidom-ls/test/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3376,6 +3376,48 @@ describe('apidom-ls-validate', function () {

assert.deepEqual(result, expected as Diagnostic[]);

languageService.terminate();
});
it('oas / yaml - every path template should be defined', async function () {
const validationContext: ValidationContext = {
comments: DiagnosticSeverity.Error,
maxNumberOfProblems: 100,
relatedInformation: false,
};

const spec = fs
.readFileSync(
path.join(__dirname, 'fixtures', 'validation', 'oas', 'path-template-all-defined.yaml'),
)
.toString();
const doc: TextDocument = TextDocument.create(
'foo://bar/path-template-all-defined.yaml',
'yaml',
0,
spec,
);

const languageService: LanguageService = getLanguageService(contextNoSchema);

const result = await languageService.doValidation(doc, validationContext);
const expected: Diagnostic[] = [
{
range: { start: { line: 5, character: 2 }, end: { line: 5, character: 43 } },
message: 'path template expressions is not matched with Parameter Object(s)',
severity: 1,
code: 3040101,
source: 'apilint',
},
{
range: { start: { line: 31, character: 2 }, end: { line: 31, character: 25 } },
message: 'path template expressions is not matched with Parameter Object(s)',
severity: 1,
code: 3040101,
source: 'apilint',
},
];
assert.deepEqual(result, expected as Diagnostic[]);

languageService.terminate();
});
});