Skip to content

Commit cbf939c

Browse files
committed
Increase compatibility with openapi-factory.
1 parent 6cb14df commit cbf939c

3 files changed

Lines changed: 21 additions & 42 deletions

File tree

docs/async-example.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,7 @@ class ModelValidator {
6262
}
6363

6464
startValidation(request) {
65-
const newRequest = {
66-
method: request.httpMethod,
67-
headers: request.headers,
68-
query: request.queryStringParameters,
69-
body: request.body,
70-
path: request.pathParameters,
71-
route: request.route
72-
};
73-
74-
this.validationAsync = this.getValidator().then(validator => validator(newRequest));
65+
this.validationAsync = this.getValidator().then(validator => validator(request));
7566
// Ensure validation may never be called, and in those cases, we want to avoid an uncaught exception
7667
this.validationAsync.catch(() => {});
7768
}

src/framework/types.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,16 @@ export interface OpenAPIFrameworkAPIContext {
420420
}
421421

422422
export interface OpenApiRequest {
423-
route: string,
424-
method: string,
425-
query?: Record<string, string>,
426-
headers?: Record<string, string>,
427-
body?: string,
428-
signedCookies?: Record<string, string>,
429-
cookies?: Record<string, string>,
430-
path?: Record<string, unknown>
423+
route: string;
424+
method?: string;
425+
httpMethod?: string
426+
query?: Record<string, string>;
427+
queryStringParameters?: Record<string, string>;
428+
headers?: Record<string, string>;
429+
body?: string;
430+
cookies?: Record<string, string>;
431+
path?: Record<string, unknown>;
432+
pathParameters?: Record<string, unknown>;
431433
}
432434

433435
export type OpenApiRequestHandler = (

src/middlewares/openapi.request.validator.ts

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ export class RequestValidator {
6868
// cache middleware by combining method, path, and contentType
6969
const contentType = ContentType.from(req);
7070
const contentTypeKey = contentType.equivalents()[0] ?? 'not_provided';
71-
const key = `${req.method}-${route}-${contentTypeKey}`.toLocaleLowerCase();
71+
const key = `${req.method || req.httpMethod}-${route}-${contentTypeKey}`.toLocaleLowerCase();
7272

7373
if (!this.middlewareCache[key]) {
7474
if (this.validationModule[key]) {
7575
this.middlewareCache[key] = this.buildMiddlewareFromModule(key);
7676
} else {
77-
this.middlewareCache[key] = this.buildMiddleware(key, route, req.method, contentType);
77+
this.middlewareCache[key] = this.buildMiddleware(key, route, req.method || req.httpMethod, contentType);
7878
}
7979
}
8080

@@ -147,18 +147,11 @@ export class RequestValidator {
147147

148148
private buildMiddlewareFromModule(validationId: string): OpenApiRequestHandler {
149149
return (req: OpenApiRequest): void => {
150-
const cookies = req.cookies
151-
? {
152-
...req.cookies,
153-
...req.signedCookies
154-
}
155-
: undefined;
156-
157150
const data = {
158-
query: req.query ?? {},
159-
headers: req.headers || {},
160-
path: req.path || {},
161-
cookies,
151+
query: req.query ?? req.queryStringParameters ?? {},
152+
headers: req.headers ?? {},
153+
path: req.path ?? req.pathParameters ?? {},
154+
cookies: req.cookies ?? {},
162155
body: req.body
163156
};
164157

@@ -208,18 +201,11 @@ export class RequestValidator {
208201
const validator = this.ajv.compile(schemaGeneral);
209202

210203
return (req: OpenApiRequest): void => {
211-
const cookies = req.cookies
212-
? {
213-
...req.cookies,
214-
...req.signedCookies
215-
}
216-
: undefined;
217-
218204
const data = {
219-
query: req.query ?? {},
220-
headers: req.headers || {},
221-
path: req.path || {},
222-
cookies,
205+
query: req.query ?? req.queryStringParameters ?? {},
206+
headers: req.headers ?? {},
207+
path: req.path ?? req.pathParameters ?? {},
208+
cookies: req.cookies ?? {},
223209
body: req.body
224210
};
225211

0 commit comments

Comments
 (0)