apigee-127 / swagger-tools

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

Adding middleware after swagger middleware, not seeing req.swagger #574

Closed garrettsutula closed 6 years ago

garrettsutula commented 6 years ago

Opening this to get a little guidance on adding middleware that can access req.swagger after it's added by the swagger metadata middleware. I have the following code for initializing the middleware:

SwaggerExpress.create(config, function(err, swaggerExpress) {
  if (err) { throw err; }

  // install middleware
  swaggerExpress.register(app);

  app.use(function(req, res, next){
    console.log(req.swagger);
  });

  var port = process.env.PORT || 10010;
  app.listen(port);

  if (swaggerExpress.runner.swagger.paths['/hello']) {
    console.log('try this:\ncurl http://127.0.0.1:' + port + '/hello?name=Scott');
  }
});

but req.swagger is undefined when that middleware runs.

If I follow the pattern in the docs, I still have the same problem which is demonstrated in this repo

whitlockjc commented 6 years ago

https://github.com/apigee-127/swagger-tools/blob/master/docs/Middleware.md#swagger-middleware-debugging

whitlockjc commented 6 years ago

Since this isn't a bug, I'm closing but we can continue talking.

garrettsutula commented 6 years ago

Thanks for the quick response & fair point on closing it. :)

I loaded up debug and I'm honestly not sure why it's not working. It seems like middleware is associated in the correct order, after swaggerMetadata:


Thu, 17 May 2018 19:25:51 GMT express:application set "etag" to 'weak'
Thu, 17 May 2018 19:25:51 GMT express:application set "etag fn" to [Function: generateETag]
Thu, 17 May 2018 19:25:51 GMT express:application set "env" to 'development'
Thu, 17 May 2018 19:25:51 GMT express:application set "query parser" to 'extended'
Thu, 17 May 2018 19:25:51 GMT express:application set "query parser fn" to [Function: parseExtendedQueryString]
Thu, 17 May 2018 19:25:51 GMT express:application set "subdomain offset" to 2
Thu, 17 May 2018 19:25:51 GMT express:application set "trust proxy" to false
Thu, 17 May 2018 19:25:51 GMT express:application set "trust proxy fn" to [Function: trustNone]
Thu, 17 May 2018 19:25:51 GMT express:application booting in development mode
Thu, 17 May 2018 19:25:51 GMT express:application set "view" to [Function: View]
Thu, 17 May 2018 19:25:51 GMT express:application set "views" to 'c:\\Users\\702412862\\Documents\\GitHub\\swagger-middleware-test\\views'
Thu, 17 May 2018 19:25:51 GMT express:application set "jsonp callback name" to 'callback'
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware Initializing middleware
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware   Identified Swagger version: 2.0
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware   Validation: succeeded
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware:metadata Initializing swagger-metadata middleware
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware:metadata   Identified Swagger version: 2.0
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware:metadata   Found Path: /hello
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware:metadata   Found Path: /swagger
Thu, 17 May 2018 19:25:51 GMT express:router use '/' query
Thu, 17 May 2018 19:25:51 GMT express:router:layer new '/'
Thu, 17 May 2018 19:25:51 GMT express:router use '/' expressInit
Thu, 17 May 2018 19:25:51 GMT express:router:layer new '/'
Thu, 17 May 2018 19:25:51 GMT express:router use '/' swaggerMetadata
Thu, 17 May 2018 19:25:51 GMT express:router:layer new '/'
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware:router Initializing swagger-router middleware
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware:router   Mock mode: disabled
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware:router   Controllers:
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware:router     c:\Users\702412862\Documents\GitHub\swagger-middleware-test\api\controllers\hello_world.js:
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware:router       hello_world_hello
Thu, 17 May 2018 19:25:51 GMT express:router use '/' swaggerRouter
Thu, 17 May 2018 19:25:51 GMT express:router:layer new '/'
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware:ui Initializing swagger-ui middleware
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware:ui   Using swagger-ui from: internal
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware:ui   API Docs path: /api-docs
Thu, 17 May 2018 19:25:51 GMT swagger-tools:middleware:ui   swagger-ui path: /docs
Thu, 17 May 2018 19:25:51 GMT express:router use '/' swaggerUI
Thu, 17 May 2018 19:25:51 GMT express:router:layer new '/'
Thu, 17 May 2018 19:25:51 GMT express:router use '/' <anonymous>                         <---------------------this is my middleware
Thu, 17 May 2018 19:25:51 GMT express:router:layer new '/'```
whitlockjc commented 6 years ago

Make a request and see if swagger-tools:middleware:metadata finds a matching route for the request.