fix(openapi): memory leak in large file handling within oas-normalize
#784
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧰 Changes
This bumps oas-normalize, @readme/openapi-parser, and @readme/better-ajv-errors in order to resolve a pretty serious issue with large OpenAPI definitions causing memory leak issues within validation.
For some context,
@readme/better-ajv-errors
uses @babel/code-frame to generate error messages like the following:The problem we're having though is that
@babel/code-frame
was running code highlighting on supplied OpenAPI JSON definitions for each error we were processing. In the case that spawned this discovery, the Cloudflare API schema, is upwards of 4MB formatted and contains well over 1,000 errors.@babel/code-frame
was highlighting this 4MB file over 1,000 times.To resolve this I've implemented a couple fixes:
@readme/better-ajv-errors
. As it was always highlighting our JSON as green It added nothing of value to me at the expense it cost.1@readme/openapi-parser
. Even with the removal of code highlighting still generating code frames for 1,000+ errors was a massively expensive operation so I've modified@readme/openapi-parser
to cap the amount of errors we show at 20 if the file is over a certain size. When this happens our validation error messaging will also now tell the user that they have an additional amount of errors that they will see when they re-run validation after fixing the errors we've shown them.2🧬 QA & Testing
Without the above fixes running
openapi:validate
on this Cloudflare API definition took 15 minutes because I gave up and killed the process.With these dependency updates re-running
openapi:validate
on the same API definition now only surfaced the first 20 errors, took 13 seconds, and told the user that they have an additional 1016 errors.After fixing the most common error in this API definition,
4xx
response HTTP codes needing to be4XX
, validation now returned 2 errors, finished in 7 seconds, and didn't tell the user they any additional errors.Like I mentioned in readmeio/openapi-parser#180 I don't love that we're masking errors for these situations but we don't have any control over
@babel/code-frame
, there aren't any better alternatives for it out there, and I have no intention of trying to figure out ways to hack or fork a better solution into it.Footnotes
https://github.com/readmeio/openapi-parser/pull/180 ↩
https://github.com/readmeio/openapi-parser/pull/180 ↩