Skip to content

Commit

Permalink
fix ignorePaths #245
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Feb 25, 2020
1 parent 104bb54 commit 41b30bd
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/framework/openapi.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ export class OpenApiContext {

public isManagedRoute(path: string): boolean {
for (const bp of this.basePaths) {
if (path.startsWith(bp) && !this.ignorePaths?.test(path)) {
if (path.startsWith(bp) && !this.shouldIgnoreRoute(path)) {
return true;
}
}
return false;
}

public shouldIgnoreRoute(path: string) {
return this.ignorePaths?.test(path);
}

public routePair(route: string): RoutePair {
const methods = this.methods(route);
if (methods) {
Expand Down
7 changes: 5 additions & 2 deletions src/middlewares/openapi.metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export function applyOpenApiMetadata(
openApiContext: OpenApiContext,
): OpenApiRequestHandler {
return (req: OpenApiRequest, res: Response, next: NextFunction): void => {
if (openApiContext.shouldIgnoreRoute(req.path)) {
return next();
}
const matched = lookupRoute(req);
if (matched) {
const { expressRoute, openApiRoute, pathParams, schema } = matched;
Expand All @@ -25,11 +28,11 @@ export function applyOpenApiMetadata(
} else if (openApiContext.isManagedRoute(req.path)) {
req.openapi = {};
}
-next();
next();
};

function lookupRoute(req: OpenApiRequest): OpenApiRequestMetadata {
const path = req.originalUrl.split("?")[0];
const path = req.originalUrl.split('?')[0];
const method = req.method;
const routeEntries = Object.entries(openApiContext.expressRouteMap);
for (const [expressRoute, methods] of routeEntries) {
Expand Down
28 changes: 19 additions & 9 deletions test/ignore.paths.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@ describe(packageJson.name, () => {
let basePath = null;

before(async () => {
const apiSpec = path.join('test', 'resources', 'openapi.yaml');
const apiSpec = path.join('test', 'resources', 'ignore.paths.yaml');
app = await createApp(
{ apiSpec, ignorePaths: /.*\/hippies$/ },
3005,
app => {
app.all('/v1/hippies', (req, res) => {
res.json([{ id: 1, name: 'farah' }, { id: 2, name: 'fred' }]);
res.json([
{ id: 1, name: 'farah' },
{ id: 2, name: 'fred' },
]);
});
app.get('/v1/hippies/1', (req, res) => {
res.json({ id: 1, name: 'farah' });
});
app.get('/v1/pets/1', (req, res) => {
res.json({ id: 1, name: 'sparky' });
});
// app.get('/v1/route_defined_in_express_only', (req, res) => {
// res.json({ hi: 'there' });
// });
},
);
basePath = app.basePath;
Expand All @@ -29,7 +38,7 @@ describe(packageJson.name, () => {

it('should ignore path and return 200, rather than validate', async () =>
request(app)
.get(`${basePath}/hippies?test`)
.get(`${basePath}/hippies`)
.query({
test: 'one',
limit: 2,
Expand Down Expand Up @@ -73,22 +82,23 @@ describe(packageJson.name, () => {
});
});

it('should validate a route defined in openapi but not express', async () =>
it('should validate a route defined in openapi but not express with invalid params', async () =>
request(app)
.get(`${basePath}/route_not_defined_within_express`)
.get(`${basePath}/route_defined_in_openapi_only`)
.expect(400)
.then(r => {
const e = r.body.errors;
expect(e[0].message).to.equal("should have required property 'name'");
expect(e[0].message).to.equal("should have required property 'id'");
}));

it('should return 404 if route is defined in swagger but not express', async () =>
it('should return 404 if route is defined in openapi but not express and params are valid', async () =>
request(app)
.get(`${basePath}/route_not_defined_within_express`)
.query({ name: 'test' })
.get(`${basePath}/route_defined_in_openapi_only`)
.query({ id: 123 })
.expect(404)
.then(r => {
const e = r.body;
console.log(e)
// There is no route defined by express, hence the validator verifies parameters,
// then it fails over to the express error handler. In this case returns empty
expect(e).to.be.empty;
Expand Down
74 changes: 74 additions & 0 deletions test/resources/ignore.paths.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
description: A sample API
termsOfService: http://swagger.io/terms/
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
- url: /v1

paths:
/hippies:
get:
description: hippies
operationId: hippies
parameters:
- name: name
in: query
required: true
schema:
type: string
responses:
"200":
description: user response

/pets/{id}:
get:
description: Returns a user based on a single ID, if the user does not have access to the pet
operationId: find pet by id
parameters:
- name: id
in: path
description: ID of pet to fetch
required: true
schema:
type: integer
format: int64
responses:
"200":
description: pet response
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"

/route_defined_in_openapi_only:
get:
description: defined here only
parameters:
- name: id
in: query
required: true
schema:
type: integer
format: int64
responses:
"200":
description: test

components:
schemas:
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string

0 comments on commit 41b30bd

Please sign in to comment.