Add middleware for validating responses against OpenAPI schema - #311
Open
Carifio24 wants to merge 11 commits into
Open
Add middleware for validating responses against OpenAPI schema#311Carifio24 wants to merge 11 commits into
Carifio24 wants to merge 11 commits into
Conversation
…d in the same way.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR adds something that I've wanted to do since I added in the OpenAPI documentation in #265, which is to add a way to validate the format of each response against what the docstring spec declares for that endpoint. This is done in the form of a middleware using
express-openapi-validator.Because our spec file is generated dynamically from the docstring comments, this required a bit of finagling so that we can get the order of generate the spec -> create the middleware -> create the endpoints to happen correctly. It also necessitates building the app before running the test suite. Adding this in also exposed several places where the OpenAPI docstring comments were incorrect. This PR makes some fixes to the comments, as well as cleans up the actual responses for both consistency and to more easily satisfy the spec.
The OpenAPI validator here is quite strict, so I don't think we want this in production - we don't have to return a 500 because of some minor spec mismatch. But it's very nice for testing purposes, so this PR sets this middleware to only be attached if
NODE_ENVis set to"test". Overall this should help ensure that our actual API functionality and our OpenAPI spec are in agreement.