From bab45cd6f6749ae3015ab453cbd684bca61dde47 Mon Sep 17 00:00:00 2001 From: Alba Mendez Date: Sun, 2 May 2021 22:09:06 +0200 Subject: [PATCH 1/2] fix content type validation for OAS3 the contentTypeValidation option is currently broken for OAS3 schemas, this PR fixes it --- README.md | 2 +- src/index.js | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7152e81..123faa3 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ If the element is an object, it must include `name` and `definition`. If the ele - `makeOptionalAttributesNullable` - Boolean that forces preprocessing of Swagger schema to include 'null' as possible type for all non-required properties. Main use-case for this is to ensure correct handling of null values when Ajv type coercion is enabled - `ajvConfigBody` - Object that will be passed as config to new Ajv instance which will be used for validating request body. Can be useful to e. g. enable type coercion (to automatically convert strings to numbers etc). See Ajv documentation for supported values. - `ajvConfigParams` - Object that will be passed as config to new Ajv instance which will be used for validating request headers and parameters. See Ajv documentation for supported values. -- `contentTypeValidation` - Boolean that indicates if to perform content type validation in case `consume` field is specified and the request body is not empty. +- `contentTypeValidation` - Boolean that indicates if to perform content type validation in case `consumes` (OAS2) or `content` (OAS3) field is specified and the request body is not empty. - `expectFormFieldsInBody` - Boolean that indicates whether form fields of non-file type that are specified in the schema should be validated against request body (e. g. Multer is copying text form fields to body) - `buildRequests` - Boolean that indicates whether if create validators for requests, default is true. - `buildResponses` - Boolean that indicates whether if create validators for responses, default is false. diff --git a/src/index.js b/src/index.js index 24d12ba..d9b5c36 100644 --- a/src/index.js +++ b/src/index.js @@ -123,8 +123,10 @@ function buildRequestValidator(referenced, dereferenced, currentPath, currentMet } if (localParameters.length > 0 || options.contentTypeValidation) { - requestSchema.parameters = buildParametersValidation(localParameters, - dereferenced.paths[currentPath][currentMethod].consumes || dereferenced.paths[currentPath].consumes || dereferenced.consumes, options); + const contentTypes = isOpenApi3 ? + Object.keys( get(dereferenced.paths[currentPath][currentMethod], 'requestBody.content') || {} ) : + ( dereferenced.paths[currentPath][currentMethod].consumes || dereferenced.paths[currentPath].consumes || dereferenced.consumes ); + requestSchema.parameters = buildParametersValidation(localParameters, contentTypes, options); } return requestSchema; From 4238c6ad03c77a8080fb50295bd940832057adfa Mon Sep 17 00:00:00 2001 From: Alba Mendez Date: Tue, 4 May 2021 01:27:06 +0200 Subject: [PATCH 2/2] support relative server URLs current code throws on relative server URLs, i.e.: servers: - url: /v2 fix it by providing a dummy base to URL --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index d9b5c36..71bdf17 100644 --- a/src/index.js +++ b/src/index.js @@ -37,7 +37,7 @@ function buildValidations(referenced, dereferenced, receivedOptions) { const schemas = {}; const basePaths = dereferenced.servers && dereferenced.servers.length - ? dereferenced.servers.map(({ url }) => new URL(url).pathname) + ? dereferenced.servers.map(({ url }) => new URL(url, 'http://foo').pathname) : [dereferenced.basePath || '/']; Object.keys(dereferenced.paths).forEach(currentPath => {