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 7 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
53 changes: 50 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,10 @@ 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';

// eslint-disable-next-line import/no-cycle
import {
Expand Down Expand Up @@ -1009,9 +1010,55 @@ 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 parameterElements: Element[] = [];
const isParameterElement = (el: Element): boolean => el.element === 'parameter';

pathItemElement.forEach((el) => {
if (el.element === 'parameters') {
char0n marked this conversation as resolved.
Show resolved Hide resolved
const pathItemParameterElements = pathItemElement.get('parameters');
if (isArrayElement(pathItemParameterElements)) {
pathItemParameterElements.forEach((parameter) => {
if (isParameterElement(parameter)) {
parameterElements.push(parameter);
}
});
}
}
if (el.element === 'operation') {
const operationParameterElements = (el as ObjectElement).get('parameters');
if (isArrayElement(operationParameterElements)) {
operationParameterElements.forEach((parameter) => {
if (isParameterElement(parameter)) {
parameterElements.push(parameter);
}
});
}
}
});

const allowedLocation = ['path', 'query'];
const pathTemplateResolveParams: { [key: string]: 'placeholder' } = {};

parameterElements.forEach((parameter) => {
char0n marked this conversation as resolved.
Show resolved Hide resolved
if (allowedLocation.includes(toValue((parameter as ObjectElement).get('in')))) {
pathTemplateResolveParams[toValue((parameter as ObjectElement).get('name'))] =
'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,104 @@
swagger: '2.0'
info:
title: Foo
version: 0.1.0
parameters:
test_id:
name: test_id
in: path
required: true
schema:
type: string
format: uuid
title: Test Id
paths:
/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: '#/parameters/test_id'
responses:
'200':
description: Successful Response
content:
application/json:
schema: {}
/just_parameters_object/{x_id}/{y_id}:
parameters:
- name: x_id
in: path
required: true
schema:
type: string
format: uuid
title: X Id
/both_parameters_and_operations_object/{a_id}/{b_id}/{c_id}:
get:
summary: Get both parameters and operations object a b c
operationId: getBothParametersAndOperationsObjectABC
parameters:
- name: b_id
in: path
required: true
schema:
type: string
format: uuid
title: B Id
responses:
'200':
description: Successful Response
content:
application/json:
schema: {}
parameters:
- name: a_id
in: path
required: true
schema:
type: string
format: uuid
title: A Id
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
openapi: 3.0.0
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:
/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: {}
/just_parameters_object/{x_id}/{y_id}:
parameters:
- name: x_id
in: path
required: true
schema:
type: string
format: uuid
title: X Id
/both_parameters_and_operations_object/{a_id}/{b_id}/{c_id}:
get:
summary: Get both parameters and operations object a b c
operationId: getBothParametersAndOperationsObjectABC
parameters:
- name: b_id
in: path
required: true
schema:
type: string
format: uuid
title: B Id
responses:
'200':
description: Successful Response
content:
application/json:
schema: {}
parameters:
- name: a_id
in: path
required: true
schema:
type: string
format: uuid
title: A Id
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
openapi: 3.1.0
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:
/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: {}
/just_parameters_object/{x_id}/{y_id}:
parameters:
- name: x_id
in: path
required: true
schema:
type: string
format: uuid
title: X Id
/both_parameters_and_operations_object/{a_id}/{b_id}/{c_id}:
get:
summary: Get both parameters and operations object a b c
operationId: getBothParametersAndOperationsObjectABC
parameters:
- name: b_id
in: path
required: true
schema:
type: string
format: uuid
title: B Id
responses:
'200':
description: Successful Response
content:
application/json:
schema: {}
parameters:
- name: a_id
in: path
required: true
schema:
type: string
format: uuid
title: A Id
Loading