GithubHelp home page GithubHelp logo

fastify / fastify-swagger Goto Github PK

View Code? Open in Web Editor NEW
858.0 858.0 197.0 4.67 MB

Swagger documentation generator for Fastify

License: MIT License

JavaScript 95.31% TypeScript 4.69%
documentation fastify fastify-plugin swagger

fastify-swagger's Introduction

CI Package Manager CI Web SIte js-standard-style CII Best Practices

NPM version NPM downloads Security Responsible Disclosure Discord Contribute with Gitpod Open Collective backers and sponsors


An efficient server implies a lower cost of the infrastructure, a better responsiveness under load and happy users. How can you efficiently handle the resources of your server, knowing that you are serving the highest number of requests as possible, without sacrificing security validations and handy development?

Enter Fastify. Fastify is a web framework highly focused on providing the best developer experience with the least overhead and a powerful plugin architecture. It is inspired by Hapi and Express and as far as we know, it is one of the fastest web frameworks in town.

The main branch refers to the Fastify v4 release. Check out the v3.x branch for v3.

Table of Contents

Quick start

Create a folder and make it your current working directory:

mkdir my-app
cd my-app

Generate a fastify project with npm init:

npm init fastify

Install dependencies:

npm i

To start the app in dev mode:

npm run dev

For production mode:

npm start

Under the hood npm init downloads and runs Fastify Create, which in turn uses the generate functionality of Fastify CLI.

Install

To install Fastify in an existing project as a dependency:

Install with npm:

npm i fastify

Install with yarn:

yarn add fastify

Example

// Require the framework and instantiate it

// ESM
import Fastify from 'fastify'

const fastify = Fastify({
  logger: true
})
// CommonJs
const fastify = require('fastify')({
  logger: true
})

// Declare a route
fastify.get('/', (request, reply) => {
  reply.send({ hello: 'world' })
})

// Run the server!
fastify.listen({ port: 3000 }, (err, address) => {
  if (err) throw err
  // Server is now listening on ${address}
})

with async-await:

// ESM
import Fastify from 'fastify'

const fastify = Fastify({
  logger: true
})
// CommonJs
const fastify = require('fastify')({
  logger: true
})

fastify.get('/', async (request, reply) => {
  reply.type('application/json').code(200)
  return { hello: 'world' }
})

fastify.listen({ port: 3000 }, (err, address) => {
  if (err) throw err
  // Server is now listening on ${address}
})

Do you want to know more? Head to the Getting Started.

Note

.listen binds to the local host, localhost, interface by default (127.0.0.1 or ::1, depending on the operating system configuration). If you are running Fastify in a container (Docker, GCP, etc.), you may need to bind to 0.0.0.0. Be careful when deciding to listen on all interfaces; it comes with inherent security risks. See the documentation for more information.

Core features

  • Highly performant: as far as we know, Fastify is one of the fastest web frameworks in town, depending on the code complexity we can serve up to 76+ thousand requests per second.
  • Extensible: Fastify is fully extensible via its hooks, plugins and decorators.
  • Schema based: even if it is not mandatory we recommend to use JSON Schema to validate your routes and serialize your outputs, internally Fastify compiles the schema in a highly performant function.
  • Logging: logs are extremely important but are costly; we chose the best logger to almost remove this cost, Pino!
  • Developer friendly: the framework is built to be very expressive and help the developer in their daily use, without sacrificing performance and security.

Benchmarks

Machine: EX41S-SSD, Intel Core i7, 4Ghz, 64GB RAM, 4C/8T, SSD.

Method:: autocannon -c 100 -d 40 -p 10 localhost:3000 * 2, taking the second average

Framework Version Router? Requests/sec
Express 4.17.3 14,200
hapi 20.2.1 42,284
Restify 8.6.1 50,363
Koa 2.13.0 54,272
Fastify 4.0.0 77,193
-
http.Server 16.14.2 74,513

Benchmarks taken using https://github.com/fastify/benchmarks. This is a synthetic, "hello world" benchmark that aims to evaluate the framework overhead. The overhead that each framework has on your application depends on your application, you should always benchmark if performance matters to you.

Documentation

中文文档地址

Ecosystem

  • Core - Core plugins maintained by the Fastify team.
  • Community - Community supported plugins.
  • Live Examples - Multirepo with a broad set of real working examples.
  • Discord - Join our discord server and chat with the maintainers.

Support

Please visit Fastify help to view prior support issues and to ask new support questions.

Contributing

Whether reporting bugs, discussing improvements and new ideas or writing code, we welcome contributions from anyone and everyone. Please read the CONTRIBUTING guidelines before submitting pull requests.

Team

Fastify is the result of the work of a great community. Team members are listed in alphabetical order.

Lead Maintainers:

