trailsjs / sails-swagger

Swagger integration for sails.js
107 stars 47 forks source link

Fix swagger paths generation to show DELETE doc #6

Closed AlexisNo closed 8 years ago

AlexisNo commented 9 years ago

Fix a little bug that could hide some swagger paths.

For example, the retrieved REST blueprints routes looked like this

{ '/todo/:id?': 
    [ Route {
        path: '/todo/:id?',
        method: 'delete',
        callbacks: [Object],
        keys: [Object],
        regexp: /^\/todo(?:\/([^\/]+?))?\/?$/i } ],
  '/todo/:id': 
    [ Route {
        path: '/todo/:id',
        method: 'get',
        callbacks: [Object],
        keys: [Object],
        regexp: /^\/todo\/(?:([^\/]+?))\/?$/i },
      Route {
        path: '/todo/:id',
        method: 'post',
        callbacks: [Object],
        keys: [Object],
        regexp: /^\/todo\/(?:([^\/]+?))\/?$/i },
      Route {
        path: '/todo/:id',
        method: 'put',
        callbacks: [Object],
        keys: [Object],
        regexp: /^\/todo\/(?:([^\/]+?))\/?$/i } ] } 

Using the following code, the first entry was overridden and the delete path did not appear in the swagger document.

mapKeys((_, path) => {
    return path.replace(/:(\w+)\??/g, '{$1}')
})

I replaced it by _.reduce() to merge the paths of '/todo/:id?' and '/todo/:id'.

To illustrate the current behavior, on http://swagger.balderdash.io/, the request DELETE /user/{id} is not documented.

I would have added some unit tests, but the current tests do not check the structure of the document so deeply yet. It should probably be done when improving the tests of the whole generated swagger document. Maybe I could work on it...

AlexisNo commented 9 years ago

@tjwebb any thoughts about this PR and #5 and #7?