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

Remove examples from apiDoc when validating requests #774

Merged
merged 1 commit into from
Aug 16, 2023

Conversation

HairyMike
Copy link
Contributor

I'd like to remove the examples from the api doc in the request validator. I don't think they're needed while validating the request.

This could be a way to bypass this weird bug: #773

@josecampana
Copy link

meanwhile you merge it, I'm disabling the request validation

@@ -42,6 +42,8 @@ export class RequestValidator {
) {
this.middlewareCache = {};
this.apiDoc = apiDoc;
// Examples not needed for validation
delete this.apiDoc.components.examples;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will only remove top level examples under components. how are other handled?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also needs to be applied to openapi.response.validator.ts

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging as is. It will be great to also add this to the response validator as suggested

@dan-trewin
Copy link

dan-trewin commented May 23, 2023

For anyone else running into this issue, I have a solution that recursively removes all examples keys from the OpenAPI spec for request and response validation. Here is a patch-package solution for anyone wanting to fix this before a PR is approved/merged:

express-openapi-validator+5.0.4.patch

diff --git a/node_modules/express-openapi-validator/dist/middlewares/openapi.request.validator.js b/node_modules/express-openapi-validator/dist/middlewares/openapi.request.validator.js
index e3e8df4..3ef5b30 100644
--- a/node_modules/express-openapi-validator/dist/middlewares/openapi.request.validator.js
+++ b/node_modules/express-openapi-validator/dist/middlewares/openapi.request.validator.js
@@ -7,12 +7,34 @@ const types_1 = require("../framework/types");
 const body_parse_1 = require("./parsers/body.parse");
 const schema_parse_1 = require("./parsers/schema.parse");
 const req_parameter_mutator_1 = require("./parsers/req.parameter.mutator");
+
+function removeExamplesKeys(obj) {
+	if (typeof obj === "object" && obj !== null) {
+		if (Array.isArray(obj)) {
+			obj.forEach(function (item) {
+				removeExamplesKeys(item);
+			});
+		} else {
+			for (var key in obj) {
+				if (key === "examples") {
+					delete obj[key];
+				} else {
+					removeExamplesKeys(obj[key]);
+				}
+			}
+		}
+	}
+}
+
 class RequestValidator {
     constructor(apiDoc, options = {}) {
         this.middlewareCache = {};
         this.requestOpts = {};
         this.middlewareCache = {};
         this.apiDoc = apiDoc;
+        // Examples not needed for validation. The following line is needed to bypass weird "id" example bug. 
+        // See: https://github.com/cdimascio/express-openapi-validator/issues/773 and https://github.com/cdimascio/express-openapi-validator/pull/774
+        removeExamplesKeys(this.apiDoc);
         this.requestOpts.allowUnknownQueryParameters =
             options.allowUnknownQueryParameters;
         this.ajv = (0, ajv_1.createRequestAjv)(apiDoc, Object.assign(Object.assign({}, options), { coerceTypes: true }));
diff --git a/node_modules/express-openapi-validator/dist/middlewares/openapi.response.validator.js b/node_modules/express-openapi-validator/dist/middlewares/openapi.response.validator.js
index ea14edf..ba398ec 100644
--- a/node_modules/express-openapi-validator/dist/middlewares/openapi.response.validator.js
+++ b/node_modules/express-openapi-validator/dist/middlewares/openapi.response.validator.js
@@ -7,10 +7,32 @@ const util_1 = require("./util");
 const types_1 = require("../framework/types");
 const mediaTypeParser = require("media-typer");
 const contentTypeParser = require("content-type");
+
+function removeExamplesKeys(obj) {
+	if (typeof obj === "object" && obj !== null) {
+		if (Array.isArray(obj)) {
+			obj.forEach(function (item) {
+				removeExamplesKeys(item);
+			});
+		} else {
+			for (var key in obj) {
+				if (key === "examples") {
+					delete obj[key];
+				} else {
+					removeExamplesKeys(obj[key]);
+				}
+			}
+		}
+	}
+}
+
 class ResponseValidator {
     constructor(openApiSpec, options = {}, eovOptions = {}) {
         this.validatorsCache = {};
         this.spec = openApiSpec;
+        // Examples not needed for validation. The following line is needed to bypass weird "id" example bug. 
+        // See: https://github.com/cdimascio/express-openapi-validator/issues/773 and https://github.com/cdimascio/express-openapi-validator/pull/774
+        removeExamplesKeys(this.spec);
         this.ajvBody = (0, ajv_1.createResponseAjv)(openApiSpec, options);
         this.eovOptions = eovOptions;
         // This is a pseudo-middleware function. It doesn't get registered with

@allenrinmar
Copy link

I'm curious what the status of this PR is. We've entered a similar issue in testing an upgrade to v5 and this looks to solve it. Does anyone need help that I could assist with?

@cdimascio cdimascio merged commit 950d429 into cdimascio:master Aug 16, 2023
@cdimascio
Copy link
Owner

@allenrinmar will fix this shortly, have a PR here: #890

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants