All Projects → ogen-go → ogen

ogen-go / ogen

Licence: Apache-2.0 license
OpenAPI v3 code generator for go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to ogen

openapi-generator-go
An opinionated OpenAPI v3 code generator for Go. Use this to generate API models and router scaffolding.
Stars: ✭ 42 (-90.37%)
Mutual labels:  code-generator, openapi, openapi3, openapi-generator
light-rest-4j
A RESTful framework built on top of light-4j with both Swagger 2.0 and OpenAPI 3.0 supports
Stars: ✭ 113 (-74.08%)
Mutual labels:  openapi, restful-api, openapi3, openapi-codegen
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 (+2338.99%)
Mutual labels:  openapi, restful-api, 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 (-91.74%)
Mutual labels:  code-generator, openapi, openapi3, openapi-generator
Unchase.OpenAPI.Connectedservice
📜 Visual Studio extension to generate OpenAPI (Swagger) web service reference.
Stars: ✭ 69 (-84.17%)
Mutual labels:  openapi, openapi3, openapi-codegen
openapi-petstore
The pet store sample
Stars: ✭ 35 (-91.97%)
Mutual labels:  openapi, openapi3, openapi-codegen
sbt-openapi-schema
Generate schema sources for Scala, Java and Elm from an openapi 3.0 spec.
Stars: ✭ 12 (-97.25%)
Mutual labels:  openapi, openapi3, openapi-codegen
fastify-openapi-glue
A plugin for Fastify to autogenerate a configuration based on a OpenApi(v2/v3) specification.
Stars: ✭ 94 (-78.44%)
Mutual labels:  openapi, openapi3, openapi-codegen
Swagger Node Codegen
An OpenAPI 3.x/Swagger 2 code generator for Node.js
Stars: ✭ 189 (-56.65%)
Mutual labels:  openapi, codegen, openapi3
intellij-openapi-generator
Intellij Plugin for openapi-generator
Stars: ✭ 73 (-83.26%)
Mutual labels:  openapi, openapi3, openapi-generator
Openapi Codegen
OpenAPI 3.0 CodeGen plus Node.js minus the Java and emojis
Stars: ✭ 224 (-48.62%)
Mutual labels:  openapi, codegen, openapi3
Swurg
Parse OpenAPI documents into Burp Suite for automating OpenAPI-based APIs security assessments (approved by PortSwigger for inclusion in their official BApp Store).
Stars: ✭ 94 (-78.44%)
Mutual labels:  openapi, restful-api, openapi3
openapi-eller
Generate OpenAPI v3 clients and servers from the command line
Stars: ✭ 19 (-95.64%)
Mutual labels:  openapi, openapi3, openapi-codegen
Api Generator
PHP-code generator for Laravel framework, with complete support of JSON-API data format
Stars: ✭ 244 (-44.04%)
Mutual labels:  code-generator, openapi
Kyaml2go
K8s Go client code generator from Kubernetes resource yamls
Stars: ✭ 226 (-48.17%)
Mutual labels:  code-generator, codegen
openapi-boilerplate
📘 Multi-file boilerplate for Open API Specification
Stars: ✭ 280 (-35.78%)
Mutual labels:  openapi, openapi3
oaie-sketch
OpenAPI Visual Editor
Stars: ✭ 54 (-87.61%)
Mutual labels:  openapi, openapi3
Xcassetpacker
A command line tool for converting a folder of images into an .xcasset package for Xcode
Stars: ✭ 150 (-65.6%)
Mutual labels:  code-generator, codegen
OpenAPI
A pharo implementation of OpenAPI 3.0.1
Stars: ✭ 20 (-95.41%)
Mutual labels:  openapi, openapi3
specifications-ITS-REST
openEHR REST API Specifications
Stars: ✭ 20 (-95.41%)
Mutual labels:  openapi, openapi3

ogen svg logo

ogen Go Reference codecov alpha

Opinionated OpenAPI v3 Code Generator for Go.

Work is still in progress, so currently no backward compatibility is provided. However, we are close to alpha.

Install

go get -d github.com/ogen-go/ogen

Usage

//go:generate go run github.com/ogen-go/ogen/cmd/ogen --target target/dir -package api --clean schema.json

