Skip to main content

ogen

OpenAPI v3 code generator for Go

go get -tool github.com/ogen-go/ogen/cmd/ogen@latest

No reflection

  • JSON encoding is code-generated and optimized, using go-faster/jx for speed and to overcome encoding/json limitations
  • Validation is code-generated directly from the specification
  • No interface or any on the hot path

No boilerplate

  • Structures are generated from the OpenAPI v3 specification
  • Path arguments, headers and query parameters are parsed into typed structures
  • String formats like uuid, date, date-time and uri map to Go types directly

Type-safe client & server

  • Statically typed client and server generated from a single spec
  • Implement one Handler interface and you are done
  • Code-generated static radix router for fast request dispatch

Optional & nullable

  • Optional and nullable fields without pointers when possible
  • Generated OptT, NilT and OptNilT wrappers with helpers
  • Array nil semantics follow the specification

Sum types

  • Sum types are generated for oneOf
  • Discriminator field is used when defined in the schema
  • Otherwise the variant is inferred by type, unique fields or enum values

Batteries included

  • OpenTelemetry tracing and metrics out of the box
  • Server-Sent Events (SSE) client generation
  • Middlewares, convenient errors and per-request options

From spec to Go, instantly

Describe your API once in OpenAPI v3 and let ogen generate a statically typed client and server.

petstore.yml
paths:
/pet/{petId}:
get:
operationId: getPetById
parameters:
- name: petId
in: path
required: true
schema:
type: integer
format: int64
responses:
'200':
description: Pet
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
generated Go
// Pet describes #/components/schemas/Pet.
type Pet struct {
ID int64 `json:"id"`
Name string `json:"name"`
Tag OptUUID `json:"tag"`
}

// Handler handles operations described
// by OpenAPI v3 specification.
type Handler interface {
// GET /pet/{petId}
GetPetById(ctx context.Context,
params GetPetByIdParams) (*Pet, error)
}