-
-
Notifications
You must be signed in to change notification settings - Fork 211
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
Conversation
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
For anyone else running into this issue, I have a solution that recursively removes all
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
|
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? |
@allenrinmar will fix this shortly, have a PR here: #890 |
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