Skip to content

Commit 2820b7c

Browse files
dotkasCopilot
andauthored
fix: adjust documentation (#31)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 251cedf commit 2820b7c

6 files changed

Lines changed: 227 additions & 304 deletions

File tree

README.md

Lines changed: 28 additions & 304 deletions
Large diffs are not rendered by default.

docs/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Documentation
2+
3+
Deep-dive docs for `oapi-codegen-rust`. See the [root README](../README.md) for
4+
a quickstart.
5+
6+
- [Installation](installation.md)
7+
- [Configuration](configuration.md) — CLI and config-file reference
8+
- [OpenAPI extensions](extensions.md) — supported vendor extension keys
9+
- [Design decisions vs. Go](design.md) — where and why we deviate from `oapi-codegen`

docs/configuration.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Configuration
2+
3+
## CLI
4+
5+
```sh
6+
oapi-codegen [--config <cfg.yaml>] [--output <out.rs>] <spec.yaml>
7+
```
8+
9+
- `<spec>` — path to the OpenAPI 3 document (YAML or JSON). Required.
10+
- `--config, -c` — path to a YAML config file. Without one, only models are
11+
generated.
12+
- `--output, -o` — output file (overrides `output` in the config). Prints to
13+
stdout if unset.
14+
15+
## Config file
16+
17+
Keys mirror [`oapi-codegen`](https://github.com/oapi-codegen/oapi-codegen)'s
18+
YAML config; unknown keys are ignored, so an existing Go config can be reused
19+
as-is. Only the subset below is interpreted.
20+
21+
| Key | Type | Purpose |
22+
| ---------------- | ------ | ---------------------------------------------------------------------- |
23+
| `package` | string | Target module name (informational). |
24+
| `output` | path | Output file path. |
25+
| `import-mapping` | map | Map referenced spec files to external Rust modules (multi-file specs). |
26+
27+
### `generate`
28+
29+
| Key | Purpose |
30+
| ----------------- | ------------------------------------------------------ |
31+
| `models` | Emit structs/enums from component schemas. |
32+
| `std-http-server` | Emit an axum server interface (`trait Api` + router). |
33+
| `client` | Emit a blocking `reqwest` client. |
34+
| `server-urls` | Emit constants/builders for the spec's `servers` URLs. |
35+
| `embedded-spec` | _Not implemented_ — rejected if set. |
36+
37+
Setting both `std-http-server` and `client` emits them into `server` / `client`
38+
submodules with shared models at the root.
39+
40+
### `output-options`
41+
42+
| Key | Purpose |
43+
| ----------------------- | ------------------------------------------------------------- |
44+
| `skip-prune` | Keep schemas not referenced by any retained operation/schema. |
45+
| `include-tags` | Only generate operations with one of these tags. |
46+
| `exclude-tags` | Skip operations with any of these tags. |
47+
| `include-operation-ids` | Only generate these `operationId`s. |
48+
| `exclude-operation-ids` | Skip these `operationId`s. |
49+
| `exclude-schemas` | Drop these component schemas before lowering. |
50+
51+
## Example
52+
53+
```yaml
54+
package: restapi
55+
output: generated/restapi.rs
56+
generate:
57+
std-http-server: true
58+
models: true
59+
server-urls: true
60+
import-mapping:
61+
schemas/common.yaml: crate::apimodel::common
62+
```

docs/design.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Design decisions vs. Go
2+
3+
Rust differs enough from Go that a faithful port makes different choices in
4+
places. This lists where `oapi-codegen-rust` deviates from
5+
[`oapi-codegen`](https://github.com/oapi-codegen/oapi-codegen) and why.
6+
7+
## Server: native async trait, axum only
8+
9+
- **Go:** generates for many frameworks (chi, echo, gin, fiber, gorilla, iris,
10+
stdhttp) from a plain-method interface.
11+
- **Rust:** one `trait Api` using native async-fn-in-traits
12+
(`fn op(&self, ..) -> impl Future<Output = ..> + Send`), targeting axum 0.8.
13+
No `async-trait` dependency.
14+
- **Why:** stable Rust does async-in-traits natively; one well-supported
15+
framework keeps output idiomatic without a macro dependency.
16+
- There is a single server mode — a typed `trait Api`, comparable to Go's
17+
strict server. There is no unstructured handler variant.
18+
19+
## Typed status-code response enums
20+
21+
- **Go:** per-status response structs / context returns.
22+
- **Rust:** one `enum <Op>Response` per operation with a variant per declared
23+
status. The same enum implements `IntoResponse` on the server and is the
24+
client's success type, so a new status is a compile-enforced exhaustive match
25+
on both sides.
26+
27+
## Token-based generation, not templates
28+
29+
- **Go:** user-overridable `text/template`.
30+
- **Rust:** build a `TokenStream` with `quote!`, parse to `syn::File` (which
31+
guarantees syntactically valid Rust), and pretty-print with `prettyplease`.
32+
33+
## Blocking `reqwest` client
34+
35+
- Returns `Result<<Op>Response, ClientError>` with a single hand-written
36+
`ClientError` enum. Blocking by default, so no async runtime is forced on
37+
consumers.
38+
39+
## Derive HTTP status semantics
40+
41+
- Status codes come from the `http` / `axum` / `reqwest` `StatusCode` types
42+
rather than a hand-maintained reason/table mirror.
43+
44+
## Vendor extensions
45+
46+
- `x-rust-type`, `x-rust-name`, `x-rust-serde-skip`, plus `oapi-codegen`
47+
compatibility keys (`x-omitempty`, `x-order`, `x-deprecated-reason`,
48+
`x-enum-varnames` / `x-enumNames`).
49+
- `x-go-*` keys are ignored (accepted, no effect). See
50+
[extensions](extensions.md).
51+
52+
## Combined output via submodules
53+
54+
- Emitting server and client together puts shared component models at the crate
55+
root and the two targets in `server` / `client` submodules, resolving the
56+
same-named/different-shaped per-operation items.
57+
58+
## OpenAPI 3.0
59+
60+
- The generator reads OpenAPI 3.0 documents (via the `openapiv3` crate).
61+
62+
## Config compatibility
63+
64+
- The YAML config mirrors `oapi-codegen`'s keys, and unknown keys are ignored,
65+
so an existing Go config can be reused as-is. See
66+
[configuration](configuration.md).

docs/extensions.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# OpenAPI extensions
2+
3+
The generator honors the vendor extensions below. Rust-specific behaviour uses
4+
`x-rust-*` keys; the rest are `oapi-codegen` compatibility keys. Any other
5+
extension — including the Go type keys `x-go-type`, `x-go-name`,
6+
`x-go-json-ignore` — is ignored (accepted, but has no effect); use the
7+
`x-rust-*` equivalents instead.
8+
9+
| Extension | Applies to | Effect |
10+
| --------------------------------- | ----------------- | --------------------------------------------------------------- |
11+
| `x-rust-type` | schema | Emit this verbatim Rust type instead of a generated one. |
12+
| `x-rust-name` | schema / property | Override the generated type or field identifier. |
13+
| `x-rust-serde-skip` | property | Drop the field with `#[serde(skip)]`. |
14+
| `x-omitempty` | property | Force `skip_serializing_if` on/off, overriding the default. |
15+
| `x-order` | property | Order struct fields explicitly (1-indexed). |
16+
| `x-deprecated-reason` | schema / property | Note for `#[deprecated]`; honored only when `deprecated: true`. |
17+
| `x-enum-varnames` / `x-enumNames` | enum schema | Override generated enum variant identifiers, positionally. |
18+
19+
## Example
20+
21+
```yaml
22+
components:
23+
schemas:
24+
Widget:
25+
type: object
26+
properties:
27+
id:
28+
type: string
29+
x-rust-name: widget_id
30+
cached_at:
31+
type: string
32+
x-rust-serde-skip: true
33+
raw:
34+
x-rust-type: serde_json::Value
35+
```

docs/installation.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Installation
2+
3+
## Install
4+
5+
```sh
6+
cargo install oapi-codegen
7+
```
8+
9+
Verify:
10+
11+
```sh
12+
oapi-codegen --version
13+
```
14+
15+
## From source
16+
17+
```sh
18+
git clone https://github.com/alchemaxinc/oapi-codegen-rust
19+
cd oapi-codegen-rust
20+
cargo install --path crates/oapi-codegen
21+
```
22+
23+
Or run it in-tree without installing:
24+
25+
```sh
26+
cargo run -p oapi-codegen -- --help
27+
```

0 commit comments

Comments
 (0)