Skip to content

Commit 2653285

Browse files
committed
Improve root page, examples, and config docs
- Rework homepage hero with install command and CTAs - Expand feature grid to 6 cards matching ogen capabilities - Add spec-to-Go showcase section - Add Examples page (real-world specs, optional/nullable, oneOf, SSE, security) - Expand config docs with feature/parser/filter recipes
1 parent ca32fbc commit 2653285

5 files changed

Lines changed: 505 additions & 35 deletions

File tree

docs/06-examples.mdx

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
---
2+
id: examples
3+
title: Examples
4+
sidebar_label: Examples
5+
sidebar_position: 6
6+
---
7+
8+
# Examples
9+
10+
This page collects small, focused examples. For a complete runnable project,
11+
see the [Getting started](intro) guide and the
12+
[ogen-go/example](https://github.com/ogen-go/example) repository.
13+
14+
## Real-world specifications
15+
16+
ogen is tested against a large set of real-world OpenAPI documents. Each of
17+
these is generated in CI from the
18+
[`examples`](https://github.com/ogen-go/ogen/tree/main/examples) directory:
19+
20+
| API | Spec |
21+
| --- | --- |
22+
| GitHub | `api.github.com.json` |
23+
| Kubernetes | `k8s.json` |
24+
| OpenAI | `openai.openapi.yaml` |
25+
| Telegram Bot API | `telegram_bot_api.json` |
26+
| Firecracker | `firecracker.json` |
27+
| Petstore (expanded) | `petstore-expanded.yml` |
28+
29+
Generation is driven by `go:generate` directives. A typical one looks like:
30+
31+
```go title="generate.go"
32+
package api
33+
34+
//go:generate go tool ogen --clean --config .ogen.yml --target api github.json
35+
```
36+
37+
Larger specs like GitHub and Kubernetes rely on type inference and skipping
38+
not-yet-supported operations:
39+
40+
```yaml title=".ogen.yml"
41+
parser:
42+
infer_types: true
43+
generator:
44+
ignore_not_implemented: ["all"]
45+
```
46+
47+
See [Config](config) for the full list of options.
48+
49+
## Optional and nullable fields
50+
51+
ogen avoids pointers for optional and nullable values. Given this schema:
52+
53+
```yaml
54+
components:
55+
schemas:
56+
Pet:
57+
type: object
58+
required: [id, name]
59+
properties:
60+
id:
61+
type: integer
62+
format: int64
63+
name:
64+
type: string
65+
nickname:
66+
type: string
67+
nullable: true
68+
tag:
69+
type: string
70+
format: uuid
71+
```
72+
73+
ogen generates wrappers instead of pointers:
74+
75+
```go
76+
type Pet struct {
77+
ID int64 `json:"id"`
78+
Name string `json:"name"`
79+
Nickname OptNilString `json:"nickname"` // optional + nullable
80+
Tag OptUUID `json:"tag"` // optional uuid.UUID
81+
}
82+
```
83+
84+
Each wrapper comes with helpers:
85+
86+
```go
87+
pet := Pet{ID: 1, Name: "Cat"}
88+
pet.Tag = NewOptUUID(uuid.New())
89+
90+
if v, ok := pet.Tag.Get(); ok {
91+
fmt.Println("tag:", v)
92+
}
93+
94+
pet.Nickname = NewOptNilString("Kitty")
95+
pet.Nickname.SetToNull() // encodes as JSON null
96+
```
97+
98+
## Sum types from `oneOf`
99+
100+
A `oneOf` with a discriminator becomes a Go sum type:
101+
102+
```yaml
103+
components:
104+
schemas:
105+
Animal:
106+
oneOf:
107+
- $ref: "#/components/schemas/Cat"
108+
- $ref: "#/components/schemas/Dog"
109+
discriminator:
110+
propertyName: kind
111+
mapping:
112+
cat: "#/components/schemas/Cat"
113+
dog: "#/components/schemas/Dog"
114+
```
115+
116+
The generated type carries the active variant and helper constructors:
117+
118+
```go
119+
type Animal struct {
120+
Type AnimalType // "Cat" or "Dog"
121+
Cat Cat
122+
Dog Dog
123+
}
124+
125+
func NewCatAnimal(v Cat) Animal
126+
func NewDogAnimal(v Dog) Animal
127+
```
128+
129+
Switch on the variant when handling a response:
130+
131+
```go
132+
switch a.Type {
133+
case CatAnimal:
134+
fmt.Println("meow:", a.Cat.Name)
135+
case DogAnimal:
136+
fmt.Println("woof:", a.Dog.Name)
137+
}
138+
```
139+
140+
ogen can also infer the discriminator automatically when none is defined — by
141+
type, by unique field names, or by enum values. See the
142+
[sum types reference](types/sumtype.md) for details.
143+
144+
## Server-Sent Events
145+
146+
For `text/event-stream` responses, ogen generates an SSE client:
147+
148+
```yaml
149+
paths:
150+
/events:
151+
get:
152+
operationId: streamEvents
153+
responses:
154+
'200':
155+
description: Event stream
156+
content:
157+
text/event-stream:
158+
schema:
159+
type: object
160+
properties:
161+
message:
162+
type: string
163+
createdAt:
164+
type: string
165+
format: date-time
166+
```
167+
168+
The generated client exposes an iterator over events and handles reconnection
169+
automatically:
170+
171+
```go
172+
stream, err := client.StreamEvents(ctx)
173+
if err != nil {
174+
return err
175+
}
176+
defer stream.Close()
177+
178+
for ev, err := range stream.All(ctx) {
179+
if err != nil {
180+
return err
181+
}
182+
fmt.Println(ev.Data.Message)
183+
}
184+
```
185+
186+
## Securing requests
187+
188+
Security schemes become a `SecuritySource` interface you implement once. For an
189+
API secured with a bearer token:
190+
191+
```yaml
192+
components:
193+
securitySchemes:
194+
bearerAuth:
195+
type: http
196+
scheme: bearer
197+
security:
198+
- bearerAuth: []
199+
```
200+
201+
```go
202+
type securitySource struct {
203+
token string
204+
}
205+
206+
func (s securitySource) BearerAuth(ctx context.Context, operationName api.OperationName) (api.BearerAuth, error) {
207+
return api.BearerAuth{Token: s.token}, nil
208+
}
209+
210+
client, err := api.NewClient(server, securitySource{token: os.Getenv("TOKEN")})
211+
```
212+
213+
The token is then attached to every request that requires it.

docs/07-config.mdx

Lines changed: 147 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,163 @@ sidebar_label: Config
55
sidebar_position: 7
66
---
77

8-
ogen could be configurated via `.ogen.yml` file:
8+
# Configuration
99

10-
```yaml [title=".ogen.yml"]
11-
# sets generator options.
10+
ogen can be configured via a YAML file passed with the `--config` flag:
11+
12+
```bash
13+
go tool ogen --config .ogen.yml --target petstore --clean petstore.yml
14+
```
15+
16+
Adding the schema annotation at the top of the file enables autocompletion and
17+
validation in editors that support the YAML language server:
18+
19+
```yaml title=".ogen.yml"
20+
# yaml-language-server: $schema=https://raw.githubusercontent.com/ogen-go/ogen/main/schemas/ogen.jsonschema.json
21+
```
22+
23+
The configuration has two main sections: `parser` controls how the OpenAPI
24+
document is read, and `generator` controls what code is produced.
25+
26+
## Features
27+
28+
ogen provides an opt-in/opt-out mechanism for features such as OpenTelemetry
29+
integration, client and server generation, and request validation.
30+
31+
```yaml title=".ogen.yml"
1232
generator:
13-
# sets generator features.
1433
features:
1534
enable:
16-
# Enables paths client generation
35+
# Generate client code for API paths.
1736
- "paths/client"
18-
# Enables paths server generation
37+
# Generate server code for API paths.
1938
- "paths/server"
20-
# Enables validation of client requests
21-
- "client/request/validation"
22-
# Enables validation of server responses
23-
- "server/response/validation"
24-
# Enables OpenTelemetry integration
39+
# Enable OpenTelemetry integration.
2540
- "ogen/otel"
41+
disable:
42+
# Disable server response validation.
43+
- "server/response/validation"
44+
```
45+
46+
Features are resolved in three steps: start from the default set (unless
47+
`disable_all` is `true`), apply `disable`, then apply `enable`. Entries in
48+
`enable` always take priority.
49+
50+
The default feature set is:
51+
52+
```yaml
53+
- paths/client
54+
- paths/server
55+
- webhooks/client
56+
- webhooks/server
57+
- ogen/otel
58+
- ogen/unimplemented
59+
```
60+
61+
You can find the [complete feature list](https://github.com/ogen-go/ogen/blob/main/gen/features.go)
62+
in the ogen repository.
63+
64+
### Generate only a client
65+
66+
To generate a client without any server code, disable everything and enable
67+
just the client features:
68+
69+
```yaml title=".ogen.yml"
70+
generator:
71+
features:
72+
enable:
73+
- client/editors
74+
- client/request/options
75+
- ogen/otel
76+
- ogen/unimplemented
77+
- paths/client
78+
- webhooks/client
2679
disable_all: true
2780
```
2881

29-
See full config example [here](https://github.com/ogen-go/ogen/blob/main/examples/config/example_all.yml).
82+
### Disable OpenTelemetry
83+
84+
OpenTelemetry is enabled by default. Disable it to drop the dependency and the
85+
instrumentation code:
86+
87+
```yaml title=".ogen.yml"
88+
generator:
89+
features:
90+
disable:
91+
- "ogen/otel"
92+
```
93+
94+
## Skipping unsupported operations
95+
96+
Some real-world specifications contain constructs that ogen cannot yet
97+
generate. By default, generation fails on the first such error. Use
98+
`ignore_not_implemented` to skip those operations instead:
99+
100+
```yaml title=".ogen.yml"
101+
generator:
102+
# Skip all not-implemented generation errors.
103+
ignore_not_implemented: ["all"]
104+
```
105+
106+
You can also skip only specific cases, which keeps the rest of the
107+
specification strict:
108+
109+
```yaml title=".ogen.yml"
110+
generator:
111+
ignore_not_implemented:
112+
- "nested objects in form parameters"
113+
```
114+
115+
## Filters
116+
117+
Generate code for a subset of the specification by path or HTTP method:
118+
119+
```yaml title=".ogen.yml"
120+
generator:
121+
filters:
122+
# Only paths matching this regular expression are generated.
123+
path_regex: "^/api/v1/.*"
124+
# Only these methods are generated.
125+
methods:
126+
- GET
127+
- POST
128+
```
129+
130+
## Type inference
30131

31-
### Features
132+
When a schema omits an explicit `type`, ogen can infer it from the schema's
133+
properties. This is required for many large real-world specs such as the
134+
GitHub or Kubernetes APIs:
32135

33-
ogen provides a opt-in/opt-out mechanism for some features like OpenTelemetry integration.
136+
```yaml title=".ogen.yml"
137+
parser:
138+
infer_types: true
139+
```
140+
141+
## Full example
142+
143+
A configuration combining the most common options, similar to what is used to
144+
generate the GitHub API client:
145+
146+
```yaml title=".ogen.yml"
147+
# yaml-language-server: $schema=https://raw.githubusercontent.com/ogen-go/ogen/main/schemas/ogen.jsonschema.json
148+
149+
parser:
150+
# Detect schema type from its properties.
151+
infer_types: true
152+
153+
generator:
154+
features:
155+
enable:
156+
- "paths/client"
157+
- "paths/server"
158+
- "ogen/otel"
159+
disable_all: true
160+
# Skip operations that are not yet supported.
161+
ignore_not_implemented: ["all"]
162+
```
34163

35-
You can find a [complete feature list](https://github.com/ogen-go/ogen/blob/main/gen/features.go) in ogen repository.
164+
See the [full reference config](https://github.com/ogen-go/ogen/blob/main/examples/config/example_all.yml),
165+
which documents every available option with example values, and the
166+
[default config](https://github.com/ogen-go/ogen/blob/main/examples/config/default_all.yml),
167+
which shows all defaults.

0 commit comments

Comments
 (0)