Supports streaming response validation via streaming_content_parsers option
#447
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.
Background
In the existing implementation, when the response body of streaming events such as Server-Sent Events passes through
Committee::Middleware::ResponseValidation, theresponse_validatemethod from OpenAPI 3 (or HyperSchema) blocks until the entire body is read. As a result, it was not possible to return responses as streams.As a workaround for this issue, the one of the solutions is to use a middleware wrapper like the below to completely skip validation for the corresponding endpoint.
Features Added by This Patch
This patch adds an option called
streaming_content_parserstoCommittee::Middleware::ResponseValidation. This option accepts a hash where the key is a Content-Type and the value is a Proc that receives and transforms the response body of that Content-Type.Content-Types specified via this option bypass the normal path (which waits for the entire body to be read) and instead go through a path via
Rack::BodyProxy. This allows the response to be streamed to the client without blocking, while still enabling validation to be performed on the transformed body via the Proc once the response body is closed.Usage Example
Limitations
For limitations of this feature, please refer to the README.