All Projects → seriousme → fastify-openapi-glue

seriousme / fastify-openapi-glue

Licence: MIT License
A plugin for Fastify to autogenerate a configuration based on a OpenApi(v2/v3) specification.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to fastify-openapi-glue

light-rest-4j
A RESTful framework built on top of light-4j with both Swagger 2.0 and OpenAPI 3.0 supports
Stars: ✭ 113 (+20.21%)
Mutual labels:  openapi, openapi3, openapi-codegen
Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+3568.09%)
Mutual labels:  swagger, openapi, openapi3
Openapi Diff
Utility for comparing two OpenAPI specifications.
Stars: ✭ 208 (+121.28%)
Mutual labels:  swagger, openapi, openapi3
Openapi Directory
🌐 Wikipedia for Web APIs. Directory of REST API definitions in OpenAPI 2.0/3.x format
Stars: ✭ 2,635 (+2703.19%)
Mutual labels:  swagger, openapi, openapi3
sbt-openapi-schema
Generate schema sources for Scala, Java and Elm from an openapi 3.0 spec.
Stars: ✭ 12 (-87.23%)
Mutual labels:  openapi, openapi3, openapi-codegen
Redoc
📘 OpenAPI/Swagger-generated API Reference Documentation
Stars: ✭ 15,935 (+16852.13%)
Mutual labels:  swagger, openapi, openapi3
Spot
Spot is a concise, developer-friendly way to describe your API contract.
Stars: ✭ 230 (+144.68%)
Mutual labels:  swagger, openapi, openapi3
Openapi Spec Validator
OpenAPI Spec validator
Stars: ✭ 161 (+71.28%)
Mutual labels:  swagger, openapi, openapi3
shipengine-openapi
The official OpenAPI 3.0 definitions for ShipEngine™
Stars: ✭ 13 (-86.17%)
Mutual labels:  swagger, openapi, openapi3
openapi-eller
Generate OpenAPI v3 clients and servers from the command line
Stars: ✭ 19 (-79.79%)
Mutual labels:  openapi, openapi3, openapi-codegen
Swaggerprovider
F# generative Type Provider for Swagger
Stars: ✭ 201 (+113.83%)
Mutual labels:  swagger, openapi, openapi3
Unchase.OpenAPI.Connectedservice
📜 Visual Studio extension to generate OpenAPI (Swagger) web service reference.
Stars: ✭ 69 (-26.6%)
Mutual labels:  openapi, openapi3, openapi-codegen
Openapi Mock
OpenAPI mock server with random data generation
Stars: ✭ 202 (+114.89%)
Mutual labels:  swagger, openapi, openapi3
openapi-schema-validator
OpenAPI schema validator for Python
Stars: ✭ 35 (-62.77%)
Mutual labels:  swagger, openapi, openapi3
Swagger Node Codegen
An OpenAPI 3.x/Swagger 2 code generator for Node.js
Stars: ✭ 189 (+101.06%)
Mutual labels:  swagger, openapi, openapi3
Openapi Codegen
OpenAPI 3.0 CodeGen plus Node.js minus the Java and emojis
Stars: ✭ 224 (+138.3%)
Mutual labels:  swagger, openapi, openapi3
Prance
Resolving Swagger/OpenAPI 2.0 and 3.0 Parser
Stars: ✭ 133 (+41.49%)
Mutual labels:  swagger, openapi, openapi3
Prism
Turn any OpenAPI2/3 and Postman Collection file into an API server with mocking, transformations and validations.
Stars: ✭ 2,484 (+2542.55%)
Mutual labels:  swagger, openapi, openapi3
Full Stack Fastapi Couchbase
Full stack, modern web application generator. Using FastAPI, Couchbase as database, Docker, automatic HTTPS and more.
Stars: ✭ 243 (+158.51%)
Mutual labels:  swagger, openapi, openapi3
ogen
OpenAPI v3 code generator for go
Stars: ✭ 436 (+363.83%)
Mutual labels:  openapi, openapi3, openapi-codegen

Fastify OpenApi Glue

CI status Coverage Status Language grade: JavaScript NPM version npm

A plugin for fastify to autogenerate a configuration based on a OpenApi(v2/v3) specification.

It aims at facilitating "design first" API development i.e. you write or obtain an API specification and use that to generate code. Given an OpenApi specification Fastify-openapi-glue handles the fastify configuration of routes and schemas etc. You can also generate your own project from a OpenApi specification.

Install

npm i fastify-openapi-glue --save

Plugin

Usage

Add the plugin to your project with register and pass it some basic options and you are done !

import openapiGlue from "fastify-openapi-glue";
import Service from "./service.js";
import Security from "./security.js";