Fastify Core team

Fastify Plugins team

Great Contributors

Great contributors on a specific area in the Fastify ecosystem will be invited to join this group by Lead Maintainers.

Past Collaborators

Hosted by

We are a At-Large Project in the OpenJS Foundation.

Sponsors

Support this project by becoming a SPONSOR! Fastify has an Open Collective page where we accept and manage financial contributions.

Acknowledgements

This project is kindly sponsored by:

Past Sponsors:

This list includes all companies that support one or more of the team members in the maintenance of this project.

License

Licensed under MIT.

For your convenience, here is a list of all the licenses of our production dependencies:

  • MIT
  • ISC
  • BSD-3-Clause
  • BSD-2-Clause

fastify-swagger's People

Contributors

allevo avatar briceruzand avatar cemremengu avatar climba03003 avatar darkgl0w avatar delvedor avatar dependabot-preview[bot] avatar dependabot[bot] avatar eomm avatar fdawgs avatar greenkeeper[bot] avatar joaopedrocampos avatar kromol avatar leandroandrade avatar liam-tait avatar m-ronchi avatar mcollina avatar michieldemey avatar mxck avatar pavelpolyakov avatar pgaubatz avatar rafaismyname avatar samzilverberg avatar scriptjs avatar skellla avatar thomasthiebaud avatar uzlopak avatar woh avatar zekth avatar zenflow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fastify-swagger's Issues

Params with separator breaking docs

I'm trying to do a route like the one specified in the Fastify docs with two params between a single set of slashes using the dash separator:

fastify.get('/example/near/:lat-:lng/radius/:r', (request, reply) => {}))

My specific route is pretty close to this: '/transform_point/:lat-:lng/:srid'. This end point works fine. The Swagger docs, however, seem to break on the separator. The path ends up looking like this:

path

The schema populates the test form correctly with the route inputs, but after hitting execute it doesn't populate the lng and lat route parameters, seemingly treating that portion of the route as a static string.

curl

My schema parameters look like this:

params: {
    lat: {
      type: 'number',
      description: 'x coordinate'
    },
    lng: {
      type: 'number',
      description: 'y coordinate'
    },
    srid: {
      type: 'integer',
      description: 'SRID of the coordinate'
    }
  },

Am I doing something wrong, or are two params between a set of slashes unsupported? Am I better off making it a single parameter and regex-ing a [number][separator][number] validation?

Many thanks!

An in-range update of fastify is breaking the build 🚨

☝️ Greenkeeper’s updated Terms of Service will come into effect on April 6th, 2018.

Version 1.2.1 of fastify was just published.

Branch Build failing 🚨
Dependency fastify
Current Version 1.2.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

fastify is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push The Travis CI build failed Details

Release Notes v1.2.1

Fixes

  • Run onSend hooks when an encapsulated handler invokes the notFound handler - #870
  • Updated default 404 message - #870

Docs

  • Fix typo in getting started documentation #869
  • Fix broken links - #866
Commits

