apigee-127 / swagger-node-runner

The heart of Swagger-Node
MIT License
102 stars 123 forks source link

writing controllers as a pipe (ctx.output overwritten by Bagpipes) #94

Open thisdotrob opened 7 years ago

thisdotrob commented 7 years ago

I am writing a controller as a pipe per the release notes for v0.7.1. The comments there say we should:

leave your response body on the ctx.output, the status on ctx.statusCode, and the headers on ctx.headers

Here is my implemented controller:

function helloPipe(ctx, next) {
  var name = ctx.request.swagger.params.name.value || 'stranger';
  var hello = util.format('Hello, %s!', name);

  ctx.output = hello;
  ctx.statusCode = 200;

  next();
}

However, this doesn't work, as ctx.output is overwritten by the Bagpipes module in its postFlight function: https://github.com/apigee-127/bagpipes/blob/master/lib/bagpipes.js#L208-L221

The only way I could get it to work was by instead passing the output as the second argument to next(), or by defining a dummy 'output' property in default.yaml on the _router fitting