From 98cdfad3f8042a13ef9d376f211739ec57279b4c Mon Sep 17 00:00:00 2001 From: Krzysztof Kowalczyk Date: Thu, 4 Jan 2024 13:56:05 +0100 Subject: [PATCH] feat(ls): add lint rule for OpenAPI Operation Parameter defined in path template (#3629) Refs #3546 --- .../services/validation/linter-functions.ts | 13 ++++++++++-- ...eter-defined-within-path-template-2-0.yaml | 17 ++++++++++++++- ...eter-defined-within-path-template-3-0.yaml | 19 ++++++++++++++++- ...eter-defined-within-path-template-3-1.yaml | 18 ++++++++++++++++ packages/apidom-ls/test/validate.ts | 21 +++++++++++++++++++ 5 files changed, 84 insertions(+), 4 deletions(-) diff --git a/packages/apidom-ls/src/services/validation/linter-functions.ts b/packages/apidom-ls/src/services/validation/linter-functions.ts index 17ef0fdc23..efe0b25732 100644 --- a/packages/apidom-ls/src/services/validation/linter-functions.ts +++ b/packages/apidom-ls/src/services/validation/linter-functions.ts @@ -1081,9 +1081,18 @@ export const standardLinterfunctions: FunctionItem[] = [ isArrayElement(element.parent) && includesClasses(['path-item-parameters'], element.parent); - if (!isInPathItemElement) return true; + const isInOperationElement = + isArrayElement(element.parent) && + includesClasses(['operation-parameters'], element.parent); + + if (!isInPathItemElement && !isInOperationElement) return true; + + const pathItemElement: Element | undefined = isInOperationElement + ? element.parent?.parent?.parent?.parent?.parent + : element.parent?.parent?.parent; + + if (pathItemElement?.element !== 'pathItem') return true; - const pathItemElement = element.parent.parent.parent; const isPathItemPartOfPathTemplating = isStringElement(pathItemElement.meta.get('path')); if (!isPathItemPartOfPathTemplating) return true; diff --git a/packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-2-0.yaml b/packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-2-0.yaml index 464a6c9db0..c117b49017 100644 --- a/packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-2-0.yaml +++ b/packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-2-0.yaml @@ -28,4 +28,19 @@ paths: required: true type: string format: uuid - + /test: + get: + summary: Get test + operationId: getTest + responses: + '200': + description: Successful Response + parameters: + - name: x_id + in: path + required: true + type: string + schema: + type: string + format: uuid + title: X Id diff --git a/packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-3-0.yaml b/packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-3-0.yaml index 35837bb26d..cd38c8d913 100644 --- a/packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-3-0.yaml +++ b/packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-3-0.yaml @@ -71,4 +71,21 @@ paths: responses: '202': description: "OK" - + /test: + get: + summary: Get test + operationId: getTest + responses: + '200': + description: Successful Response + content: + application/json: + schema: {} + parameters: + - name: x_id + in: path + required: true + schema: + type: string + format: uuid + title: X Id diff --git a/packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-3-1.yaml b/packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-3-1.yaml index 02f49f282b..e85ec36f77 100644 --- a/packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-3-1.yaml +++ b/packages/apidom-ls/test/fixtures/validation/oas/parameter-defined-within-path-template-3-1.yaml @@ -90,3 +90,21 @@ paths: responses: '202': description: "OK" + /test: + get: + summary: Get test + operationId: getTest + responses: + '200': + description: Successful Response + content: + application/json: + schema: {} + parameters: + - name: x_id + in: path + required: true + schema: + type: string + format: uuid + title: X Id diff --git a/packages/apidom-ls/test/validate.ts b/packages/apidom-ls/test/validate.ts index aee25453c8..227ba66096 100644 --- a/packages/apidom-ls/test/validate.ts +++ b/packages/apidom-ls/test/validate.ts @@ -3586,6 +3586,13 @@ describe('apidom-ls-validate', function () { code: 3102000, source: 'apilint', }, + { + range: { start: { line: 38, character: 10 }, end: { line: 46, character: 0 } }, + message: 'parameter is not defined within path template', + severity: 1, + code: 3102000, + source: 'apilint', + }, ]; assert.deepEqual(result, expected as Diagnostic[]); @@ -3628,6 +3635,13 @@ describe('apidom-ls-validate', function () { code: 3102000, source: 'apilint', }, + { + range: { start: { line: 84, character: 10 }, end: { line: 91, character: 0 } }, + message: 'parameter is not defined within path template', + severity: 1, + code: 3102000, + source: 'apilint', + }, ]; assert.deepEqual(result, expected as Diagnostic[]); @@ -3670,6 +3684,13 @@ describe('apidom-ls-validate', function () { code: 3102000, source: 'apilint', }, + { + range: { start: { line: 103, character: 10 }, end: { line: 110, character: 0 } }, + message: 'parameter is not defined within path template', + severity: 1, + code: 3102000, + source: 'apilint', + }, ]; assert.deepEqual(result, expected as Diagnostic[]);