Skip to content

Commit

Permalink
feat(apidom-ls): add reference test case
Browse files Browse the repository at this point in the history
  • Loading branch information
kowalczyk-krzysztof committed Dec 15, 2023
1 parent 171fe97 commit 5dcdf03
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
20 changes: 4 additions & 16 deletions packages/apidom-ls/src/services/validation/linter-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1015,36 +1011,28 @@ 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)) {
parameters.forEach((parameter) => {
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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: {}
11 changes: 9 additions & 2 deletions packages/apidom-ls/test/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5dcdf03

Please sign in to comment.