Skip to content

Commit

Permalink
feat: allow using req.url based on config
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkegg committed Aug 14, 2023
1 parent 13d8c0e commit 486fb66
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/framework/openapi.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ export class OpenApiContext {
public readonly openApiRouteMap = {};
public readonly routes: RouteMetadata[] = [];
public readonly ignoreUndocumented: boolean;
public readonly useRequestUrl: boolean;
private readonly basePaths: string[];
private readonly ignorePaths: RegExp | Function;

constructor(spec: Spec, ignorePaths: RegExp | Function, ignoreUndocumented: boolean = false) {
constructor(
spec: Spec,
ignorePaths: RegExp | Function,
ignoreUndocumented: boolean = false,
useRequestUrl = false,
) {
this.apiDoc = spec.apiDoc;
this.basePaths = spec.basePaths;
this.routes = spec.routes;
this.ignorePaths = ignorePaths;
this.ignoreUndocumented = ignoreUndocumented;
this.buildRouteMaps(spec.routes);
this.useRequestUrl = useRequestUrl;
}

public isManagedRoute(path: string): boolean {
Expand Down
1 change: 1 addition & 0 deletions src/framework/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export interface OpenApiValidatorOpts {
ignoreUndocumented?: boolean;
securityHandlers?: SecurityHandlers;
coerceTypes?: boolean | 'array';
useRequestUrl?: boolean;
/**
* @deprecated
* Use `formats` + `validateFormats` to ignore specified formats
Expand Down
16 changes: 11 additions & 5 deletions src/middlewares/openapi.metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export function applyOpenApiMetadata(
if (openApiContext.shouldIgnoreRoute(path)) {
return next();
}
const matched = lookupRoute(req);
const matched = lookupRoute(req, openApiContext.useRequestUrl);
if (matched) {
const { expressRoute, openApiRoute, pathParams, schema } = matched;
if (!schema) {
// Prevents validation for routes which match on path but mismatch on method
if(openApiContext.ignoreUndocumented) {
if (openApiContext.ignoreUndocumented) {
return next();
}
throw new MethodNotAllowed({
Expand All @@ -54,7 +54,10 @@ export function applyOpenApiMetadata(
// add the response schema if validating responses
(<any>req.openapi)._responseSchema = (<any>matched)._responseSchema;
}
} else if (openApiContext.isManagedRoute(path) && !openApiContext.ignoreUndocumented) {
} else if (
openApiContext.isManagedRoute(path) &&
!openApiContext.ignoreUndocumented
) {
throw new NotFound({
path: req.path,
message: 'not found',
Expand All @@ -63,8 +66,11 @@ export function applyOpenApiMetadata(
next();
};

function lookupRoute(req: OpenApiRequest): OpenApiRequestMetadata {
const path = req.originalUrl.split('?')[0];
function lookupRoute(
req: OpenApiRequest,
useRequestUrl: boolean,
): OpenApiRequestMetadata {
const path = useRequestUrl ? req.url : req.originalUrl.split('?')[0];
const method = req.method;
const routeEntries = Object.entries(openApiContext.expressRouteMap);
for (const [expressRoute, methods] of routeEntries) {
Expand Down
10 changes: 8 additions & 2 deletions src/openapi.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class OpenApiValidator {
if (options.$refParser == null) options.$refParser = { mode: 'bundle' };
if (options.validateFormats == null) options.validateFormats = true;
if (options.formats == null) options.formats = {};
if (options.useRequestUrl == null) options.useRequestUrl = false;

if (typeof options.operationHandlers === 'string') {
/**
Expand Down Expand Up @@ -103,7 +104,12 @@ export class OpenApiValidator {
resOpts,
).preProcess();
return {
context: new OpenApiContext(spec, this.options.ignorePaths, this.options.ignoreUndocumented),
context: new OpenApiContext(
spec,
this.options.ignorePaths,
this.options.ignoreUndocumented,
this.options.useRequestUrl,
),
responseApiDoc: sp.apiDocRes,
error: null,
};
Expand Down Expand Up @@ -201,7 +207,7 @@ export class OpenApiValidator {
return resmw(req, res, next);
})
.catch(next);
})
});
}

// op handler middleware
Expand Down

0 comments on commit 486fb66

Please sign in to comment.