Features

  • No reflection or interface{}
    • The json encoding is code-generated, optimized and uses go-faster/jx for speed and overcoming encoding/json limitations
    • Validation is code-generated according to spec
  • Code-generated static radix router
  • No more boilerplate
    • Structures are generated from OpenAPI v3 specification
    • Arguments, headers, url queries are parsed according to specification into structures
    • String formats like uuid, date, date-time, uri are represented by go types directly
  • Statically typed client and server
  • Convenient support for optional, nullable and optional nullable fields
    • No more pointers
    • Generated Optional[T], Nullable[T] or OptionalNullable[T] wrappers with helpers
    • Special case for array handling with nil semantics relevant to specification
      • When array is optional, nil denotes absence of value
      • When nullable, nil denotes that value is nil
      • When required, nil currently the same as [], but is actually invalid
      • If both nullable and required, wrapper will be generated (TODO)
  • Generated sum types for oneOf
    • Primitive types (string, number) are detected by type
    • Discriminator field is used if defined in schema
    • Type is inferred by unique fields if possible
  • OpenTelemetry tracing and metrics

Example generated structure from schema:

// Pet describes #/components/schemas/Pet.
type Pet struct {
	Birthday     time.Time     `json:"birthday"`
	Friends      []Pet         `json:"friends"`
	ID           int64         `json:"id"`
	IP           net.IP        `json:"ip"`
	IPV4         net.IP        `json:"ip_v4"`
	IPV6         net.IP        `json:"ip_v6"`
	Kind         PetKind       `json:"kind"`
	Name         string        `json:"name"`
	Next         OptData       `json:"next"`
	Nickname     NilString     `json:"nickname"`
	NullStr      OptNilString  `json:"nullStr"`
	Rate         time.Duration `json:"rate"`
	Tag          OptUUID       `json:"tag"`
	TestArray1   [][]string    `json:"testArray1"`
	TestDate     OptTime       `json:"testDate"`
	TestDateTime OptTime       `json:"testDateTime"`
	TestDuration OptDuration   `json:"testDuration"`
	TestFloat1   OptFloat64    `json:"testFloat1"`
	TestInteger1 OptInt        `json:"testInteger1"`
	TestTime     OptTime       `json:"testTime"`
	Type         OptPetType    `json:"type"`
	URI          url.URL       `json:"uri"`
	UniqueID     uuid.UUID     `json:"unique_id"`
}

Example generated server interface:

// Server handles operations described by OpenAPI v3 specification.
type Server interface {
	PetGetByName(ctx context.Context, params PetGetByNameParams) (Pet, error)
	// ...
}

Example generated client method signature:

type PetGetByNameParams struct {
    Name string
}

// GET /pet/{name}
func (c *Client) PetGetByName(ctx context.Context, params PetGetByNameParams) (res Pet, err error)

Generics

Instead of using pointers, ogen generates generic wrappers.

For example, OptNilString is string that is optional (no value) and can be null.

// OptNilString is optional nullable string.
type OptNilString struct {
	Value string
	Set   bool
	Null  bool
}

Multiple convenience helper methods and functions are generated, some of them:

func (OptNilString) Get() (v string, ok bool)
func (OptNilString) IsNull() bool
func (OptNilString) IsSet() bool

func NewOptNilString(v string) OptNilString

Recursive types

If ogen encounters recursive types that can't be expressed in go, pointers are used as fallback.

Sum types

For oneOf sum-types are generated. ID that is one of [string, integer] will be represented like that:

type ID struct {
	Type   IDType
	String string
	Int    int
}

// Also, some helpers:
func NewStringID(v string) ID
func NewIntID(v int) ID

JSON

Code generation provides very efficient and flexible encoding and decoding of json:

// Decode decodes Error from json.
func (s *Error) Decode(d *jx.Decoder) error {
	if s == nil {
		return errors.New("invalid: unable to decode Error to nil")
	}
	return d.ObjBytes(func(d *jx.Decoder, k []byte) error {
		switch string(k) {
		case "code":
			if err := func() error {
				v, err := d.Int64()
				s.Code = int64(v)
				if err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"code\"")
			}
		case "message":
			if err := func() error {
				v, err := d.Str()
				s.Message = string(v)
				if err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"message\"")
			}
		default:
			return d.Skip()
		}
		return nil
	})
}

Links

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