From 5dcdf03093930be156f5a705ca0ea6fa950891e6 Mon Sep 17 00:00:00 2001 From: Krzysztof Kowalczyk Date: Fri, 15 Dec 2023 11:01:25 +0100 Subject: [PATCH] feat(apidom-ls): add reference test case --- .../services/validation/linter-functions.ts | 20 +++---------- .../oas/path-template-all-defined.yaml | 30 +++++++++++++++---- packages/apidom-ls/test/validate.ts | 11 +++++-- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/packages/apidom-ls/src/services/validation/linter-functions.ts b/packages/apidom-ls/src/services/validation/linter-functions.ts index 764da1c02a..6091c068b4 100644 --- a/packages/apidom-ls/src/services/validation/linter-functions.ts +++ b/packages/apidom-ls/src/services/validation/linter-functions.ts @@ -12,11 +12,7 @@ import { } from '@swagger-api/apidom-core'; import { CompletionItem } from 'vscode-languageserver-types'; import { test, resolve } from 'openapi-path-templating'; -import { - OperationElement, - ParameterElement, - isParameterElement, -} from '@swagger-api/apidom-ns-openapi-3-0'; +import { OperationElement, isParameterElement } from '@swagger-api/apidom-ns-openapi-3-0'; // eslint-disable-next-line import/no-cycle import { @@ -1015,14 +1011,14 @@ export const standardLinterfunctions: FunctionItem[] = [ functionName: 'apilintOpenAPIPathTemplateValid', function: (element: Element) => { if (isStringElement(element)) { - const parameterElements: ParameterElement[] = []; - const pathItemElement = (element.parent as MemberElement).value as ObjectElement; if (pathItemElement.length === 0) { return true; } + const pathTemplateResolveParams: { [key: string]: string } = {}; + pathItemElement.forEach((operation) => { const parameters = (operation as OperationElement).get('parameters'); if (isArrayElement(parameters)) { @@ -1030,21 +1026,13 @@ export const standardLinterfunctions: FunctionItem[] = [ if (isParameterElement(parameter)) { const allowedLocation = ['path', 'query']; if (allowedLocation.includes(toValue(parameter.in))) { - parameterElements.push(parameter); + pathTemplateResolveParams[toValue(parameter.name) as string] = 'placeholder'; } } }); } }); - const pathTemplateResolveParams = parameterElements.reduce( - (params: { [key: string]: string }, parameterElement) => { - params[toValue(parameterElement.name) as string] = 'placeholder'; // eslint-disable-line no-param-reassign - return params; - }, - {}, - ); - const pathTemplate = toValue(element); const resolvedPathTemplate = resolve(pathTemplate, pathTemplateResolveParams); diff --git a/packages/apidom-ls/test/fixtures/validation/oas/path-template-all-defined.yaml b/packages/apidom-ls/test/fixtures/validation/oas/path-template-all-defined.yaml index 295e6d3648..7b01e0454d 100644 --- a/packages/apidom-ls/test/fixtures/validation/oas/path-template-all-defined.yaml +++ b/packages/apidom-ls/test/fixtures/validation/oas/path-template-all-defined.yaml @@ -2,12 +2,21 @@ 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 baz - operationId: >- - deleteFooBarBaz + summary: Delete foo bar test baz + operationId: deleteFooBarTestBaz parameters: - name: foo_id in: path @@ -32,8 +41,7 @@ paths: /test/{foo_id}/{bar_id}: get: summary: Get test foo bar - operationId: >- - getTestFooBar + operationId: getTestFooBar parameters: - name: foo_id in: path @@ -48,3 +56,15 @@ paths: 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: {} diff --git a/packages/apidom-ls/test/validate.ts b/packages/apidom-ls/test/validate.ts index 2f59e04be7..5ef6ddce19 100644 --- a/packages/apidom-ls/test/validate.ts +++ b/packages/apidom-ls/test/validate.ts @@ -3403,14 +3403,21 @@ describe('apidom-ls-validate', function () { const result = await languageService.doValidation(doc, validationContext); const expected: Diagnostic[] = [ { - range: { start: { line: 5, character: 2 }, end: { line: 5, character: 43 } }, + 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: 31, character: 2 }, end: { line: 31, character: 25 } }, + 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,