krakenjs / swaggerize-express

Design-driven apis with swagger 2.0 and express.
Other
355 stars 81 forks source link

Handler for root request not working #83

Closed fela98 closed 8 years ago

fela98 commented 8 years ago

I usually create a root request, that is some information about the API that is returned when a GET is made to '/' of the API. I can specify this in my swagger.json file. However adding a listener to it did not seem to work :/

I tried:

app.use(swaggerize({
    api: spec,
    handlers: {
    '/': {
        '$get': function(req, res) {
            res.send(infoObject);
        }
    }
}));

but with no luck. I also tried an empty string...

Is there any other way, than adding a handler manually?

tlivings commented 8 years ago

These handlers bind to the api specification.

Generally, the info is returned from the docspath property, which will return the swagger spec.

Is that what you are looking for?

fela98 commented 8 years ago

No, not exactly @tlivings I am not looking to get the swagger spec, or the documentation of the API. Forget what I said about getting information about the API...

What I am looking for is to simply be able to add a handler for the '/' (root) request. Until now I've just been using a simple express router to add a handler. However, I want to add the handler by using swaggerize express and add it as a property to the handlers object.

I want to do:

app.use(swaggerize({
    api: spec,
    handlers: {
    '/': {
        '$get': function(req, res) {
            res.send(something);
        }
    }
}));

But I currently have to do the following before initializing swaggerize express:

express.get('/', function(res, req, next) {
   res.send(something);
})

I hope that this clarifies what my problem is.

fela98 commented 8 years ago

Oops... I accidentally closed the issue...

tlivings commented 8 years ago

The swaggerize module is used to bind routes defined in the API spec to handlers.

You can't define a handler that doesn't have a corresponding API entry. Instead you must do it as you mentioned above.

fela98 commented 8 years ago

It does in fact have a route defined in the API spec. It also shows up in the Swagger UI. It should also be allowed according to the Swagger Spec 2.0 where it says that the pattern is "/{path}", where "{path}" in this case is an empty string, resulting in "/"

"paths": {
    "/": {
      "get": {
        "tags": [
          "about"
        ],
        "summary": "Get a status of the API",
        "description": "Gets status and other useful information about the API",
        "produces": [
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "Status acquired",
            "schema": {
              "type": "object"
            }
          }
        }
      }
    }
  }
tlivings commented 8 years ago

And what is the base path in your swagger doc?

fela98 commented 8 years ago

My "basePath" is not present. This means, according to the swagger spec, that the "host" value will be used. This is not specified either which means that the API will be served on the same host that serves the documentation, which also is the case.

I have also tried the following:

app.use(swaggerize({
  api: spec,
  handlers: {
    '$get': function(req, res) {
      res.send(something);
    }
  }
));

And another question, how should this be done if you use js files as handlers instead of an object? The only logical way I could think of sould be to use the index.js file for an eventual root request I've tried doing it like this, but with no success:

app.use(swaggerize({
  api: spec,
  handlers: './handlers'
));

Where ./handlers contains the following file named index.js

module.exports = {
  'get': function(req, res) {
    res.send(something);
  }
};

Everything I tried simply resulted in a Cannot GET / response from express.

tlivings commented 8 years ago

Have you tried following the swagger spec such that you've defined the path as /{path} and allow the param to be empty?

tlivings commented 8 years ago

I've added a PR to swaggerize-routes that fixes this.

tlivings commented 8 years ago

Landed in swaggerize-routes@1.0.6.

IbraheemAlSaady commented 6 years ago

I know this is an old dead thread, but I'm having the same problem, it might help some one out there.

After some digging with the source code, turns out you can just add a x-handler property to you YAML or JSON file like this x-handler: [handlers_directory]/[fileName.js]

Here is an example from my YAML file

/:
    x-handler: controllers/index.js # defines a specific handler for swaggerize routes
    get:
      x-handler: controllers/index.js
      description: Server thread uptime information and healthcheck URL.
      operationId: get
      ....reset of your YAML file

You can check that in the swaggerize routes test folder

I wish this can be added the docs so it will make it easier for users.

Cheers.