Skip to content

Latest commit

 

History

History
59 lines (42 loc) · 2.32 KB

README.md

File metadata and controls

59 lines (42 loc) · 2.32 KB

Fastify swagger code generator

This repository contains an experiment to see if its possible to take a swagger file (v2) and autogenerate a configuration for fastify.

The result is quite simple:

  • index.js loads the swagger file, feeds it to the parser and uses the result to generate the routes.

  • AJV itself misses some format handlers so ajv-oai is being used as it adds the missing OpenApi formats.

  • For each route the parser has determined an operationId, either taken from the swagger definition or generated by the parser.

  • The Service class in service.js holds the implementation of the service (e.g. the business logic). When generating the routes the service class is checked for methods with the same name as the operationId which are then attached as handler. When not available, a "not implemented" handler is attached to the route.

  • generator.js can be used to generate the Service class. The generator can be extended to add information about parameters, information in comments etc.

  • fastify-swagger has been included to do a round trip. The swagger UI shows on /documentation

Examples

Clone this repository and run npm i Executing node index.js will then start fastify on localhost port 3000 with the routes extracted from the petstore example

{
  "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.