Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Generation Options

Generation options choose the output layout, select operations, and control the generated API’s names and visibility. Each section here explains what an option does and links to the walkthrough that shows it in use. The Flag Summary lists every option with its default.

Commands run from a checkout of this repository. If you installed the tool, replace cargo run -- with oas3-gen. Use the path to your own document in place of spec.json or petstore.json. Unless stated otherwise, examples refer to fixtures/petstore.json; Rust excerpts may omit imports, attributes, and unrelated definitions.

Generation Modes

The positional mode argument selects the kind of output. Choose types when you need data types, client or client-mod when you want to call an API, and server-mod when you want to implement one:

cargo run -- generate <MODE> -i spec.json -o <OUTPUT>
ModeOutputLearn More
types (default)Type definitions in one fileSchemas and Types
clientTypes and a reqwest client in one fileChoosing the Output Layout
client-modtypes.rs, client.rs, and mod.rs in a directoryClient Generation
server-modtypes.rs, server.rs, and mod.rs in a directoryServer Generation

In every mode, a type’s derives follow how the API uses it. The petstore’s Pet appears only in responses, so it derives Deserialize but not Serialize. NewPet, which the API only accepts, derives Serialize and Validate instead. Server output reverses those directions.

Workspace Crate Output

Use --workspace when you want the output to be its own crate, with a Cargo.toml and a src/ directory. You can build that crate standalone or add it to a Cargo workspace. The flag is available with client-mod and server-mod; using it with types or client produces a generation error.

-w, --workspace

The crate’s src/lib.rs takes the place of mod.rs as the library root, and the type and client or server files contain the same code as module output. To see a generated crate and add it to a workspace, follow Generating a Separate Crate for a client or the server version for a server.

Crate Name

The package takes its name from the final component of the output directory, so -o petstore-api produces name = "petstore-api" and a petstore_api library target. Names are transliterated to ASCII and converted to kebab-case: -o "output/Pet Store" yields pet-store, -o café-api yields cafe-api, and -o petstore_v2 yields petstore-v2. The library target of petstore-v2 is still petstore_v2, because Cargo maps dashes to underscores.

Generation fails when no valid name can be derived, for example when the directory name is empty after sanitization or starts with a digit.

Generated Manifest

The manifest declares a [package] and the dependencies its generated code uses. Its description comes from the document’s info.title. The generator rewrites Cargo.toml on every run and adds a banner identifying it as generated output, so changes you make to it don’t survive regeneration.

The package version starts at 0.0.0. Pass --module-version to set a version for the generated crate:

cargo run -- generate client-mod -i petstore.json -o petstore-api --workspace --module-version 1.0.0

The --module-version flag requires --workspace, because that’s the mode that creates the manifest.

Dependencies

Each generator release contains the dependency version requirements it writes to the manifest. It doesn’t read them from your project’s environment. The requirement for oas3-gen-support uses the generator’s version. Cargo resolves these requirements when you build the generated crate. If you include module output in an existing crate instead, add the same dependencies to that crate’s manifest.

The manifest declares crates referenced by the generated source. For example, a specification without date or time types doesn’t need a chrono entry, and server output doesn’t need the client’s reqwest dependency. The following table describes the dependencies you may see:

CrateDeclared when the generated code
anyhowreturns anyhow::Result from client or server methods
axumdefines a server-mod router and extractors, with the multipart feature
axum-extrareads query parameters or cookie API keys on the server, with the cookie and query features
bonderives builders (--enable-builders)
chronomaps date, date-time, or time formats
futuresreturns or sends a server’s text/event-stream as a BoxStream
httpemits header constants or HeaderMap conversions
indexmapemits IndexMap/IndexSet collection types
oas3-gen-supportuses runtime derives, diagnostics, multipleOf checks, or reads event streams on the client
regexemits pattern validation constants
reqwestperforms client-mod HTTP calls; the form feature sends application/x-www-form-urlencoded bodies
reqwest_cookie_storesends cookie API keys or encodes cookie parameters from the client
secrecystores API keys and bearer tokens
serdederives Serialize/Deserialize
serde_html_formserializes query parameters on the client, with only its ser and std features
serde_jsonhandles freeform serde_json::Value payloads
serde_withapplies serde_as conversions
uuidmaps the uuid format
validatorderives Validate

serde_json gains the preserve_order feature under the default collection policy so that freeform JSON objects keep their key order. With --no-ordered-collections the feature is omitted. Likewise, validator gains its indexmap feature when the generated code also uses indexmap. The feature checks the length of an IndexSet or IndexMap field, such as the maxItems limit on the tags of NewPet.

Operation Filtering

You may only need a few operations from a large API. Use --only to select those operations, or --exclude to leave specific operations out. You can use one of these flags at a time.

