nori/codegen/ir
Language-agnostic intermediate representation for code generation.
Sits between the OpenAPI Document and language-specific generators.
Decouples parsing from generation: third-party emitters (other languages,
other frameworks) can consume CodegenIR without depending on the parser
or knowing the OpenAPI spec details.
Build one with nori.build_ir(doc), then walk it. The shape is intended
to be stable and human-readable — semantic versioning rules of nori
itself apply to this module.
Types
API key location.
pub type ApiKeyLocationIR {
InHeader
InQuery
InCookie
}
Constructors
-
InHeader -
InQuery -
InCookie
The complete codegen IR — everything a generator needs to produce code.
pub type CodegenIR {
CodegenIR(
title: String,
version: String,
base_url: option.Option(String),
types: List(TypeDef),
endpoints: List(Endpoint),
security_schemes: List(SecuritySchemeIR),
global_security: List(SecurityRequirementIR),
)
}
Constructors
-
CodegenIR( title: String, version: String, base_url: option.Option(String), types: List(TypeDef), endpoints: List(Endpoint), security_schemes: List(SecuritySchemeIR), global_security: List(SecurityRequirementIR), )Arguments
- global_security
-
Global security requirements (applied to all endpoints unless overridden)
Validation keywords from the schema a field or parameter was built from.
They apply to the value itself: for a nullable or optional value, to the
value when present; for an array, min_items/max_items count the list and
items holds the constraints of each element. Bounds are kept as written,
so a generator decides how to compare an integer with a fractional bound.
pub type Constraints {
Constraints(
minimum: option.Option(Float),
maximum: option.Option(Float),
exclusive_minimum: option.Option(Float),
exclusive_maximum: option.Option(Float),
min_length: option.Option(Int),
max_length: option.Option(Int),
pattern: option.Option(String),
min_items: option.Option(Int),
max_items: option.Option(Int),
items: option.Option(Constraints),
)
}
Constructors
-
Constraints( minimum: option.Option(Float), maximum: option.Option(Float), exclusive_minimum: option.Option(Float), exclusive_maximum: option.Option(Float), min_length: option.Option(Int), max_length: option.Option(Int), pattern: option.Option(String), min_items: option.Option(Int), max_items: option.Option(Int), items: option.Option(Constraints), )
How a tagged union picks its member: the property that carries the tag, and every tag value paired with the member it selects.
mapping is already resolved. A member the spec’s mapping does not name
gets its schema name as its tag, which is what OpenAPI prescribes, so a
generator never has to know about the implicit case.
pub type Discriminator {
Discriminator(
property: String,
mapping: List(#(String, TypeRef)),
)
}
Constructors
-
Discriminator( property: String, mapping: List(#(String, TypeRef)), )
An API endpoint.
pub type Endpoint {
Endpoint(
operation_id: String,
method: HttpMethod,
path: String,
summary: option.Option(String),
description: option.Option(String),
tags: List(String),
parameters: List(EndpointParam),
request_body: option.Option(RequestBodyIR),
responses: List(ResponseIR),
deprecated: Bool,
security: option.Option(List(SecurityRequirementIR)),
extensions: dict.Dict(String, json.Json),
)
}
Constructors
-
Endpoint( operation_id: String, method: HttpMethod, path: String, summary: option.Option(String), description: option.Option(String), tags: List(String), parameters: List(EndpointParam), request_body: option.Option(RequestBodyIR), responses: List(ResponseIR), deprecated: Bool, security: option.Option(List(SecurityRequirementIR)), extensions: dict.Dict(String, json.Json), )Arguments
- security
-
Per-endpoint security. None = use global, Some([]) = public, Some([…]) = specific
- extensions
-
The operation’s
x-*extensions, as written in the spec. Generators read the ones they know (the MCP generator readsx-mcp).
An endpoint parameter.
pub type EndpointParam {
EndpointParam(
name: String,
location: ParamLocation,
type_ref: TypeRef,
required: Bool,
description: option.Option(String),
constraints: Constraints,
)
}
Constructors
-
EndpointParam( name: String, location: ParamLocation, type_ref: TypeRef, required: Bool, description: option.Option(String), constraints: Constraints, )
An enum variant.
pub type EnumVariant {
EnumVariant(name: String, value: String)
}
Constructors
-
EnumVariant(name: String, value: String)
A field in a record type.
pub type Field {
Field(
name: String,
type_ref: TypeRef,
required: Bool,
description: option.Option(String),
read_only: Bool,
write_only: Bool,
constraints: Constraints,
)
}
Constructors
-
Field( name: String, type_ref: TypeRef, required: Bool, description: option.Option(String), read_only: Bool, write_only: Bool, constraints: Constraints, )
HTTP methods.
pub type HttpMethod {
Get
Post
Put
Delete
Patch
Head
Options
}
Constructors
-
Get -
Post -
Put -
Delete -
Patch -
Head -
Options
Parameter locations.
pub type ParamLocation {
PathParam
QueryParam
HeaderParam
CookieParam
}
Constructors
-
PathParam -
QueryParam -
HeaderParam -
CookieParam
Primitive types.
pub type PrimitiveType {
PString
PInt
PFloat
PBool
PDateTime
PDate
PBinary
PUnit
}
Constructors
-
PString -
PInt -
PFloat -
PBool -
PDateTime -
PDate -
PBinary -
PUnit
Response IR.
pub type ResponseIR {
ResponseIR(
status_code: String,
description: String,
content_type: option.Option(String),
type_ref: option.Option(TypeRef),
)
}
Constructors
-
ResponseIR( status_code: String, description: String, content_type: option.Option(String), type_ref: option.Option(TypeRef), )
A security requirement: which scheme + required scopes.
pub type SecurityRequirementIR {
SecurityRequirementIR(
scheme_name: String,
scopes: List(String),
)
}
Constructors
-
SecurityRequirementIR(scheme_name: String, scopes: List(String))
A security scheme definition from components/securitySchemes.
pub type SecuritySchemeIR {
BearerAuth(name: String, format: option.Option(String))
ApiKeyAuth(
name: String,
param_name: String,
location: ApiKeyLocationIR,
)
BasicAuth(name: String)
OAuth2Auth(name: String)
OpenIdConnectAuth(name: String, url: String)
}
Constructors
-
BearerAuth(name: String, format: option.Option(String))Bearer token (Authorization: Bearer
) -
ApiKeyAuth( name: String, param_name: String, location: ApiKeyLocationIR, )API key in header, query, or cookie
-
BasicAuth(name: String)HTTP Basic auth
-
OAuth2Auth(name: String)OAuth2 — we store just the name, user implements the flow
-
OpenIdConnectAuth(name: String, url: String)OpenID Connect
A named type definition.
pub type TypeDef {
RecordType(
name: String,
fields: List(Field),
description: option.Option(String),
)
EnumType(
name: String,
variants: List(EnumVariant),
description: option.Option(String),
)
UnionType(
name: String,
members: List(TypeRef),
discriminator: option.Option(Discriminator),
description: option.Option(String),
)
AliasType(
name: String,
target: TypeRef,
description: option.Option(String),
)
}
Constructors
-
RecordType( name: String, fields: List(Field), description: option.Option(String), )Object type with fields, e.g., User { id, name, email }
-
EnumType( name: String, variants: List(EnumVariant), description: option.Option(String), )Enum type, e.g., Status = “active” | “inactive”
-
UnionType( name: String, members: List(TypeRef), discriminator: option.Option(Discriminator), description: option.Option(String), )Union type, e.g., UserOrAdmin = User | Admin
-
AliasType( name: String, target: TypeRef, description: option.Option(String), )Type alias, e.g., type UserId = String
A type reference — describes what type a field or parameter has.
pub type TypeRef {
Named(name: String)
Primitive(PrimitiveType)
Array(item: TypeRef)
Dict(key: TypeRef, value: TypeRef)
Nullable(inner: TypeRef)
Optional(inner: TypeRef)
Literal(value: String)
Unknown
}
Constructors
-
Named(name: String)Reference to another named type
-
Primitive(PrimitiveType)Primitive type
-
Array(item: TypeRef)Array of a type
-
Dictionary/map type
-
Nullable(inner: TypeRef)Nullable type (T | null)
-
Optional(inner: TypeRef)Optional type (may be absent)
-
Literal(value: String)Literal string value
-
UnknownUnknown/any type
Values
pub fn base_primitive(
ref: TypeRef,
) -> option.Option(PrimitiveType)
The primitive a value ultimately renders from, looking through optional and
array wrappers. None for named, dict, or unknown types.
pub fn mentions_primitive(
ref: TypeRef,
prim: PrimitiveType,
) -> Bool
Whether prim appears anywhere in ref: through lists, map values and
optional wrappers. base_primitive stops at a map.
pub fn param_scalar(
ref: TypeRef,
enum_names: List(String),
) -> TypeRef
What a path, query or header value is written and read as: an enum in
enum_names, an Int, Float or Bool, and otherwise text. A record, union,
map or Unknown has no single string form, so the caller supplies one.
The client’s arguments, the routes’ query records and the MCP decoder all type the same parameter, so they must agree; this is the one rule. A list parameter applies it to its elements.
pub fn strip_optional(ref: TypeRef) -> TypeRef
Drops Nullable / Optional wrappers, leaving the type underneath.
Every generator needs this to decide how a value is rendered, and three of
them had grown their own copy under three names. Type names become
cross-module calls, so the copies drifting is the same class of bug that
moved the case helpers into naming.
pub fn success_response(
ep: Endpoint,
) -> option.Option(ResponseIR)
The response an endpoint’s success is typed by: its lowest 2xx status.
The client’s decoder, the routes’ handler type and the MCP output schema all describe this one response, so they take it from here rather than each picking a 2xx by its own rule.