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 4 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
34 changes: 31 additions & 3 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,11 @@ import {
toValue,
ArraySlice,
ObjectElement,
isArrayElement,
} from '@swagger-api/apidom-core';
import { CompletionItem } from 'vscode-languageserver-types';
import { test } from 'openapi-path-templating';
import { test, resolve } from 'openapi-path-templating';
import { OperationElement, isParameterElement } from '@swagger-api/apidom-ns-openapi-3-0';

// eslint-disable-next-line import/no-cycle
import {
Expand Down Expand Up @@ -1009,9 +1011,35 @@ export const standardLinterfunctions: FunctionItem[] = [
functionName: 'apilintOpenAPIPathTemplateValid',
function: (element: Element) => {
if (isStringElement(element)) {
return true;
const pathItemElement = (element.parent as MemberElement).value as ObjectElement;

if (pathItemElement.length === 0) {
return true;
}

const pathTemplateResolveParams: { [key: string]: string } = {};
char0n marked this conversation as resolved.
Show resolved Hide resolved

pathItemElement.forEach((operation) => {
char0n marked this conversation as resolved.
Show resolved Hide resolved
const parameters = (operation as OperationElement).get('parameters');
char0n marked this conversation as resolved.
Show resolved Hide resolved
if (isArrayElement(parameters)) {
parameters.forEach((parameter) => {
if (isParameterElement(parameter)) {
char0n marked this conversation as resolved.
Show resolved Hide resolved
const allowedLocation = ['path', 'query'];
if (allowedLocation.includes(toValue(parameter.in))) {
pathTemplateResolveParams[toValue(parameter.name) as string] = 'placeholder';
}
}
});
}
});

char0n marked this conversation as resolved.
Show resolved Hide resolved
const pathTemplate = toValue(element);
const resolvedPathTemplate = resolve(pathTemplate, pathTemplateResolveParams);

return !test(resolvedPathTemplate, { strict: true });
}
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,70 @@
openapi: 3.0.0
char0n marked this conversation as resolved.
Show resolved Hide resolved
info:
title: Foo
version: 0.1.0
components:
parameters:
test_id:
name: test_id
in: path
required: true
schema:
type: string
format: uuid
title: Test Id
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 test baz
operationId: deleteFooBarTestBaz
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: {}
/reference/{test_id}/{baz_id}:
get:
summary: Get test baz
operationId: getReferenceTestBaz
parameters:
- $ref: '#/components/parameters/test_id'
responses:
'200':
description: Successful Response
content:
application/json:
schema: {}
50 changes: 50 additions & 0 deletions packages/apidom-ls/test/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3376,6 +3376,56 @@ describe('apidom-ls-validate', function () {

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

languageService.terminate();
});
// eslint-disable-next-line
it.only('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: 15, character: 2 }, end: { line: 15, character: 43 } },
message: 'path template expressions is not matched with Parameter Object(s)',
severity: 1,
code: 3040101,
source: 'apilint',
},
{
range: { start: { line: 40, character: 2 }, end: { line: 40, character: 25 } },
message: 'path template expressions is not matched with Parameter Object(s)',
severity: 1,
code: 3040101,
source: 'apilint',
},
{
range: { start: { line: 58, character: 2 }, end: { line: 58, character: 31 } },
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();
});
});
Loading