--only <id1,id2,...>
--exclude <id1,id2,...>

Both flags take the normalized snake_case operation IDs that oas3-gen list operations shows, separated by commas. Filtering uses these IDs before any --fn-name overrides. For example, --only list_pets,create_pet keeps the petstore’s listPets and createPet operations, and --exclude upload_pet_image keeps every operation except uploadPetImage.

A client omits the methods you filter out, and a server omits their trait methods and routes. In a server, a callback follows the operation that declares it unless you name the callback’s own ID. See Choosing Operations and Names for a filtered client, and the server version for callbacks.

Schema Dependency Resolution

Filtering an operation also filters the types needed to use it. The generator collects the schemas used by the selected operations’ parameters, request bodies, and responses, then follows references from those schemas to other schemas. For example, the petstore’s listPets returns Pets, an array of Pet values. Selecting only list_pets also generates the schemas Pet refers to, such as Species and PetStatus, though the operation doesn’t refer to them directly.

Schema Filtering

By default, the generator includes schemas used by the selected operations and the schemas they depend on. Pass --all-schemas when you also need types that no operation references.

--all-schemas

Suppose a specification defines Pet, Category, Store, and Inventory, but its operations only use Pet and Category. By default, the output contains Pet and Category, and with --verbose the generator reports how many orphaned schemas it skipped. With --all-schemas, the output contains all four types.

You can’t combine --all-schemas with --only or --exclude in one command. If you need both a complete set of types and a filtered client, generate two outputs:

cargo run -- generate types -i spec.json -o types.rs --all-schemas
cargo run -- generate client -i spec.json -o client.rs --only list_pets

The first file contains all schemas. The second contains the filtered client and its own required types; these commands don’t make the client import types from the first file.

API Name Override

Use --api-name to name the client struct in client or client-mod mode, or the server trait in server-mod mode. The generator converts the supplied name to PascalCase.

--api-name <NAME>

By default, the client name comes from the specification’s info.title. For example, the petstore’s title, "Petstore", becomes PetstoreClient; an empty title produces ApiClient. The default server trait name is ApiServer. Changing the API name doesn’t rename the operations; use --fn-name for those. See naming the client and naming the server trait for examples.

Function Name Overrides

Use --fn-name to choose the generated client or server method name for an operation. The key is its operationId from the specification; the generator also accepts the normalized operation ID shown by oas3-gen list operations. Repeat the flag to rename more than one operation.

--fn-name <ID=NAME>

The generator converts your method name to snake_case and uses it to derive the request type’s name. For example, --fn-name listPets=fetch_all_pets produces the method fetch_all_pets and the request type FetchAllPetsRequest. Use the new names when calling the client or implementing the server trait, as Choosing Operations and Names shows.

Visibility

Use --visibility to choose where generated items can be accessed. The default, public, makes them available to other crates. Choose crate when the generated API is an internal part of your application.

-C, --visibility <LEVEL>
ValueModifierUse Case
public (default)pubLibrary distribution
cratepub(crate)Internal crate types
filepub(super)Items used only inside the generated module

The modifier applies to generated types, their fields, and their methods, such as the helper methods on unions. With --visibility crate, for example, the petstore’s Pet struct and each of its fields are pub(crate).

In module output, pub(super) lets the generated files use each other’s items: client.rs or server.rs can reach the types in types.rs. The module’s mod.rs or lib.rs doesn’t re-export anything, so the items stay inside the generated module. Keep the default public for a workspace crate that other crates depend on, because crate and file restrict the re-exports in lib.rs.

Header Emission

Header name constants use http::HeaderName, so you can refer to headers without repeating their string names. By default, the generator creates constants for request parameters and response headers used by the selected operations. Use --all-headers to include header parameters from components/parameters even when no operation references them.

--all-headers

For example, the petstore’s showPetById operation uses an x-api-version header, so the output contains an X_API_VERSION constant. The document also declares an x-debug-trace header parameter in components/parameters that no operation uses. With --all-headers, the output gains a constant for it:

pub const X_DEBUG_TRACE: http::HeaderName = http::HeaderName::from_static(
    "x-debug-trace",
);

Headers that the http crate already names are the exception. The http::header module declares constants for standard headers such as Content-Type, Location, and Authorization. The generator doesn’t declare its own constant for any of them; generated code refers to http’s constant, such as http::header::LOCATION, instead.

OpenAPI says to ignore header parameters named Accept, Content-Type, or Authorization, so the generator leaves them out of request types. Media types cover Accept and Content-Type, and security schemes cover Authorization. For example, the petstore’s listAuditEvents declares a required Authorization header parameter, yet its request struct has no header field. To send credentials in Authorization, declare a security scheme, as that operation also does.

Builder Generation