const options = {
  specification: `${currentDir}/petstore-openapi.v3.json`,
  service: new Service(),
  securityHandlers: new Security(),
  prefix: "v1",
  noAdditional: true,
  ajvOptions: {
    formats: {
      "custom-format": /\d{2}-\d{4}/
    }
  }
};


fastify.register(openapiGlue, options);

All schema and routes will be taken from the OpenApi specification listed in the options. No need to specify them in your code.

Options

  • specification: this can be a JSON object, or the name of a JSON or YAML file containing a valid OpenApi(v2/v3) file
  • service: this can be a javascript object or class, or the name of a javascript file containing such an object. If the import of the file results in a function instead of an object then the function will be executed during import.
  • securityHandlers: this can be a javascript object or class, or the name of a javascript file containing such an object. If the import of the file results in a function instead of an object then the function will be executed during import. See the securityHandlers documentation for more details.
  • prefix: this is a string that can be used to prefix the routes, it is passed verbatim to fastify. E.g. if the path to your operation is specified as "/operation" then a prefix of "v1" will make it available at "/v1/operation". This setting overrules any "basePath" setting in a v2 specification. See the servers documentation for more details on using prefix with a v3 specification.
  • noAdditional: by default Fastify will silently ignore additional properties in a message. Setting noAdditional to true will change this behaviour and will make Fastify return a HTTP error 400 when additional properties are present. Default value for this option is false.
  • ajvOptions: Pass additional options to AJV (see https://ajv.js.org/options.html)

specification and service are mandatory, securityHandlers, prefix and noAdditional are optional.

See the examples section for a demo.

Please be aware that this will refer to your service object or your securityHandler object and not to Fastify as explained in the bindings documentation

Generator

To make life even more easy there is the openapi-glue cli. The openapi-glue cli takes a valid OpenApi (v2/v3) file (JSON or YAML) and generates a project including a fastify plugin that you can use on any fastify server, a stub of the service class and a skeleton of a test harness to test the plugin.

Usage

  openapi-glue [options] <OpenApi specification>

or if you don't have openapi-glue installed:

  npx github:seriousme/fastify-openapi-glue <OpenApi specification>

This will generate a project based on the provided OpenApi specification. Any existing files in the project folder will be overwritten! See the generator examples section for a demo.

Options


  -p <name>                   The name of the project to generate
  --projectName=<name>        [default: generatedProject]

  -b <dir> --baseDir=<dir>    Directory to generate the project in.
                              This directory must already exist.
                              [default: "."]

The following options are only usefull for testing the openapi-glue plugin:
  -c --checksumOnly           Don't generate the project on disk but
                              return checksums only.
  -l --localPlugin            Use a local path to the plugin.

See the generator example section for a demo.

Examples

Clone this repository and run npm i

Plugin

Executing npm start will start fastify on localhost port 3000 with the routes extracted from the petstore example and the accompanying service definition

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "params.petId should be integer"
}
{
  "statusCode": 500,
  "error": "Internal Server Error",
  "message": "Operation findPetsByStatus not implemented"
}
{
  "statusCode": 500,
  "error": "Internal Server Error",
  "message":"\"name\" is required!"
}

as the pet returned by service.js does not match the response schema.

Generator

The folder examples/generatedProject contains the result of running openapi-glue -l --baseDir=examples examples/petstore/petstore-swagger.v2.yaml. The generated code can be started using npm start in examples/generatedProject (you will need to run npm i in the generated folder first)

Notes

  • the plugin ignores information in a v3 specification under server/url as there could be multiple values here, use the prefix option if you need to prefix your routes. See the servers documentation for more details.
  • fastify only supports application/json and text/plain out of the box. The default charset is utf-8. If you need to support different content types, you can use the fastify addContentTypeParser API.
  • fastify only supports one schema per route. So while the v3 standard allows for multiple content types per route, each with their own schema this is currently not going to work with fastify. Potential workarounds include a custom content type parser and merging schemas upfront using JSON schema oneOf.
  • the plugin aims to follow fastify and does not compensate for features that are possible according to the OpenApi specification but not possible in standard fastify (without plugins). This will keep the plugin lightweigth and maintainable.
  • if you have special needs on querystring handling (e.g. arrays, objects etc) then fastify supports a custom querystring parser. You might need to pass the AJV option coerceTypes: 'array' as an option.

Contributing

  • contributions are always welcome.
  • if you plan on submitting new features then please create an issue first to discuss and avoid disappointments.
  • main development is done on the master branch therefore PRs to that branch are preferred.
  • please make sure you have run npm test before you submit a PR.

Fastify-swaggergen

Fastify-openapi-glue is the successor to the now deprecated fastify-swaggergen project. Main difference is that it:

  • aims to support OpenApi and not just Swagger V2 (hence the name change)
  • does not include fastify-swagger support anymore. If you need to show the swagger UI you can include it yourself. Removing the swagger UI clears up a number of dependencies.

License

Licensed under MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].