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

Support for ignoring validation based on specific method and path combinations #991

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
10 changes: 6 additions & 4 deletions src/framework/openapi.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ export class OpenApiContext {
this.serial = spec.serial;
}

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

public shouldIgnoreRoute(path: string) {
return typeof this.ignorePaths === 'function' ? this.ignorePaths(path) : this.ignorePaths?.test(path);
public shouldIgnoreRoute(path: string, method: string): boolean {
return typeof this.ignorePaths === 'function'
? this.ignorePaths(path, method)
: this.ignorePaths?.test(path);
}

public routePair(route: string): RoutePair {
Expand Down
9 changes: 6 additions & 3 deletions src/middlewares/openapi.metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export function applyOpenApiMetadata(
const path = req.path.startsWith(req.baseUrl)
? req.path
: `${req.baseUrl}/${req.path}`;
if (openApiContext.shouldIgnoreRoute(path)) {
const { method } = req;
if (openApiContext.shouldIgnoreRoute(path, method)) {
return next();
}
const matched = lookupRoute(req, openApiContext.useRequestUrl);
Expand Down Expand Up @@ -56,7 +57,7 @@ export function applyOpenApiMetadata(
(<any>req.openapi)._responseSchema = (<any>matched)._responseSchema;
}
} else if (
openApiContext.isManagedRoute(path) &&
openApiContext.isManagedRoute(path, method) &&
!openApiContext.ignoreUndocumented
) {
throw new NotFound({
Expand All @@ -71,7 +72,9 @@ export function applyOpenApiMetadata(
req: OpenApiRequest,
useRequestUrl: boolean,
): OpenApiRequestMetadata {
const path = useRequestUrl ? req.url.split('?')[0] : req.originalUrl.split('?')[0];
const path = useRequestUrl
? req.url.split('?')[0]
: req.originalUrl.split('?')[0];
const method = req.method;
const routeEntries = Object.entries(openApiContext.expressRouteMap);
for (const [expressRoute, methods] of routeEntries) {
Expand Down
18 changes: 17 additions & 1 deletion test/ignore.paths.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ describe('ignorePaths as Function', () => {
const apiSpec = path.join('test', 'resources', 'ignore.paths.yaml');

app = await createApp(
{ apiSpec, ignorePaths: (path) => path.endsWith('/hippies') },
{
apiSpec,
ignorePaths: (path, method) =>
path.endsWith('/hippies') && ['GET', 'POST'].includes(method),
},
3005,
app => {
app.all('/v1/hippies', (req, res) => {
Expand All @@ -121,6 +125,7 @@ describe('ignorePaths as Function', () => {
{ id: 2, name: 'fred' },
]);
});

app.get('/v1/hippies/1', (req, res) => {
res.json({ id: 1, name: 'farah' });
});
Expand Down Expand Up @@ -156,6 +161,17 @@ describe('ignorePaths as Function', () => {
.expect('Content-Type', /json/)
.expect(200));

it('should not ignore path and return 405', async () =>
request(app)
.put(`${basePath}/hippies`)
.query({
test: 'one',
limit: 2,
})
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(405));

it('should not ignore path and return 404', async () =>
request(app)
.get(`${basePath}/hippies/1`)
Expand Down
Loading