Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
swagger: '2.0'
info:
title: Test OpenApi 2 spec
description: Test that our plugins prefer to match responses to least-templated paths over most-templated paths
version: 0.1.0
paths:
? '/preferLeastTemplatedPathOverMostTemplatedPath/{templatedPath}/{templatedPath2}'
: get:
responses:
200:
description: Response body should be a number
schema:
type: number
? '/preferLeastTemplatedPathOverMostTemplatedPath/{templatedPath}/nonTemplatedPath'
: get:
responses:
200:
description: Response body should be a string
schema:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.0.0
info:
title: Test OpenApi 3 spec
description: Test that our plugins prefer to match responses to least-templated paths over most-templated paths
version: 0.1.0
paths:
? /preferLeastTemplatedPathOverMostTemplatedPath/{templatedPath}/{templatedPath2}
: get:
responses:
200:
description: Response body should be a number
content:
application/json:
schema:
type: number
? /preferLeastTemplatedPathOverMostTemplatedPath/{templatedPath}/nonTemplatedPath
: get:
responses:
200:
description: Response body should be a string
content:
application/json:
schema:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
swagger: '2.0'
info:
title: Test OpenApi 2 spec
description: Test that our plugins prefer to match responses to least-templated paths over most-templated paths
version: 0.1.0
paths:
? /preferLeastTemplatedPathOverMostTemplatedPath/{templatedPath}/nonTemplatedPath
: get:
responses:
200:
description: Response body should be a string
schema:
type: string
? /preferLeastTemplatedPathOverMostTemplatedPath/{templatedPath}/{templatedPath2}
: get:
responses:
200:
description: Response body should be a number
schema:
type: number
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.0.0
info:
title: Test OpenApi 3 spec
description: Test that our plugins prefer to match responses to least-templated paths over most-templated paths
version: 0.1.0
paths:
? /preferLeastTemplatedPathOverMostTemplatedPath/{templatedPath}/nonTemplatedPath
: get:
responses:
200:
description: Response body should be a string
content:
application/json:
schema:
type: string
? /preferLeastTemplatedPathOverMostTemplatedPath/{templatedPath}/{templatedPath2}
: get:
responses:
200:
description: Response body should be a number
content:
application/json:
schema:
type: number
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import chai from 'chai';
import path from 'path';

import chaiResponseValidator from '../../..';

const openApiSpecsDir = path.resolve(
'../../commonTestResources/exampleOpenApiFiles/valid/preferLeastTemplatedPathOverMostTemplatedPath',
);
const { expect } = chai;

describe('expect(res).to.satisfyApiSpec (using an OpenAPI spec with similar least-templated and most-templated OpenAPI paths)', () => {
[2, 3].forEach((openApiVersion) => {
describe(`OpenAPI ${openApiVersion}`, () => {
const openApiSpecs = [
{
isLeastTemplatedPathFirst: true,
pathToApiSpec: path.join(
openApiSpecsDir,
'leastTemplatedPathBeforeMostTemplatedPath',
`openapi${openApiVersion}.yml`,
),
},
{
isLeastTemplatedPathFirst: false,
pathToApiSpec: path.join(
openApiSpecsDir,
'leastTemplatedPathAfterMostTemplatedPath',
`openapi${openApiVersion}.yml`,
),
},
];

openApiSpecs.forEach((spec) => {
const { pathToApiSpec, isLeastTemplatedPathFirst } = spec;

describe(`res.req.path matches a least-templated OpenAPI path ${
isLeastTemplatedPathFirst ? 'before' : 'after'
} a templated OpenAPI path`, () => {
const res = {
status: 200,
req: {
method: 'GET',
path:
'/preferLeastTemplatedPathOverMostTemplatedPath/templatedPath/nonTemplatedPath',
},
body: 'valid body (string)',
};

before(() => {
chai.use(chaiResponseValidator(pathToApiSpec));
});

it('passes', () => {
expect(res).to.satisfyApiSpec;
});

it('fails when using .not', () => {
const assertion = () => expect(res).to.not.satisfyApiSpec;
expect(assertion).to.throw(
"not to satisfy the '200' response defined for endpoint 'GET /preferLeastTemplatedPathOverMostTemplatedPath/{templatedPath}/nonTemplatedPath'",
);
});
});
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import path from 'path';

import jestOpenAPI from '../../..';

const openApiSpecsDir = path.resolve(
'../../commonTestResources/exampleOpenApiFiles/valid/preferLeastTemplatedPathOverMostTemplatedPath',
);

describe('expect(res).toSatisfyApiSpec() (using an OpenAPI spec with similar templated and non-templated OpenAPI paths)', () => {
[2, 3].forEach((openApiVersion) => {
describe(`OpenAPI ${openApiVersion}`, () => {
const openApiSpecs = [
{
isLeastTemplatedPathFirst: true,
pathToApiSpec: path.join(
openApiSpecsDir,
'leastTemplatedPathBeforeMostTemplatedPath',
`openapi${openApiVersion}.yml`,
),
},
{
isLeastTemplatedPathFirst: false,
pathToApiSpec: path.join(
openApiSpecsDir,
'leastTemplatedPathAfterMostTemplatedPath',
`openapi${openApiVersion}.yml`,
),
},
];

openApiSpecs.forEach((spec) => {
const { pathToApiSpec, isLeastTemplatedPathFirst } = spec;

describe(`res.req.path matches a non-templated OpenAPI path ${
isLeastTemplatedPathFirst ? 'before' : 'after'
} a templated OpenAPI path`, () => {
const res = {
status: 200,
req: {
method: 'GET',
path:
'/preferLeastTemplatedPathOverMostTemplatedPath/templatedPath/nonTemplatedPath',
},
body: 'valid body (string)',
};

beforeAll(() => {
jestOpenAPI(pathToApiSpec);
});

it('passes', () => {
expect(res).toSatisfyApiSpec();
});

it('fails when using .not', () => {
const assertion = () => expect(res).not.toSatisfyApiSpec();
expect(assertion).toThrow(
"not to satisfy the '200' response defined for endpoint 'GET /preferLeastTemplatedPathOverMostTemplatedPath/{templatedPath}/nonTemplatedPath'",
);
});
});
});
});
});
});
11 changes: 10 additions & 1 deletion packages/openapi-validator/lib/utils/common.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,29 @@ const doesOpenApiPathMatchPathname = (openApiPath, pathname) => {
return doesColonPathMatchPathname(pathInColonForm, pathname);
};

const countPathParams = (openApiPath) => {
return (openApiPath.match(/\{/g) || []).length;
};

export const findOpenApiPathMatchingPossiblePathnames = (
possiblePathnames,
OAPaths,
) => {
let openApiPath;
let nbPathParams = -1;
// eslint-disable-next-line no-restricted-syntax
for (const pathname of possiblePathnames) {
// eslint-disable-next-line no-restricted-syntax
for (const OAPath of OAPaths) {
const count = countPathParams(OAPath);
if (OAPath === pathname) {
return OAPath;
}
if (doesOpenApiPathMatchPathname(OAPath, pathname)) {
openApiPath = OAPath;
if (nbPathParams == -1 || count < nbPathParams) {
nbPathParams = count;
openApiPath = OAPath;
}
}
}
}
Expand Down