All Projects → contiamo → openapi-generator-go

contiamo / openapi-generator-go

Licence: MIT license
An opinionated OpenAPI v3 code generator for Go. Use this to generate API models and router scaffolding.

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to openapi-generator-go

ogen
OpenAPI v3 code generator for go
Stars: ✭ 436 (+938.1%)
Mutual labels:  code-generator, openapi, openapi3, openapi-generator
php-json-schema-model-generator
Creates (immutable) PHP model classes from JSON-Schema files including all validation rules as PHP code
Stars: ✭ 36 (-14.29%)
Mutual labels:  code-generator, openapi, openapi3, openapi-generator
go-openapi
OpenAPI Specification (OAS) 3.0 implementation for Go
Stars: ✭ 38 (-9.52%)
Mutual labels:  openapi, openapi-spec, openapi3
openapi
OpenAPI 3 Specification for golang
Stars: ✭ 18 (-57.14%)
Mutual labels:  openapi, openapi-spec, openapi3
Openapi Generator
OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
Stars: ✭ 10,634 (+25219.05%)
Mutual labels:  openapi, openapi3, openapi-generator
intellij-openapi-generator
Intellij Plugin for openapi-generator
Stars: ✭ 73 (+73.81%)
Mutual labels:  openapi, openapi3, openapi-generator
oaie-sketch
OpenAPI Visual Editor
Stars: ✭ 54 (+28.57%)
Mutual labels:  openapi, openapi3
specifications-ITS-REST
openEHR REST API Specifications
Stars: ✭ 20 (-52.38%)
Mutual labels:  openapi, openapi3
aws2openapi
Amazon Web Services API description to OpenAPI 3.0 definition
Stars: ✭ 45 (+7.14%)
Mutual labels:  openapi, openapi3
openapi-filter
Filter internal paths, operations, parameters, schemas etc from OpenAPI/Swagger/AsyncAPI definitions
Stars: ✭ 112 (+166.67%)
Mutual labels:  openapi, openapi3
Openapi Cli Generator
Generate a CLI from an OpenAPI 3 specification
Stars: ✭ 121 (+188.1%)
Mutual labels:  code-generator, openapi
openapi-definitions
OpenAPI Definitions
Stars: ✭ 30 (-28.57%)
Mutual labels:  openapi, openapi-spec
swagger-converter
OpenAPI/Swagger 2.0 to OpenAPI 3.0 Converter WebService
Stars: ✭ 58 (+38.1%)
Mutual labels:  openapi, openapi3
OpenAPI
A pharo implementation of OpenAPI 3.0.1
Stars: ✭ 20 (-52.38%)
Mutual labels:  openapi, openapi3
openapi-boilerplate
📘 Multi-file boilerplate for Open API Specification
Stars: ✭ 280 (+566.67%)
Mutual labels:  openapi, openapi3
Api Generator
PHP-code generator for Laravel framework, with complete support of JSON-API data format
Stars: ✭ 244 (+480.95%)
Mutual labels:  code-generator, openapi
openapi-viewer
Browse and test a REST API described with the OpenAPI 3.0 Specification
Stars: ✭ 85 (+102.38%)
Mutual labels:  openapi, openapi3
sails-hook-swagger-generator
A tool to help generate Swagger specification documentation based on OAS 3.0 for Sails APIs
Stars: ✭ 71 (+69.05%)
Mutual labels:  openapi3, openapi-generator
oag
Idiomatic Go (Golang) client package generation from OpenAPI documents
Stars: ✭ 51 (+21.43%)
Mutual labels:  code-generator, openapi
openapi4j
OpenAPI 3 parser, JSON schema and request validator.
Stars: ✭ 92 (+119.05%)
Mutual labels:  openapi, openapi3

openapi-generator-go

test

Generate go code from an openapi spec!

# generate structs, enums and a router from a spec file
openapi-generator-go generate --spec ./example/api.yaml --output ./example/generated
# generate models and router independently
openapi-generator-go generate models --spec ./example/api.yaml --output ./example/models --package-name models
openapi-generator-go generate router --spec ./example/api.yaml --output ./example/router --package-name router

Why

The platform team at Contiamo has a schema first development flow. This means we write and review the OpenAPI spec before we write the implementation.

To make this easier we started using (or tried to use) various OpenAPI code generators to reduce busy work and ensure precise consistent implementations. Unfortunately, we never really enjoyed the output from these generators and have created our own generator that matches our preferred style and conventions.

What

The generator consists of two parts

  1. generator for the HTTP server mux using go-chi, and
  2. a model generator for the request and response objects.

The router generator

We chose go-chi because it is fast, supports the standard library Handler interface, and is easy to add middlewares using r.Use.

The generator will create a method NewRouter and handler interfaces that you must then implement and pass to NewRouter. It is important to note that it does not generate any handler logic, just the required interfaces. Handlers can be grouped into specific interfaces by adding x-handler-group: <Name> to your schema. This is one of the only schema extensions we have added to improve our code generation. The handler interface will be called NameHandler and will have a method matching the operationId from the spec.

For example, each of our projects will implement a project specific NewRouter that does the handler configuration and initialization of the generated router. In the next example, openapi.NewRouter is generated using

openapi-generator-go generate router --spec ./example/api.yaml --output ./example/router --package-name openapi

The project specific router initialization:

func NewRouter(ctx context.Context, db *sql.DB, cfg config.Config) (http.Handler, error) {
    // do setup stuff

    // and probably some more setup for each of the handler implementations
    userHandler := handlers.NewUserHandler(ctx, db)
    resourcesHandler := handlers.NewResource(ctx, db)

    // openapi-generator-go generate router --spec ./example/api.yaml --output ./example/router --package-name openapi
    apiRouter := openapi.NewRouter(
        // the UsersHandler interface contains a method for endpoint with
        // x-handler-group: Users
        userHandler,

        // ResourcesHandler interace contains a method for each endpoint with
        // x-handler-group: Resources
        resourcesHandler,
    )

    root.Route("/", func(api chi.Router) {
        // now  init and dd your middlewares
		r := middlewares.WithRecovery(os.Stderr, cfg.Debug)
		t := middlewares.WithTracing(config.ApplicationName, nil, middlewares.ChiRouteName)
		l := middlewares.WithLogging(config.ApplicationName)
		m := middlewares.WithMetrics(config.ApplicationName, nil)
		a := authorization.NewMiddleware(cfg.Authorization.HeaderName, publicKey)

		api.Use(r.WrapHandler)
		api.Use(t.WrapHandler)
		api.Use(l.WrapHandler)
		api.Use(m.WrapHandler)
		api.Use(a.WrapHandler)

        // mount the generate router
		api.Mount("/", apiRouter)
	})

	return root, nil

}

The model generator

The model generator is a work in progress, but covers the most common cases we need in a REST API.

You can find various examples of the what is supported and the corresponding output in our test fixtures.

Our generator differs from the official OpenAPI generator tools by also providing getters for many fields, which makes it easier to define and work with interfaces of the models.

We also provide better support for :

  • enums, enum support also includes some utility methods that make validating the enums much easier,
  • arrays,
  • allOf,
  • and oneOf that feel more natural in Go. Creating as many strong types as is possible and using interface{} otherwise. These cases often failed or generated non-compilable code with the official generator.

Future work

[x] Adding validators for each model using the ozzo-validation Validatable interface [ ] Support for oneOf discriminators

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].