apigee-127 / swagger-tools

A Node.js and browser module that provides tooling around Swagger.
MIT License
701 stars 373 forks source link

Response validation blocks stream #575

Open JusbeR opened 6 years ago

JusbeR commented 6 years ago

I wrote an API that streams data from another server to client. The problem is that my API buffers the whole stream to memory until it starts sending it to client. This is of course pretty much wrecks the idea. Then I noticed that if I disable swagger validation for the responses { validateResponse: false }, the block is removed and stream+flow control works as they should.

I am just guessing here, but is it so that swagger response validation does not actually allow anything to be written to client until end() is called? https://github.com/apigee-127/swagger-tools/blob/master/middleware/swagger-validator.js#L185

Any ideas what to do here? I would hate to disable the response validation for all the APIs, but apparently I cannot disable that per API.

JusbeR commented 6 years ago

Answering to self: Did not come up with any sane solution so I made an insane hack instead https://github.com/apigee-127/swagger-tools/compare/master...kuha-tnx:feature/ignore-resp-validation

Then just in client:

someApiHandler: (req, res) => {
  ...
  res.swaggerIgnoreResponseValidation = true;
  someStream.pipe(res);
chrisandrews7 commented 6 years ago

+1

barriber commented 2 years ago

@JusbeR Came with same issue lately, instead of forking swagger-validator.js you can do

` app.use((req, res, next) => {

middleware.swaggerValidator({
  validateResponse: req.swagger.apiPath !== {API}
})(req, res, next);

});`