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

fix #229: Validation errors when paths have multiple path parameters #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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,27 @@
swagger: '2.0'
info:
title: Test OpenApi 2 spec
description: Test that our plugins prefer to match responses to less generic paths over more generic paths
version: 0.1.0
paths:
'/preferLessGenericPathOverMoreGenericPath/{templatedPath}/{forCodeCoverage}':
get:
responses:
200:
description: Response body should be a number
schema:
type: number
'/preferLessGenericPathOverMoreGenericPath/{templatedPath}/{templatedPath2}':
get:
responses:
200:
description: Response body should be a number
schema:
type: number
'/preferLessGenericPathOverMoreGenericPath/{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,33 @@
openapi: 3.0.0
info:
title: Test OpenApi 3 spec
description: Test that our plugins prefer to match responses to less generic paths over more generic paths
version: 0.1.0
paths:
/preferLessGenericPathOverMoreGenericPath/{templatedPath}/{forCodeCoverage}:
get:
responses:
200:
description: Response body should be a number
content:
application/json:
schema:
type: number
/preferLessGenericPathOverMoreGenericPath/{templatedPath}/{templatedPath2}:
get:
responses:
200:
description: Response body should be a number
content:
application/json:
schema:
type: number
/preferLessGenericPathOverMoreGenericPath/{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 less generic paths over more generic paths
version: 0.1.0
paths:
/preferLessGenericPathOverMoreGenericPath/{templatedPath}/nonTemplatedPath:
get:
responses:
200:
description: Response body should be a string
schema:
type: string
/preferLessGenericPathOverMoreGenericPath/{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 less generic paths over more generic paths
version: 0.1.0
paths:
/preferLessGenericPathOverMoreGenericPath/{templatedPath}/nonTemplatedPath:
get:
responses:
200:
description: Response body should be a string
content:
application/json:
schema:
type: string
/preferLessGenericPathOverMoreGenericPath/{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/preferLessGenericPathOverMoreGenericPath',
);
const { expect } = chai;

describe('expect(res).to.satisfyApiSpec (using an OpenAPI spec with similar less generic paths and more generic OpenAPI paths)', () => {
[2, 3].forEach((openApiVersion) => {
describe(`OpenAPI ${openApiVersion}`, () => {
const openApiSpecs = [
{
isLessGenericPathFirst: true,
pathToApiSpec: path.join(
openApiSpecsDir,
'lessGenericPathBeforeMoreGenericPath',
`openapi${openApiVersion}.yml`,
),
},
{
isLessGenericPathFirst: false,
pathToApiSpec: path.join(
openApiSpecsDir,
'lessGenericPathAfterMoreGenericPath',
`openapi${openApiVersion}.yml`,
),
},
];

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

describe(`res.req.path matches a least-templated OpenAPI path ${
isLessGenericPathFirst ? 'before' : 'after'
} a templated OpenAPI path`, () => {
const res = {
status: 200,
req: {
method: 'GET',
path:
'/preferLessGenericPathOverMoreGenericPath/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 /preferLessGenericPathOverMoreGenericPath/{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/preferLessGenericPathOverMoreGenericPath',
);

describe('expect(res).toSatisfyApiSpec() (using an OpenAPI spec with similar less generic paths and more generic OpenAPI paths)', () => {
[2, 3].forEach((openApiVersion) => {
describe(`OpenAPI ${openApiVersion}`, () => {
const openApiSpecs = [
{
isLessGenericPathFirst: true,
pathToApiSpec: path.join(
openApiSpecsDir,
'lessGenericPathBeforeMoreGenericPath',
`openapi${openApiVersion}.yml`,
),
},
{
isLessGenericPathFirst: false,
pathToApiSpec: path.join(
openApiSpecsDir,
'lessGenericPathAfterMoreGenericPath',
`openapi${openApiVersion}.yml`,
),
},
];

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

describe(`res.req.path matches a non-templated OpenAPI path ${
isLessGenericPathFirst ? 'before' : 'after'
} a templated OpenAPI path`, () => {
const res = {
status: 200,
req: {
method: 'GET',
path:
'/preferLessGenericPathOverMoreGenericPath/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 /preferLessGenericPathOverMoreGenericPath/{templatedPath}/nonTemplatedPath'",
);
});
});
});
});
});
});
42 changes: 41 additions & 1 deletion packages/openapi-validator/lib/utils/common.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,40 @@ const doesOpenApiPathMatchPathname = (openApiPath, pathname) => {
return doesColonPathMatchPathname(pathInColonForm, pathname);
};

const compareOpenApiPathsByGenericity = (openApiPath1, openApiPath2) => {
// genericity is a based on the idea that a path with a template parameters is more generic
// than a path without any template parameter
// simple examples:
// "/a" == "/b"
// "/{foo}" == "/{bar}"
// "/{foo}" > "/a"
// "/a" < "/{foo}"
// examples with templated prefix:
// "/{hello}/a" == "/{bye}/b"
// "/{hello}/{foo}" == "/{bye}/{bar}"
// "/{hello}/{foo}" > "/{bye}/a"
// "/{hello}/a" < "/{bye}/{foo}"
// examples with hardcoded prefix:
// "/hello/a" == "/bye/b"
// "/hello/{foo}" == "/bye/{bar}"
// "/hello/{foo}" > "/bye/a"
// "/hello/a" < "/bye/{foo}"
const pathElements1 = openApiPath1.substring(1).split(/\//);
const pathElements2 = openApiPath2.substring(1).split(/\//);
for (let i = 0; i < pathElements1.length && i < pathElements2.length; i++) {
const isTemplateElement1 = pathElements1[i][0] == '{';
const isTemplateElement2 = pathElements2[i][0] == '{';
if (isTemplateElement1 && !isTemplateElement2) {
return 1;
} else if (!isTemplateElement1 && isTemplateElement2) {
return -1;
}
}
// returning 0 is valid because this function is called with paths of the same length,
// so we don't have to compare "/{foo}/a" and "/{bar}" for instance.
return 0;
};

export const findOpenApiPathMatchingPossiblePathnames = (
possiblePathnames,
OAPaths,
Expand All @@ -34,7 +68,13 @@ export const findOpenApiPathMatchingPossiblePathnames = (
return OAPath;
}
if (doesOpenApiPathMatchPathname(OAPath, pathname)) {
openApiPath = OAPath;
// favor OAPath if it is least generic than openApiPath
if (
!openApiPath ||
compareOpenApiPathsByGenericity(OAPath, openApiPath) < 0
) {
openApiPath = OAPath;
}
}
}
}
Expand Down