JLavoie
JLavoie

Reputation: 17596

Swagger-hapi: path /models generate errors

The following NodeJS Hapi code generate an error when querying at http://localhost:3000/documentation

If I change the path of the endpoint to something else than /models, like /users for instance, everything works well. It looks like the endpoint /models is reserved.

Any idea why any other endpoint work except /models? How can I fix it? I can't change the URL as too many people use it.

var Hapi            = require('hapi'),
    Inert           = require('inert'),
    Vision          = require('vision'),
    Joi             = require('joi'),
    HapiSwagger     = require('hapi-swagger')


var server = new Hapi.Server();
server.connection({
    host: 'localhost',
    port: 3000
});

var swaggerOptions = {
    apiVersion: "1.0"
};

server.register([
    Inert,
    Vision,
    {
        register: HapiSwagger,
        options: swaggerOptions
    }], function (err) {
    server.start(function(){
        // Add any server.route() config here
        console.log('Server running at:', server.info.uri);
    });
});

server.route(
    {
        method: 'GET',
        path: '/models',
        config: {
            handler: function (request, reply) {
                reply("list of models")
            },
            description: 'Get todo',
            notes: 'Returns a todo item by the id passed in the path',
            tags: ['api'],
            validate: {
                params: {
                    username: Joi.number()
                        .required()
                        .description('the id for the todo item')
                }
            }
        }
    }
)



server.start(function(){
    // Add any server.route() config here
    console.log('Server running at:', server.info.uri);
});

Upvotes: 0

Views: 303

Answers (1)

Glenn Jones
Glenn Jones

Reputation: 36

Yes models is part of swagger's internal structure and it looks like there is an issue in the swagger.js file when dealing with endpoints that use models as part of the URL for an endpoint.

The easy fix for this is to use a nickname. This changes the internal ref in swagger, but the UI should still say models and it will fire against your endpoint correctly.

{
    method: 'GET',
    path: '/models/{username}',
    config: {
        handler: function (request, reply) {
            reply("list of models")
        },
        description: 'Get todo',
        notes: 'Returns a todo item by the id passed in the path',
        tags: ['api'],
        plugins: {
            'hapi-swagger': {
              nickname: 'modelsapi'
            }
          },
        validate: {
            params: {
                username: Joi.number()
                    .required()
                    .description('the id for the todo item')
            }
        }
    }
}

Upvotes: 2

Related Questions