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

Fix Mutation of API Spec #537

Merged
merged 2 commits into from Feb 14, 2021
Merged

Conversation

ahilke
Copy link
Contributor

@ahilke ahilke commented Feb 12, 2021

Fixes #535

I found the line that mutates the parameters causing the error here: https://github.com/cdimascio/express-openapi-validator/blob/v4.10.11/src/middlewares/parsers/schema.preprocessor.ts#L489

It doesn't seem easy to change that behaviour thoug. About the idea of using a set, this seems to me like it could hide some actual errors. So I opted for just cloning the spec passed to middleware(). That should avoid other unexpected behaviour, too.

Add failing test case for
cdimascio#535.

The problem manifests itself when calling `middleware()` with an object
multiple times, as the object itself is mutated which might cause errors
on subsequent calls.

In this specific example, the parameters for GET /ping/{value} are
modified in such a way that a second call will cause a validation error.
This happens in `preprocessPathLevelParameters`.
Clone the API specification passed to the `middleware()` function. This
avoids mutating the `apiSpec` parameter passed to that function, which
can lead to unexpected behaviour.

Fixes cdimascio#535.
@cdimascio cdimascio merged commit 2866ce6 into cdimascio:master Feb 14, 2021
@ahilke ahilke deleted the fix-mutation-of-apispec branch February 15, 2021 12:23
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.

Error: schema is invalid when calling middleware multiple times
2 participants