Giter Site home page Giter Site logo

Comments (10)

whitlockjc avatar whitlockjc commented on June 23, 2024

I would love to have the feedback of @grahamj, @BigstickCarpet and others.

from swagger-tools.

JamesMessinger avatar JamesMessinger commented on June 23, 2024

Are you saying that swaggerMetadata should overwrite req.params?

from swagger-tools.

whitlockjc avatar whitlockjc commented on June 23, 2024

I'm asking your opinion on the matter. Long story short, I namespaced the parameters swaggerMetadata processed to req.swagger.params just so I didn't muck with req.params. I've gotten some feedback that this could be a little unintuitive and so I wanted to get some feedback on it. What are your thoughts right now about this?

from swagger-tools.

JamesMessinger avatar JamesMessinger commented on June 23, 2024

Ah, ok. Thanks for the clarification. I actually like the way it is now. For a few reasons:

  • Everything else that swaggerMetadata adds to req is namespaced under req.swagger, so this is consistent behavior. The docs pretty clearly point this out, so IMHO, RTFM.
  • Modifying req.params directly could break other middleware. By namespacing all of swaggerMetadata's stuff under req.swagger, you're guaranteed to play nice with other middleware.
  • If people really want the Swagger params directly on the req object, then they can easily add this snippet to their app:
app.use(function(req, res, next) {
    if (req.swagger.params)
        req.params = req.swagger.params;
    next();
});

from swagger-tools.

whitlockjc avatar whitlockjc commented on June 23, 2024

+1

from swagger-tools.

prabhatjha avatar prabhatjha commented on June 23, 2024

Ok so I was the one who suggested it to Jeremy from an "outsider" perspective. My take was that it's one less thing for me to think when I am in controllers since I am so used to req.blah for requests in regular node app.

Coming from Java background, I was thinking something equivalent to:

 import swagger.request;
 var p = request.params;

from swagger-tools.

JamesMessinger avatar JamesMessinger commented on June 23, 2024

I can definitely see why it would be desirable to have Swagger's param data directly at req.params, but the main issue is that it would break any middleware that expects the "normal" req.params object. Since swagger-tools needs to work for a variety of different use-cases, it really needs to play nice with other middleware.

That said, if you're not using any other third-party middleware that relies on req.params, then it's totally safe for you to use the middleware snippet that I posted above, which will overwrite req.params with the Swagger param data

from swagger-tools.

prabhatjha avatar prabhatjha commented on June 23, 2024

Cool. @whitlockjc I think it can be closed then.

from swagger-tools.

6utt3rfly avatar 6utt3rfly commented on June 23, 2024

I know this is coming in late, but I thought I would offer a bit more feedback:

  1. Swagger-tools utilizes tools to set req.query and req.body, but it avoids req.params? Seems like it's breaking the pattern...
  2. Node Express prevents modifying the req.params with middleware, so the suggestion by BigstickCarpet to add middleware would not work.
  3. I think a simple solution would be to modify swagger-router.js to add:
var swaggerParamsToParams = function(req){
  _.forEach(req.swagger.params, function(param, key){
    if (param.schema.in === 'path')
      req.params[key] = param.value;
  })
}

just before calling the handler. That wouldn't break any middleware, but it would also allow the use of the more common req.params variable

from swagger-tools.

whitlockjc avatar whitlockjc commented on June 23, 2024
  1. swagger-tools does nothing of the sort regarding req.body or req.query. The server middleware is responsible for these things. And while swagger-tools provides default middleware for you so things just work, it's up to you to own this if need be. req.swagger is the only object swagger-tools creates/manipulates

  2. swagger-tools does not alter req.params

  3. I don't know that we have a problem here so the solution doesn't make much sense to me

from swagger-tools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.