OpenAPI
Officialv5.8.3@kubb/adapter-oas
Options
Pass these options to adapterOas(). All of them are optional.
Options overview
| Option | Purpose |
|---|---|
validate | Validate the spec before parsing. |
contentType | Preferred media type for request and response schemas. |
server | Which spec server Kubb resolves into the document baseURL. |
discriminator | How discriminator fields are interpreted. |
enums | Where inline enums live. |
dateType | How date-time, date, and time schemas are represented. |
integerType | How integers map to TypeScript. |
unknownType | Type for schemas Kubb cannot infer. |
emptySchemaType | Type for empty schemas. |
enumSuffix | Suffix for derived enum names. |
Option details
validate
Validates the OpenAPI spec with @readme/openapi-parser before parsing. Each problem is reported as a KUBB_INVALID_SPEC warning, and generation continues. Set it to false to skip the check, which makes generation faster on a large spec. Run kubb validate to list every error.
| Type | boolean |
| Required | false |
| Default | true |
contentType
Media type to keep when an operation declares several, for both request bodies and responses. Without a value, Kubb keeps every media type the operation declares.
| Type | 'application/json' | string |
| Required | false |
server
Selects which entry in the spec's servers array Kubb resolves into the document baseURL, filling in any {variable} placeholders. server.index points at one of the spec's servers, usually 0 for the primary one. server.variables supplies placeholder values, falling back to each variable's default from the spec. Omit server and baseURL resolves to null.
| Type | { index?: number, variables?: Record<string, string> } |
| Required | false |
The resolved baseURL reaches banner functions through BannerMeta.baseURL but does not set request URLs on its own. To change where a generated client sends requests, use that plugin's own baseURL option (@kubb/plugin-fetch, @kubb/plugin-axios, @kubb/plugin-msw).
With a spec server of https://api.{env}.example.com, server: { index: 0, variables: { env: 'prod' } } resolves baseURL to https://api.prod.example.com.
discriminator
How discriminator fields on oneOf/anyOf schemas are interpreted.
| Type | 'preserve' | 'propagate' |
| Required | false |
| Default | 'preserve' |
type field is precisely typed.openapi: 3.0.3
components:
schemas:
Animal:
required: [type]
type: object
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
discriminator:
propertyName: type
mapping:
cat: '#/components/schemas/Cat'
dog: '#/components/schemas/Dog'
Cat:
type: object
properties:
type: { type: string }
indoor: { type: boolean }
Dog:
type: object
properties:
type: { type: string }
name: { type: string }export type Cat = { type: string; indoor?: boolean }
export type Dog = { type: string; name?: string }
export type Animal = Cat | Dogexport type Cat = { type: 'cat'; indoor?: boolean }
export type Dog = { type: 'dog'; name?: string }
export type Animal = Cat | Dogenums
Where inline enums live.
| Type | 'inline' | 'root' |
| Required | false |
| Default | 'inline' |
PetStatusEnum) and references it wherever it appears.For an enum with values active and inactive on Pet.status:
export type Pet = { status?: 'active' | 'inactive' }export type PetStatusEnum = 'active' | 'inactive'
export type Pet = { status?: PetStatusEnum }dateType
How date-time, date, and time schemas are represented downstream.
| Type | false | 'string' | 'stringOffset' | 'stringLocal' | 'date' | { dateTime?, date?, time? } |
| Required | false |
| Default | 'string' |
Pass a single value to apply it to all three formats:
date-time, date, and time as ISO 8601 strings. The generated TypeScript type is string.date-time as a string with a timezone offset. The date and time formats fall back to 'string'.date-time as a local string without a timezone. The date and time formats fall back to 'string'.Date objects. JSON values need parsing to revive them as Date objects.The string variants all emit string at the TypeScript type level. Schema plugins such as @kubb/plugin-zod validate the offset or local format.
Pass an object to set dateTime, date, and time independently. A key you leave out defaults to 'string', regardless of the other keys.
adapterOas({
dateType: {
dateTime: 'date', // Date object for timestamps
date: 'string',
time: 'string',
},
})integerType
How type: integer (and format: int64) maps to TypeScript.
| Type | 'number' | 'bigint' |
| Required | false |
| Default | 'bigint' |
JSON.stringify and JSON.parse cannot round-trip it. Use it only when you handle bigint serialization yourself.Number.MAX_SAFE_INTEGER.A schema with type: string stays a string whatever its format, so the ProtoJSON int64 strings that gRPC-gateway emits stay strings. @kubb/plugin-zod validates them with a digits regex, and @kubb/plugin-faker mocks them as numeric strings.
unknownType
AST type used when a schema's type cannot be inferred from the spec (additionalProperties: true, a missing type, and similar).
| Type | 'any' | 'unknown' | 'void' |
| Required | false |
| Default | 'unknown' |
void.emptySchemaType
AST type used for empty schemas ({}). It takes the same values as unknownType and follows it unless you set it.
| Type | 'any' | 'unknown' | 'void' |
| Required | false |
| Default | unknownType ('unknown' by default) |
unknownType: 'unknown' for safety and emptySchemaType: 'any' so empty 204 response bodies stay easy to use.enumSuffix
Suffix for the names Kubb gives inline enums: schema name, property name, and suffix, in PascalCase. The status enum on Pet becomes PetStatusEnum, or PetStatusType with enumSuffix: 'type'.
| Type | string |
| Required | false |
| Default | 'enum' |