GithubHelp home page GithubHelp logo

Comments (16)

mcollina avatar mcollina commented on June 1, 2024 3

Oh, you'll need the exposeRoute: true option.

from fastify-swagger.

lgertel avatar lgertel commented on June 1, 2024 1

Great, working now!
One little thing: On the Usage section on README.md - there is a extra {}

fastify.register(require('fastify-swagger'), {
  swagger: {
    info: {
      title: 'Test swagger',
      description: 'testing the fastify swagger api',
      version: '0.1.0'
    },
    routePrefix: '/documentation',
    host: 'localhost',
    schemes: ['http'],
    consumes: ['application/json'],
    produces: ['application/json'],
    securityDefinitions: {
      apiKey: {
        type: 'apiKey',
        name: 'apiKey',
        in: 'header'
      }
    }
  }
})

This doesn't work!

Here is my version:

fastify.register(require('fastify-swagger'), {
    swagger: {
      info: {
        title: 'EDS Cognitive',
        description: 'Testing the fastify swagger api',
        version: '0.1.0'
      },
    },
    exposeRoute: true,
    routePrefix: '/documentation'
  })

Not sure why but....
Thanks a lot.
We will start to use fastify on a big enterprise project, you'll see me a lot here...lol

cya

from fastify-swagger.

mcollina avatar mcollina commented on June 1, 2024

I'm a bit lost. What do you mean? Can you make a full example?

from fastify-swagger.

lgertel avatar lgertel commented on June 1, 2024

I'm trying to add fastify-swagger on my project. The project was created with fastify-cli!
This is my app.js:

module.exports = function (fastify, opts, next) {

  fastify.register(require('@now-ims/fastify-firestore'), {
    projectId: 'nomikos-544df',
    keyFilename: './nomikos-78ff516da5d8.json'
  })

  fastify.register(require('fastify-swagger'), {
    swagger: {
      info: {
        title: 'Test swagger',
        description: 'testing the fastify swagger api',
        version: '0.1.0'
      },
      routePrefix: '/documentation',
      host: 'localhost',
      schemes: ['http'],
      consumes: ['application/json'],
      produces: ['application/json'],
      securityDefinitions: {
        apiKey: {
          type: 'apiKey',
          name: 'apiKey',
          in: 'header'
        }
      }
    }
  })

  // This loads all plugins defined in plugins
  // those should be support plugins that are reused
  // through your application
  fastify.register(AutoLoad, {
    dir: path.join(__dirname, 'plugins')
  })

  // This loads all plugins defined in services
  // define your routes in one of these
  fastify.register(AutoLoad, {
    dir: path.join(__dirname, 'services')
  })

  // Make sure to call next when done
  next()
}

In the documentation i saw that it's necessary to add the command below but I'm not sure where to put it, as the server is initialised by fastify start command.

fastify.swagger()

from fastify-swagger.

mcollina avatar mcollina commented on June 1, 2024

fastify.swagger() will just return the swagger definition. You do not have to do much, just navigate to /documentation and your swagger-ui should appear.

Is it working?

from fastify-swagger.

lgertel avatar lgertel commented on June 1, 2024

But where to add the fastify.swagger() ? On the route?

My root.js

'use strict'

module.exports = function (fastify, opts, next) {
  const options = {
    schema: {
      response: {
        200: {
          type: 'object',
          properties: {
            root: { type: 'boolean' }
          }
        }
      }
    }
  }

  fastify.get('/', options, function (request, reply) {
    reply.send({ root: true })
  })

  next()
}

// 20180611161700
// http://localhost:3000/documentation

{
  "statusCode": 404,
  "error": "Not Found",
  "message": "Not Found"
}

from fastify-swagger.

mcollina avatar mcollina commented on June 1, 2024

@lgertel would you like to send a PR to update the README?

I'm glad you picked fastify!

from fastify-swagger.

lgertel avatar lgertel commented on June 1, 2024

@mcollina yeah! sure!
I'll work on this and send news asap.

Who knows, maybe sometime I'll be able to work on the core too :)

from fastify-swagger.

radvansky-tomas avatar radvansky-tomas commented on June 1, 2024

Doesnt work for me, with current CLI create and having multiple folders in services folder. swagger docs is only shown for routes defined in app.js, root where are registrations

from fastify-swagger.

mcollina avatar mcollina commented on June 1, 2024

Can you please paste your app.js?

from fastify-swagger.

radvansky-tomas avatar radvansky-tomas commented on June 1, 2024
"use strict";
const PORT = process.env.PORT || 3000;
const path = require("path");
const AutoLoad = require("fastify-autoload");
const fastify = require("fastify")({
  logger: true
});
// Require external modules
const mongoose = require("mongoose");

// Connect to DB
mongoose
  .connect("mongodb://localhost:27017/navrush")
  .then(() => console.log("MongoDB connected…"))
  .catch(err => console.log(err));

// Place here your custom code!

// Do not touch the following lines

// This loads all plugins defined in plugins
// those should be support plugins that are reused
// through your application
fastify.register(AutoLoad, {
  dir: path.join(__dirname, "plugins")
});

// This loads all plugins defined in services
// define your routes in one of these
fastify.register(AutoLoad, {
  dir: path.join(__dirname, "services")
});

// Register Swagger
fastify.register(require('fastify-swagger'), {
  swagger: {
    info: {
      title: 'EDS Cognitive',
      description: 'Testing the fastify swagger api',
      version: '0.1.0'
    },
  },
  exposeRoute: true,
  routePrefix: '/documentation'
})

fastify.listen(PORT, err => {
  if (err) throw err;
  console.log(`server listening on ${fastify.server.address().port}`);
});

fastify.ready(err => {
  if (err) throw err;
  fastify.swagger();
});

from fastify-swagger.

radvansky-tomas avatar radvansky-tomas commented on June 1, 2024

screen shot 2019-01-22 at 11 30 32 am

from fastify-swagger.

radvansky-tomas avatar radvansky-tomas commented on June 1, 2024

If I add any schema definition to services/auth/index,js there are not part of swagger generated docs

from fastify-swagger.

mcollina avatar mcollina commented on June 1, 2024

that needs to be loaded before the autoload calls. There is a comment at the top where to add your plugins.

from fastify-swagger.

radvansky-tomas avatar radvansky-tomas commented on June 1, 2024

Thank you, works. Please try to add this to main example, as this is what you will get if you follow guides :D

from fastify-swagger.

Armania avatar Armania commented on June 1, 2024

This also worked for me,

I was trying to install @fastify/swagger-ui but was getting an error.
-> AssertionError [ERR_ASSERTION]: The dependency '@fastify/swagger' of plugin '@fastify/swagger-ui' is not registered

I tried just registering fastify-swagger, via "autoload" but adding a file for swagger. but that did not work. But i did not have the exposeRoute: true option.

/app.js
// ... fastify.register(require("@fastify/swagger"), { swagger: { info: { title: "Armani Machines", description: "Api documentation test using Swagger/Swaggerui", version: "0.1.0", }, }, exposeRoute: true, routePrefix: "/documentation", uiConfig: { docExpansion: "full", deepLinking: false, }, }); // ...

This code worked for me

from fastify-swagger.

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.