Use --enable-builders when you want to construct generated values through setter methods. The generator adds bon::Builder derives to schema structs and #[builder] constructors to request structs. A request constructor assembles the nested parameter structs and validates the request before returning it.

--enable-builders

Builders are disabled by default, and enabling them adds a bon dependency. The Builder Pattern chapter shows what changes in the generated code, how to add bon to your project, and how to use each kind of builder.

Raw Response Bodies

Use --raw-body when a client downloads bodies you don’t want to hold in memory all at once, such as large files. By default, the client reads each binary response body into a Vec<u8> before it returns. With the flag, the client returns the body unread as a reqwest::Body, and your code reads it as it arrives.

--raw-body

For example, the petstore client’s get_pet_photo() returns ApiResponse<reqwest::Body, Problem> instead of ApiResponse<Vec<u8>, Problem>. The flag doesn’t change request bodies or server-mod output. Streaming Binary Bodies lists the response bodies it affects and shows how to read one as a stream.

Documentation Formatting

Descriptions and summaries in the OpenAPI document become documentation comments in the generated code. Use --doc-format to format longer comments with the external mdformat tool. The generator sends text longer than 100 bytes to mdformat with a line-wrap setting of 100 characters.

--doc-format

Install mdformat and make sure it’s available on your PATH before using the flag:

pip install mdformat

Without the flag, the generator preserves documentation text while converting escaped newlines to line breaks, so a long description can remain on one line. With the flag, the petstore’s CollarTelemetry description is wrapped into multiple lines:

/// A batch of smart-collar readings, written with JSON Schema 2020-12 keywords. Keywords without a Rust
/// equivalent are accepted and ignored.
pub struct CollarTelemetry {
    /* ... */
}

Known Limitations

The following constraints and serialization options aren’t fully represented in the generated code. The petstore examples show where each limitation applies:

  • With --no-ordered-collections, an array with uniqueItems: true, such as the tags of Pet, is a Vec and accepts repeated items. See Ordering and Collections.
  • An optional field that also allows null is an Option<T>, so JSON null and a missing property both decode as None, and None is left out when serializing. A client therefore can’t send null to clear a field with a JSON merge patch, such as the nickname in the PetPatch body of updatePet. Likewise, a field without a type, such as anyValue in CollarTelemetry, decodes null as None.
  • The generator ignores allowReserved on query parameters. The client percent-encodes reserved characters, such as the / in a q value for searchPets.
  • A query parameter’s value must be a scalar or an array of scalars. A JSON object or an array of arrays in an untyped parameter, such as filter on searchPets, makes the client method return an error before it sends the request.
  • An anyOf whose branches each require a different property isn’t enforced. The PetCarrier schema requires either wheels or straps, but both become optional fields, so a carrier with neither decodes successfully.
  • A multipart part name can’t contain ", \, a carriage return, a line feed, or another control character. The generator warns about quotes and line breaks, as described in Generation Warnings.
  • A multipleOf that the generator can’t check exactly only appears in the field’s documentation. See Multiples.

Flag Summary

This table collects the options used across the generation chapters. Defaults apply when you omit the corresponding option, and the last column links to each option’s explanation. Run oas3-gen generate --help to see the command-line help, including input, output, and terminal options.

FlagDefaultDescriptionDetails
modetypesGeneration mode: types, client, client-mod, server-modGeneration Modes
-w, --workspacefalseEmit a workspace-compatible Cargo.toml with sources in src/; requires client-mod or server-modWorkspace Crate Output
--module-version0.0.0Version written to the generated Cargo.toml [package] table; requires --workspaceGenerated Manifest
-C, --visibilitypublicItem visibility: public, crate, fileVisibility
--enum-modemergeEnum duplicate handling: merge, preserve, relaxedEnum Mode
--enum-layoutspecVariant ordering: spec, sortedEnum Layout
--no-helpersfalseDisable enum constructor helpersHelper Methods
--odata-supportfalseMake @odata.* fields optionalOData Support
-c, --customize(none)Custom serde_with adapter; repeatableType Customization
--fn-name(none)Custom function name per operation (ID=NAME); repeatableFunction Name Overrides
--api-name(none)Name for the generated client struct or server traitAPI Name Override
--all-headersfalseEmit header constants for all component-level headersHeader Emission
--enable-buildersfalseEnable bon builder derives and methodsBuilder Generation
--raw-bodyfalseReturn client binary response bodies as an unread reqwest::Body instead of Vec<u8>Raw Response Bodies
--no-ordered-collectionsfalseEmit HashMap/Vec instead of indexmap collection typesOrdering and Collections
--doc-formatfalseFormat documentation comments with mdformatDocumentation Formatting
--only(none)Include only specified operationsOperation Filtering
--exclude(none)Exclude specified operationsOperation Filtering
--all-schemasfalseGenerate all schemas regardless of usageSchema Filtering