Comments (26)
I think the problem is that Swagger isn't designed around the regular Express middleware paradigm (of course, finding documentation about that for Express is also challenge). Should we not be using something like app.use(swagger)
instead of swagger.setAppHandler(app)
?
from swagger-node.
IMO it'd be lovely to design in a way that's composable and uses the tools express provides :). Then it can be used everywhere, with any express setup.
For example, routes can easily be handled using express.Router
:
var swagger = new Swagger();
// since we're in swagger land, can use an extended API
swagger.putResource({ /* .. */ });
// now we're ready to apply it to an app instance
var app = express();
// generate and return an express.Router instance from our config
app.use(swagger.routes())
from swagger-node.
👍 👍
Also - yes the connect/express middleware docs are really bad.
from swagger-node.
@eddieajau I see this module not so much as middleware but more as a wrapper around express
apps.
With that in mind, I'd like to change the interface in such a way that it requires the appHandler
on creation I.E.
var express = require('express');
var Swagger = require('swagger-node-express');
var app = express();
var mySwagger = new Swagger(app);
mySwagger.addModles([/*...*/]);
mySwagger.addResources([/*...*/]);
I'd also like to completely remove error handling logic as it should be defined on your application anyway.
Going with this approach would also solve versioning and probably many others. We could then do something like this:
var v1Swagger = new Swagger(app, {prefix: '/v1'});
var v2Swagger = new Swagger(app, {prefix: '/v2'});
v1Swagger.addModels([/*These are now isolated from v2*/]);
v2Swagger.addModels([/*These are now isolated from v1*/]);
cc @fehguy @enterline @therealklanni @tracker1
from swagger-node.
@jsdevel +1
I completely agree with you. Multiple Swagger instances should make things much cleaner, especially when handling multiple API versions.
from swagger-node.
Thanks @rabchev! Luckily @enterline got us most of the way there with 9246582. A lot of the (anti-)?patterns (like error handling and headers etc.) were added prior to his commit. I feel as you do that being clean is pretty important at this point. We've been using swagger a lot and loving it! However, the code is still a bit scrappy and can be cleaned up even more. Cleaning it up will cause breaking changes, but perhaps @fehguy can weigh in on what the appetite is around here for doing that.
from swagger-node.
Love the input and cleaning up this library. @jsdevel we can create a new minor version (2.1) if we break the API
from swagger-node.
Sounds great @fehguy! Thanks for replying. Shall we continue to make the PRs against master?
from swagger-node.
@jsdevel i rebased the develop branch--let's use that for breaking changes.
from swagger-node.
I vote for @eddieajau's approach: var swaggerInstance = swagger(); app.use(swaggerInstance)
. It's the node/express/'plays well with others' way. Wrapping express is actively anti-modular.
Also breaking API changes = major version change surely?
from swagger-node.
+1 @eddieajau. I think Swagger should work better with Express
from swagger-node.
+1 on getting this sorted.
from swagger-node.
@timruffles @eddieajau I disagree with "swagger should be middleware". Much of what swagger does is register operations (PUT
, GET
etc.). If swagger were middelware, it would have to handle all that manually. Correct me if I'm wrong.
I see swagger as more of an "app wrapper". var swagger = new Swagger(app);
. To me this is perfectly acceptable, allows swagger to fully leverage express, and isn't too far from an acceptable look and feel.
from swagger-node.
@timruffles I'm not sure I see how that's much different from this:
var app = express();
var swagger = new Swagger(app);
swagger.putResource({/*..*/});
This way is terser and leverages express just fine. As middleware, swagger would only be given the req
, res
, and next
objects. Wouldn't it then have to manually parse req.path
to know which action to take?
from swagger-node.
It wouldn't have to be middleware. As said, I recommend using generating an express.Router instance from the swagger config. That way you get all the access you want, but leave how to apply the routes to the express app up to the library user.
from swagger-node.
Internally, I believe swagger calls app.<verb>
which appears to accomplish what using express.Router
does.
from swagger-node.
Right - but that was it's hidden from library uses, so it can't compose with another library. e.g take swagger's router, pass it into a another library that works with routers, etc etc.
I can't see any downsides to playing nicely with the express ecosystem.
from swagger-node.
I can't see how anyone could oppose playing better with the ecosystem but it seems to me that it would drastically expand the addressable userbase if swagger-node-express could be pushed down the stack to Connect, eliminating the dependency on Express, which is already a very thin layer offering what amounts to only a bit of user level sugar on top of the core stack functionality. I recognize it would require slightly more robust dependency management but it would immediately facilitate far more robust and modular API assembly along the lines of things like Connect-Architect
from swagger-node.
@hillct I think we're stuck with express given the name of this module.
from swagger-node.
As entertaining as that is, it's not a good reason to forego the significant advantage that pushing swagger down the stack really offers.
from swagger-node.
@hillct if I'm not mistaken, express 4.0 has completely removed connect as a dependency. I agree in principle with keeping things modular and reusable. I fail to see how the changes I've proposed break that. There are several modules out there already that are meant to be used with express
and not connect
.
from swagger-node.
@jsdevel @hillct I do think you are both making very valid points. Swagger for node could become much lighter and portable with some refactoring.
As @jsdevel has pointed out, this particular implementation is geared towards express with the hope that it provides a very fast, low-configuration mechanism to integrate with Swagger. I would love to see this get forked or started as a pure "swagger-connect" middleware project.
Does it make sense to fork this project to satisfy that goal?
from swagger-node.
@fehguy that makes sense to me.
I just opened #131 to remove the error handling. Test included.
from swagger-node.
@timruffles see #131. What are your thoughts on that approach? It moves more towards leveraging express which I believe is in line with the desires expressed herein.
from swagger-node.
@jsdevel as you say, Express has 'removed' Connect as a dependency but it's done so by establishing a full middleware support layer that's essentially a duplicate of Connect such that what were formerly Connect-compatible middleware plugins are now Connect/Express compatible. I should have been clearer, that I'm not arguing against the changes you propose. I'm arguing that a relatively small amount of additional refactoring would yield a module that would be compatible with both Express and Connect.
Also, I completely agree with your sentiments in #130 (comment) as this would facilitate broadening supported stack configurations with which swagger-node-express could be deployed.
from swagger-node.
Thanks @hillct. Can you review #131?
from swagger-node.
Related Issues (20)
- Is there a way to hide an API in swagger UI ? Something like `hidden: true` in API definitions in swagger json file for nodejs application
- INVALID_TYPE on Date format field
- e.replace is not a function
- bug & Error: Response validation failed: failed schema validation HOT 2
- lodash version causing node vulnerability audit HOT 1
- swagger project edit cannot get / HOT 13
- I facing Issue in Swagger api ? HOT 1
- Error: Cannot find module swagger_router HOT 23
- Question: is `swagger-node-express` out of date? HOT 2
- Question: swagger-node-express has dependency vulnerability with outdated lodash version HOT 1
- Getting stack trace in swagger validation response
- Invalid request body validation for properties defined with capital letters
- Is this project deprecated? HOT 4
- Current version using old version of mocha that has a critical vulnerability HOT 1
- Uploading files mongodb using gridfs using swagger-tools, swagger-ui-express, multer-gridfs-storage, multer
- is the swagger-ui automatically included ?
- Is swagger-node deprecated? HOT 1
- help me to solve this issue
- 361 HOT 1
- Is there any possibility to upload auto generated swagger docs to the swagger.io
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swagger-node.