Skip to content

Commit cd4cf8b

Browse files
author
nikkegg
committed
feat: allow using req.url based on config
1 parent 0d13f35 commit cd4cf8b

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

src/framework/openapi.context.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,23 @@ export class OpenApiContext {
1111
public readonly openApiRouteMap = {};
1212
public readonly routes: RouteMetadata[] = [];
1313
public readonly ignoreUndocumented: boolean;
14+
public readonly useRequestUrl: boolean;
1415
private readonly basePaths: string[];
1516
private readonly ignorePaths: RegExp | Function;
1617

17-
constructor(spec: Spec, ignorePaths: RegExp | Function, ignoreUndocumented: boolean = false) {
18+
constructor(
19+
spec: Spec,
20+
ignorePaths: RegExp | Function,
21+
ignoreUndocumented: boolean = false,
22+
useRequestUrl = false,
23+
) {
1824
this.apiDoc = spec.apiDoc;
1925
this.basePaths = spec.basePaths;
2026
this.routes = spec.routes;
2127
this.ignorePaths = ignorePaths;
2228
this.ignoreUndocumented = ignoreUndocumented;
2329
this.buildRouteMaps(spec.routes);
30+
this.useRequestUrl = useRequestUrl;
2431
}
2532

2633
public isManagedRoute(path: string): boolean {

src/framework/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export interface OpenApiValidatorOpts {
118118
ignoreUndocumented?: boolean;
119119
securityHandlers?: SecurityHandlers;
120120
coerceTypes?: boolean | 'array';
121+
useRequestUrl?: boolean;
121122
/**
122123
* @deprecated
123124
* Use `formats` + `validateFormats` to ignore specified formats

src/middlewares/openapi.metadata.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ export function applyOpenApiMetadata(
2525
if (openApiContext.shouldIgnoreRoute(path)) {
2626
return next();
2727
}
28-
const matched = lookupRoute(req);
28+
const matched = lookupRoute(req, openApiContext.useRequestUrl);
2929
if (matched) {
3030
const { expressRoute, openApiRoute, pathParams, schema } = matched;
3131
if (!schema) {
3232
// Prevents validation for routes which match on path but mismatch on method
33-
if(openApiContext.ignoreUndocumented) {
33+
if (openApiContext.ignoreUndocumented) {
3434
return next();
3535
}
3636
throw new MethodNotAllowed({
@@ -54,7 +54,10 @@ export function applyOpenApiMetadata(
5454
// add the response schema if validating responses
5555
(<any>req.openapi)._responseSchema = (<any>matched)._responseSchema;
5656
}
57-
} else if (openApiContext.isManagedRoute(path) && !openApiContext.ignoreUndocumented) {
57+
} else if (
58+
openApiContext.isManagedRoute(path) &&
59+
!openApiContext.ignoreUndocumented
60+
) {
5861
throw new NotFound({
5962
path: req.path,
6063
message: 'not found',
@@ -63,8 +66,11 @@ export function applyOpenApiMetadata(
6366
next();
6467
};
6568

66-
function lookupRoute(req: OpenApiRequest): OpenApiRequestMetadata {
67-
const path = req.originalUrl.split('?')[0];
69+
function lookupRoute(
70+
req: OpenApiRequest,
71+
useRequestUrl: boolean,
72+
): OpenApiRequestMetadata {
73+
const path = useRequestUrl ? req.url : req.originalUrl.split('?')[0];
6874
const method = req.method;
6975
const routeEntries = Object.entries(openApiContext.expressRouteMap);
7076
for (const [expressRoute, methods] of routeEntries) {

src/openapi.validator.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class OpenApiValidator {
4949
if (options.$refParser == null) options.$refParser = { mode: 'bundle' };
5050
if (options.validateFormats == null) options.validateFormats = true;
5151
if (options.formats == null) options.formats = {};
52+
if (options.useRequestUrl == null) options.useRequestUrl = false;
5253

5354
if (typeof options.operationHandlers === 'string') {
5455
/**
@@ -103,7 +104,12 @@ export class OpenApiValidator {
103104
resOpts,
104105
).preProcess();
105106
return {
106-
context: new OpenApiContext(spec, this.options.ignorePaths, this.options.ignoreUndocumented),
107+
context: new OpenApiContext(
108+
spec,
109+
this.options.ignorePaths,
110+
this.options.ignoreUndocumented,
111+
this.options.useRequestUrl,
112+
),
107113
responseApiDoc: sp.apiDocRes,
108114
error: null,
109115
};
@@ -201,7 +207,7 @@ export class OpenApiValidator {
201207
return resmw(req, res, next);
202208
})
203209
.catch(next);
204-
})
210+
});
205211
}
206212

207213
// op handler middleware

0 commit comments

Comments
 (0)