The new version differs by 4 commits.

  • 2d8ac88 Bumped v1.2.1
  • cc2f9c9 Run onSend hooks when an encapsulated handler invokes the notFound handler (#870)
  • b010287 Fix typo in getting started documentation (#869)
  • 8491872 [docs] Fix broken links (#866)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Returned specs does not contain definitions

Hi,

I would like to convert the spec I get from const spec = fastify.swagger() into a typescript d.ts.

I tryied many libraries, but none could do the trick. Then I realised that there was no definitions property in the outputed spec. I can see the paths, but no definition.

Is there any setting to output it?

Declare an empty response for 204

For 204 response (empty response), Swagger allows for its description by not specifying a content https://swagger.io/docs/specification/describing-responses/.

However, for fastify-swagger, I haven't found a way to do that yet. If I do:

{
  schema: {
    response: {
      204: {
        description: 'Successfully deleted item'
      }
    }
  }
}

I get the following stacktrace:

Uncaught Exception Error: undefined unsupported
    at build (/Users/tri/project/node_modules/fast-json-stringify/index.js:80:13)
    at getValidatorForStatusCodeSchema (/Users/tri/project/node_modules/fastify/lib/validation.js:12:10)
    at /Users/tri/project/node_modules/fastify/lib/validation.js:18:21
    at Array.reduce (<anonymous>)
    at getResponseSchema (/Users/tri/project/node_modules/fastify/lib/validation.js:17:22)
    at build (/Users/tri/project/node_modules/fastify/lib/validation.js:44:28)

'Error: plugin must be a function' while using Jest coverage reporting

I've been stuck debugging this issue all day. I think I've finally figure out the source of the issue and it looks like it has something to do with Jest coverage generator, fastify-swagger, and avvio.

On this PR: MatterhornDev/matterhorn#25
A user has added fastify-swagger to a basic fastify server.

I'm using Jest for testing. npm run test works as expected. Everything is green.

Running npm run test:ci or npm run coverage (which both execute jest --coverage in some fashion) everything fails and blows up.

I found this issue: swagger-api/swagger-node#560 (comment) which mentions that the swagger doc generator doesn't exactly play nicely with Jest. I added "collectCoverageFrom": [ "src" ] to my jest.config.json in order to force the coverage reporter to only look at my actual source code. This resolved a really nasty (barely useful) error message and has now left me with the following:

👻 matterhorn (panbhatt-fastify_swagger_implementation %)▷ npm run test -- --coverage --coverageReporters=lcov

> [email protected] test /Users/ethanarrowood/Documents/GitHub/matterhorn
> jest --config jest.config.json "--coverage" "--coverageReporters=lcov"

/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/boot.js:178
    throw new Error('plugin must be a function')
    ^

Error: plugin must be a function
    at Boot.Object.<anonymous>.Boot.use (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/boot.js:178:11)
    at Object.server.(anonymous function) [as register] (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio
/boot.js:30:14)
    at Object.<anonymous>.module.exports [as dynamic] (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastify-swagger/dynamic.js:35:13)
    at fastifySwagger (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastify-swagger/index.js:18:13)
    at Plugin.Object.<anonymous>.Plugin.exec (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:84:17)
    at Boot.loadPlugin (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:168:10)
    at Task.release (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastq/queue.js:127:16)
    at worked (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastq/queue.js:169:10)
    at toLoad.finish (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:171:7)
    at done (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:129:5)
    at check (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:140:7)
    at processTicksAndRejections (internal/process/next_tick.js:74:9)
 PASS  tests/index.test.ts
/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/boot.js:178
    throw new Error('plugin must be a function')
    ^

Error: plugin must be a function
    at Boot.Object.<anonymous>.Boot.use (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/boot.js:178:11)
    at Object.server.(anonymous function) [as register] (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio
/boot.js:30:14)
    at Object.<anonymous>.module.exports [as dynamic] (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastify-swagger/dynamic.js:35:13)
    at fastifySwagger (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastify-swagger/index.js:18:13)
    at Plugin.Object.<anonymous>.Plugin.exec (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:84:17)
    at Boot.loadPlugin (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:168:10)
    at Task.release (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastq/queue.js:127:16)
    at worked (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastq/queue.js:169:10)
    at toLoad.finish (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:171:7)
    at done (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:129:5)
    at check (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:140:7)
    at processTicksAndRejections (internal/process/next_tick.js:74:9)
/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/boot.js:178
    throw new Error('plugin must be a function')
    ^

Error: plugin must be a function
    at Boot.Object.<anonymous>.Boot.use (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/boot.js:178:11)
    at Object.server.(anonymous function) [as register] (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/boot.js:30:14)
    at Object.<anonymous>.module.exports [as dynamic] (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastify-swagger/dynamic.js:35:13)
    at fastifySwagger (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastify-swagger/index.js:18:13)
    at Plugin.Object.<anonymous>.Plugin.exec (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:84:17)
    at Boot.loadPlugin (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:168:10)
    at Task.release (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastq/queue.js:127:16)
    at worked (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastq/queue.js:169:10)
    at toLoad.finish (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:171:7)
    at done (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:129:5)
    at check (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:140:7)
    at processTicksAndRejections (internal/process/next_tick.js:74:9)
/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/boot.js:178
    throw new Error('plugin must be a function')
    ^

Error: plugin must be a function
    at Boot.Object.<anonymous>.Boot.use (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/boot.js:178:11)
    at Object.server.(anonymous function) [as register] (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/boot.js:30:14)
    at Object.<anonymous>.module.exports [as dynamic] (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastify-swagger/dynamic.js:35:13)
    at fastifySwagger (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastify-swagger/index.js:18:13)
    at Plugin.Object.<anonymous>.Plugin.exec (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:84:17)
    at Boot.loadPlugin (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:168:10)
    at Task.release (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastq/queue.js:127:16)
    at worked (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/fastq/queue.js:169:10)
    at toLoad.finish (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:171:7)
    at done (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:129:5)
    at check (/Users/ethanarrowood/Documents/GitHub/matterhorn/node_modules/avvio/plugin.js:140:7)
    at processTicksAndRejections (internal/process/next_tick.js:74:9)
 FAIL  tests/plugins.test.ts
  ● Test suite failed to run

    Call retries were exceeded

      at ChildProcessWorker.initialize (node_modules/jest-worker/build/workers/ChildProcessWorker.js:177:21)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        7.605s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `jest --config jest.config.json "--coverage" "--coverageReporters=lcov"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/ethanarrowood/.npm/_logs/2019-03-12T01_47_07_774Z-debug.log

I'm now going to try and dig into these error messages, but I thought I'd post this issue in case someone could spot the mistake or propose a solution faster than I can.

FWIW, coverage works fine on the master branch (where there is no fastify-swagger installed).

Some other areas I have debunked:

  • It is not caused by the directory structure or import structure of my src/ contents
  • As far as I can tell everything has been updated (and works as expected on master)

Unable to generate the /documentation Swagger URI

Hi Team,
I am testing this plugin with Fastify 1.0.0-rc-2 and even after lot of attempts unable to get the swagger UI up. The JSON representation shows that the default URL /documentation is not a part of swagger configuration that is being returned.

{"swagger":"2.0","info":{"title":"Test swagger","description":"testing the fastify swagger api","version":"0.1.0"},"host":"localhost","schemes":["http"],"consumes":["application/json"],"produces":["application/json"],"paths":{"/":{"get":{"responses":{"200":{"description":"Default Response"}}}},"/error":{"get":{"responses":{"200":{"description":"Default Response"}}}},"/some-route/{id}":{"put":{"summary":"qwerty","description":"post some data","tags":["user","code"],"parameters":[{"name":"body","in":"body","schema":{"type":"object","properties":{"hello":{"type":"string"},"obj":{"type":"object","properties":{"some":{"type":"string"}}}}}},{"name":"id","in":"path","required":true,"description":"user id","type":"string"}],"responses":{"201":{"schema":{"description":"Successful response","type":"object","properties":{"hello":{"type":"string"}}},"description":"Successful response"}}}},"/ram":{"get":{"responses":{"200":{"description":"Default Response"}}}}}}

This is what I am getting. All the source code is available at https://github.com/panbhatt/Fastify-api-boilerplate ( in master branch ). Please HELP.

Support openapi 3.0 for dynamic configuration

Seems underlying module swagger-parser already supports new openapi v3 specifications. So I think it would be good if this module will support it as well.

The main idea just to make https://github.com/fastify/fastify-swagger/blob/master/dynamic.js#L59 configurable through options.

However it will also require to refactor some things like securityDefinitions, because in 3.0 they moved to other sections of config file.

I'm not sure that fastify schemas are compatible with openapi v3 specs.

Remove dynamic requirements

I use webpack on the server side. ES 6 expects static imports and emits the following warnings. In general, I believe it is bad practice to include dynamic requirements in a module - even while a dynamic import is being considered in ES.

I have replaced my project path with '/path/to/project' in the traces below:

WARNING in /path/to/project/index.js
47:15-60 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/fastify-swagger/index.js
 @ ./es6/lib/app/index.js
 @ ./es6/index.js

I'll send a PR for this.

Deprecated route

Hi!, i have a deprecated route in schema and defined like deprecated: true but not showing in the ui the route with the style like line-through.

This is an issue or im not defining the schema correctly?

Thx!

Add a transform method for schema

Hello I would like to suggest an extra option to add a transform method in the plugin options for dynamic mode.
Use case : using Joi validation library we need to convert Joi schema back into a JSON schema only for Swagger documentation generation. With a transform method it would be easy to convert it with a third-party library.

file upload schema error 400

Hi, when i define the schema consumer in endpoint /upload to 'multipart/form-data' for uploading file i dont know wich is the schema structure body for passing the base64 string or the file that i need to upload.

If y define the schema:

schema: {
      description: 'Upload file',
      tags: ['Info'],
      consumes: [ 'multipart/form-data' ],
      summary: 'Upload the file to the server',
      body: {
        type: 'object',
        properties: {
          file: {type: 'string'}
        }
      },
      response: {
        200: {
          description: 'Succesful response',
          type: 'boolean'
        }
      }
    }

when i send the post get the error:

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "body should be object"
}

I fetch in the dynamic.js code looking for the solution but i dont get with it.

Can you help me?

PD: Im sorry if this is not the place for this kind of issues.

Thx

An in-range update of tap is breaking the build 🚨

The devDependency tap was updated from 12.1.1 to 12.1.2.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

tap is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 7 commits.

  • cf8a2e1 12.1.2
  • 04d27df Add attributes file
  • 3620c76 Remove some tests of things that can't be done in v11+
  • f61181a Run snapshots in -J mode, remove an extraneous test
  • 699b179 (synonym) added 'identical' to same to create symmetry to 'equivalent' synonym on equal
  • fe466f5 test: don't break when running in buffered mode
  • f1979a9 typo in usage.txt

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Multiple docs in project

Hi,
I need to make different docs for different routes in my application. I have 2 "root" routes, for example:

fastify.register(require('./routes/one'), {
  prefix: '/one'
})

fastify.register(require('./routes/two'), {
  prefix: '/two'
})

And want two documentations for subroutes of /one and /two.
Is there any way to make it now? Or maybe add something like subroutesOf: '/one' parameter to plugin options to filter routes needed for each documentation?
Thanks for your time.

Add support for different response content types

Swagger supports different content types in the response like this:

/health:
  get:
    responses:
      '200':
        description: Successful response
        content:
          text/plain:
            schema:
              type: string
              enum: ['UP','DOWN']

Currently it is not possible to define a response like this with fastify-swagger.

Support `tags` and `externalDocs` in dynamic mode

As per OpenAPI 2.0 specification, you are missing 2 top-level fields described here https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#fixed-fields:

  • tags
  • externalDocs

Because you are reconstructing swaggerObject from opts.swagger in dynamic.js even if those fields are given, they are stripped in the output.

I'm filing this issue because I found that tags was missing, but there may be more fields that are lacking deeper down, I didn't check.

Swagger expose "/documentation/*" endpoint

Hi, i have an issue that i dont know if to others happend but in swagger is loading the /documentation/* endpoint, i resolve if i ask in the fastify-swagger plugin by the condition in dynamic:

if (route.path === 'documentation/*' || route.schema && route.schema.hide)

but i dont know if is the correct way because maybe someone just resolvered.

This is the json that swagger generates with only one endpoint:

{
   "swagger":"2.0",
   "info":{
      "version":"1.0.0",
      "title":"API Documentation",
      "description":"API Endpoints Documentation"
   },
   "schemes":[
      "https",
      "http"
   ],
   "consumes":[
      "application/json"
   ],
   "produces":[
      "application/json"
   ],
   "paths":{
      "/documentation/*":{
         "get":{
            "responses":{
               "200":{
                  "description":"Default Response"
               }
            }
         }
      },
      "/v1/keep-alive":{
         "get":{
            "summary":"Obtain the status of the API",
            "description":"Get the status of the API",
            "tags":[
               "Info"
            ],
            "responses":{
               "200":{
                  "schema":{
                     "description":"Succesful response",
                     "type":"string"
                  },
                  "description":"Succesful response"
               },
               "400":{
                  "schema":{
                     "description":"Not Found response",
                     "type":"string"
                  },
                  "description":"Not Found response"
               },
               "500":{
                  "schema":{
                     "description":"Error response",
                     "type":"object",
                     "properties":{
                        "error":{
                           "type":"string"
                        },
                        "message":{
                           "type":"string"
                        },
                        "statusCode":{
                           "type":"number"
                        }
                     }
                  },
                  "description":"Error response"
               }
            }
         }
      }
   }
}

and this is my config:

{
   "swagger":{
      "info":{
         "version":"1.0.0",
         "title":"API Documentation",
         "description":"API Endpoints Documentation"
      },
      "schemes":[
         "http"
      ],
      "consumes":[
         "application/json"
      ],
      "produces":[
         "application/json"
      ]
   },
   "exposeRoute":true
}

External $ref does not work

Description
It doesn't work if $ref is pointing to the definition in another file. External references are supported by swagger

This code does not work:
$ref: './resources.yaml#/resources/User'

Expected result
It should load definition from external files

Actual result
It fails when it tries to load the file (GET /documentation/path-to-file). Server responds with 404 since it's not aware about any other files except of main swagger file that is passed to the plugin.

Corrupt file attachments

We encountered an issue where a file download triggered via the Content-Disposition header will result in a link to download the file, but the file will be corrupt. A similiar issue is also documented in following swagger-ui issue:

swagger-api/swagger-ui#4638

In our case we want to download a tar-file. So the content-type of the response is application/gzip. We want to add the download-endpoint to the swagger-documentation but if we do that the downloaded file is corrupt.

In the swagger-api/swagger-ui#4638 Issue the solution is to upgrade to Swagger UI 3. Is there a workaround in fastify-swagger?

Declare 204 with type null

When I declare the scheme as below

response: {
        204: {
          description: 'Succesful response',
        },
      },

I get the following stacktrace:

Error: undefined unsupported
    at build (/Users/guilherme.gregio/Documents/easynvest/easynvest.preferencies/node_modules/fast-json-stringify/index.js:94:13)
    at getValidatorForStatusCodeSchema (/Users/guilherme.gregio/Documents/easynvest/easynvest.preferencies/node_modules/fastify/lib/validation.js:13:10)
    at /Users/guilherme.gregio/Documents/easynvest/easynvest.preferencies/node_modules/fastify/lib/validation.js:19:21
    at Array.reduce (<anonymous>)
    at getResponseSchema (/Users/guilherme.gregio/Documents/easynvest/easynvest.preferencies/node_modules/fastify/lib/validation.js:18:22)
    at build (/Users/guilherme.gregio/Documents/easynvest/easynvest.preferencies/node_modules/fastify/lib/validation.js:58:31)
    at afterRouteAdded (/Users/guilherme.gregio/Documents/easynvest/easynvest.preferencies/node_modules/fastify/fastify.js:607:9)
    at Object._encapsulateThreeParam (/Users/guilherme.gregio/Documents/easynvest/easynvest.preferencies/node_modules/avvio/boot.js:387:7)
    at Boot.callWithCbOrNextTick (/Users/guilherme.gregio/Documents/easynvest/easynvest.preferencies/node_modules/avvio/boot.js:311:5)
    at Boot._after (/Users/guilherme.gregio/Documents/easynvest/easynvest.preferencies/node_modules/avvio/boot.js:215:26)

When I try use type: 'null'

response: {
        204: {
          type: 'null',
          description: 'Succesful response',
        },
      },

But the result is "Unknown Type: null" in model's section on swagger doc

fastify is not iterable

Versions

name version
node 8.9.0
fastify 0.40.0
fastify-swagger 0.3.1

Error

TypeError: fastify is not iterable
    at Function.swagger (/codes/node_modules/fastify-swagger/index.js:70:22)
    at fastify.ready (/codes/server.js:44:11)
    at Function._encapsulateThreeParam (/codes/node_modules/avvio/boot.js:332:7)
    at Boot.callWithCbOrNextTick (/codes/node_modules/avvio/boot.js:287:5)
    at release (/codes/node_modules/fastq/queue.js:127:16)
    at Object.resume (/codes/node_modules/fastq/queue.js:61:7)
    at Boot.Plugin.loadPlugin.call (/codes/node_modules/avvio/boot.js:141:20)
    at toLoad.finish (/codes/node_modules/avvio/plugin.js:108:7)
    at done (/codes/node_modules/avvio/plugin.js:66:5)
    at check (/codes/node_modules/avvio/plugin.js:77:7)

Script

const fastify = require('fastify')({
  logger: {
    level: 'info',
  },
});
const fastifySwagger = require('fastify-swagger');

const routes = require('./app/controller');

fastify.register(fastifySwagger, {
  swagger: {
    info: {
      title: 'insurance-agent-favor/backend',
      version: '0.1.0',
    },
    host: 'localhost',
    schemes: ['http'],
    consumes: ['application/json'],
    produces: ['application/json'],
  },
  exposeRoute: true,
});
fastify.register(routes);

const start = async () => {
  try {
    await fastify.listen(3000);
    fastify.swagger();
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};
start();

Else

It is ok with [email protected]

HTTP/2 Support

Fastify currently has experimental HTTP/2 support.

Is it possible to use fastify-swagger with http/2? But since I'm using Typescript, my project won't compile because the fastify-swagger types require a HTTP Server, instead of HTTP2

Rename repository to fastify-openapi

Swagger 2 was made OpenAPI 2 quite some time ago. Swagger is the brand name of SmartBear Software. In the spirit of the move to the open standard, the previous Swagger 2 specification became the OpenAPI 2 specification. Version 3 of the OpenAPI specification is still undergoing review.

I am recommending the repository name be changed to fastify-openapi to reflect the direction of the standard and to stop using SmartBear's proprietary branding.

This will require a few small changes since the higher level object is referred to as swagger at the moment.

Problems with `ignoreTrailingSlash` fastify option

Hello, just taking the dynamic example and changing the fastify construction:

const fastify = require('fastify')({
  ignoreTrailingSlash: true
})

makes the url sample documentation url http://localhost:3000/documentation redirect to http://localhost:3000/documentation/documentation

Seems related to the routes redirect. But I am really not sure where the real cause and true good fix is... in this plugin, fastify or find-my-way... I can't really find where the original redirect happens in fastify or find-my-way that seems to be cause by ignoreTrailingSlash option.

I would think ideally if we could know the options of fastify instance we could just ignore the setup of redirect route. But I can't seem to find a nice way of knowing if that option is turned on as it or the original options do not seem to be exposed? I would be willing to do a PR with some guidance on the best approach where the fix is.

I also checked this with code in #56 and it still exhibits this issue.

image

Thanks.

Support passing swagger definition in static mode

Currently, in static mode, there is no way to pass a content of the file directly. You need to pass the path to the file instead.

Current behavior

mode: 'static',
specification: {
    path: './examples/example-static-specification.yaml'
},

New feature request

mode: 'static',
specification: {
    doc: {
        ...real swagger document... (content of the specification file)
    }
},

Why?

We use fastify-swagger to provide a fastify integration with NestJS (and Swagger module exposed by @nestjs/swagger). Unluckily, we cannot fully support this platform since there is no way to pass swagger definition's object directly (you always have to pass a path to the file in static mode). Would it be a big deal to provide such possibility in the future?

Version 10 of node.js has been released

Version 10 of Node.js (code name Dubnium) has been released! 🎊

To see what happens to your code in Node.js 10, Greenkeeper has created a branch with the following changes:

  • Added the new Node.js version to your .travis.yml

If you’re interested in upgrading this repo to Node.js 10, you can open a PR with these changes. Please note that this issue is just intended as a friendly reminder and the PR as a possible starting point for getting your code running on Node.js 10.

More information on this issue

Greenkeeper has checked the engines key in any package.json file, the .nvmrc file, and the .travis.yml file, if present.

  • engines was only updated if it defined a single version, not a range.
  • .nvmrc was updated to Node.js 10
  • .travis.yml was only changed if there was a root-level node_js that didn’t already include Node.js 10, such as node or lts/*. In this case, the new version was appended to the list. We didn’t touch job or matrix configurations because these tend to be quite specific and complex, and it’s difficult to infer what the intentions were.

For many simpler .travis.yml configurations, this PR should suffice as-is, but depending on what you’re doing it may require additional work or may not be applicable at all. We’re also aware that you may have good reasons to not update to Node.js 10, which is why this was sent as an issue and not a pull request. Feel free to delete it without comment, I’m a humble robot and won’t feel rejected 🤖


FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of swagger-parser is breaking the build 🚨

Version 5.0.4 of swagger-parser was just published.

Branch Build failing 🚨
Dependency swagger-parser
Current Version 5.0.2
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

swagger-parser is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 8 commits.

  • 5870803 release v5.0.4
  • 9331095 updated dependencies
  • 33a665c fixed a line of code that didn't work on some older web browsers (IE11)
  • e58c457 Merge pull request #92 from marcelstoer/feat/dup-operationId
  • 4e6ed4b Add check for duplicate operation ids
  • f8113cf release v5.0.3
  • 91640e9 updated dependencies
  • 8c62e51 Modified the sample URL to use HTTPS instead of HTTP

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Wrong redirect

v0.10.1 - There stil seems to be a bug when using routePrefix: '/' even after this fix (#65) from @allevo. It redirects the browser to index.html instead of http://domain/index.html

Authorize UI does appear to work when configured

I don't seem to be able to configure authorization properly to gain the authorize UI button in the docs as in the petstore reference implementation you see in the image below:

screen shot 2018-01-04 at 10 51 25 pm

I have attempted by setting headers ie.

  headers: {
    type: 'object',
    properties: {
      'x-api-token': {
        type: 'string',
        description: 'optional api token'
      },
      'x-api-version': {
        type: 'string',
        description: 'optional api version'
      }
    }
  },

and

  headers: {
    type: 'object',
    properties: {
      authorization: {
        type: 'string',
        description: 'api token'
      }
    },
    required: ['authorization']
  },

from what I was able to read from the tests.

I have also tried adding securityDefinitions within the swagger object. These are normally attached to a security object for scope ie.

"security": [{
  "api_key": []
}]

Undefined description in path parameters will cause yaml dump crash

As the title described, if I didn't add description field in parameters in schema when I access /documentation/yaml will cause crash, and the expection is:

YAMLException: unacceptable kind of an object to dump [object Undefined]

Specifically in line 146 in index.js

const swaggerString = yaml.safeDump(swaggerObject)

Since the yaml feature is bundled with documentation routes feature no matter if user need it or not, so this will lead to potential risk.

Actually js-yaml has an option for ignoring invalid value when dumps, see safedump docs
Simply change to this:

const swaggerString = yaml.safeDump(swaggerObject, { skipInvalid: true })

This may hide the exception, but at least it will not crash.

How to add Dynamic Querystring Parameters ?

Hello
I'm trying to achieve the same goal than this : https://stackoverflow.com/questions/49582559/how-to-document-dynamic-query-parameter-names-in-openapi-swagger
basically trying to mix on the doc some known querystrings params with not known that can be used by the api behind dynamically :

GET api/v1/users?knownParam1=value1&knownParam2=value2...&dynamicParam1=...&foo=bar

I tryied the object type with the additionalProperties flag :

  querystring: {
    knownParam1: { type: 'string', description: 'String - bla bla ' },
    knownParam2:{ type: 'string', description: 'String - bla bla ' },
    params : {
      'type': 'object',
      'properties': {
        unknownParam1: { type: 'string' },
        unknownParam2: { type: 'string' }
      },
      'additionalProperties': true,
    },

But the end result still have a "params" parameter rather than listing the examples

I try really to mix known api query params and unknowparams, the free form object seems to fit my need for the unknowns but I don't know how to make that key dynamic to let the user generate the additional ones exactly as in the SOF thread
image

If Someone has any idea,
thanks

Cannot find module 'fastify-swagger'

fastify-swagger module not published the latest version

module.js:538
    throw err;
    ^

Error: Cannot find module 'fastify-swagger'
    at Function.Module._resolveFilename (module.js:536:15)
    at Function.Module._load (module.js:466:25)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)

Project:
screen shot 2017-11-10 at 2 06 03 pm

As above screenshot is shown, I can only see one file package.json after installing fastify-swagger.

Is this project well maintained?

fastify.ready() with fastify-cli

Hello everyone.
I'm using fastify-cli to run my project, and don't know where to add the configuration:

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

Thanks!

Plugin not working with JOI

Here is schema example for route:

import * as Joi from 'joi';

export const PING_CREATE_VALIDATOR = {
    schema: {
        body: Joi.object({
            example: Joi.string().required(),
        }).required(),
    },
    schemaCompiler: (schema: any) => (data: any) => Joi.validate(data, schema),
};

Error:

TypeError: Cannot read property 'description' of null
    at Object.keys.forEach.p (node_modules/fastify-swagger/index.js:217:34)
    at Array.forEach (native)
    at getPathParams (node_modules/fastify-swagger/index.js:212:23)
    at Object.swagger (node_modules/fastify-swagger/index.js:121:11)
    at fastify.ready (src/app.ts:49:20)
    at Object._encapsulateThreeParam (node_modules/avvio/boot.js:345:7)
    at Boot.callWithCbOrNextTick (node_modules/avvio/boot.js:300:5)
    at Object.push (node_modules/fastq/queue.js:88:14)
    at Boot.ready (node_modules/avvio/boot.js:255:18)
    at Object.server.(anonymous function) [as ready] (node_modules/avvio/boot.js:46:21)

Not support JSON schema items properties object

{
  type: "object",
  required: ["message", "errors"]
  properties: {
    message: {
      type: "string"
    },
    errors: {
      type: "array",
      items: [
        {
          type: "object",
          properties: {
            field: {
              type: "string"
            },
            error: {
              type: "string"
            },
            type: {
              type: "string"
            }
          }
        }
      ]
    }
  }
}

image

[Suggestion] Change swagger url

Hi,

Are you guys open for a PR that will customize the url so let's say I want to change the url from documentation to docs?

Let me know what you guys think.

Cheers.

generate fastify code from swagger definition ?

Hi,

Since fastify already uses JSON-schema I thought it might be an idea to try to generate fastify code from a swagger doc so one can use a Design-First approach as well.
Either using swagger codegen, a fastify specific tool or fully dynamically by using JSON path queries on a swagger definition (using a fastify plugin ?)

Has anybody already tried this ?

Swagger codegen currently only generates Express code for NodeJS

Kind regards,
Hans

Fastify-Swagger introduction to a TYPESCRIPT proj doen't allow swagger details

While importing fastify-swagger into a fastify (ts) based project, when we are defining the schema of any method, we are restricted because of the RouteSchema defined in the fastify.d.ts.
For Swagger we need to expand this to include other properties like documenation,tags,summary,security.
Creating a pull reques to acheive this.

handling of ajv pattern properties

Hello!
i think this ticket is not a real issue , but might be a missing feature from ajv/swagger.
According to ajv spec we can set dynamic key in properties with the patternProperties.
In fastify it works like charms for validation but not for the swagger gen part.
here is a example :
schema :

 "response": {
      "200": {
       " description":
          "if uplink need acknowledgement (ack=true), set the reponse the configuration for the device (downlink)",
        "type": "object",
        "patternProperties": {
          "^([0-9]|[A-Z])*$": {
            "type": "object",
            "properties": {
              "downlinkData": {
                "type": "string",
                "description": "the encoded hexadecimal configuration"
              }
            }
          }
        },
        "additionalProperties": false
      },

the swagger output:

{
  "responses": {
    "200": {
      "schema": {
        "description": "if uplink need acknowledgement (ack=true), set the reponse the configuration for the device (downlink)",
        "type": "object",
        "patternProperties": {
          "^([0-9]|[A-Z])*$": {
            "type": "object",
            "properties": {
              "downlinkData": {
                "type": "string",
                "description": "the encoded hexadecimal configuration"
              }
            }
          }
        },
        "additionalProperties": false
      },
      "description": "if uplink need acknowledgement (ack=true), set the reponse the configuration for the device (downlink)"
    }
  }
}

So we clearly see that the swagger schema holds a patternProperties wich is not descriped in the swagger spec v3. https://swagger.io/docs/specification/data-models/dictionaries/

thanks for your time.
Olivier

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.