oaspec/openapi

The OpenAPI document as oaspec reads it, and the diagnostics oaspec reports about it.

oaspec.parse_file and oaspec.parse_string return an OpenApiSpec(Unresolved); oaspec.resolve turns it into an OpenApiSpec(Resolved) in which every RefOr is a Value. Every failure and warning is a Diagnostic.

Types

How additionalProperties is modeled in the AST.

pub type AdditionalProperties {
  Forbidden
  Untyped
  Typed(SchemaRef)
  Unspecified
}

Constructors

  • Forbidden

    additionalProperties: false (extra keys rejected)

  • Untyped

    additionalProperties: true (accept any JSON value, surfaced in generated types as Dict(String, Dynamic))

  • Typed(SchemaRef)

    additionalProperties: { schema } (typed dict in generated types)

  • Unspecified

    additionalProperties key absent from the spec. Per JSON Schema any extra keys are still permitted at runtime, but the generated record does not surface them — users opt in by writing true or a schema explicitly.

A callback object: maps URL expressions to PathItems.

pub type Callback(stage) {
  Callback(entries: dict.Dict(String, RefOr(PathItem(stage))))
}

Constructors

Components section containing reusable schemas, parameters, etc.

pub type Components(stage) {
  Components(
    schemas: dict.Dict(String, SchemaRef),
    parameters: dict.Dict(String, RefOr(Parameter(stage))),
    request_bodies: dict.Dict(String, RefOr(RequestBody(stage))),
    responses: dict.Dict(String, RefOr(Response(stage))),
    security_schemes: dict.Dict(String, RefOr(SecurityScheme)),
    path_items: dict.Dict(String, RefOr(PathItem(stage))),
    headers: dict.Dict(String, Header),
    examples: dict.Dict(String, JsonValue),
    links: dict.Dict(String, Link),
    callbacks: dict.Dict(String, RefOr(Callback(stage))),
  )
}

Constructors

Contact information for the API.

pub type Contact {
  Contact(
    name: option.Option(String),
    url: option.Option(String),
    email: option.Option(String),
  )
}

Constructors

An error or warning about a document. code is a stable identifier such as "missing_field", pointer is the JSON pointer of the node (empty when there is none), and hint suggests a fix.

pub type Diagnostic {
  Diagnostic(
    code: String,
    phase: Phase,
    severity: Severity,
    target: Target,
    pointer: String,
    source_loc: SourceLoc,
    message: String,
    hint: option.Option(String),
  )
}

Constructors

OpenAPI discriminator for oneOf/anyOf.

pub type Discriminator {
  Discriminator(
    property_name: String,
    mapping: dict.Dict(String, String),
  )
}

Constructors

  • Discriminator(
      property_name: String,
      mapping: dict.Dict(String, String),
    )

Encoding definition for a media type property.

pub type Encoding {
  Encoding(
    content_type: option.Option(String),
    style: option.Option(ParameterStyle),
    explode: option.Option(Bool),
  )
}

Constructors

An external documentation reference.

pub type ExternalDoc {
  ExternalDoc(url: String, description: option.Option(String))
}

Constructors

Header definition for responses or components.

pub type Header {
  Header(
    description: option.Option(String),
    required: Bool,
    schema: option.Option(SchemaRef),
  )
}

Constructors

Type-safe HTTP status code representation. Replaces raw String status codes throughout the codebase.

pub type HttpStatusCode {
  Status(Int)
  StatusRange(Int)
  DefaultStatus
}

Constructors

  • Status(Int)

    An exact HTTP status code, e.g. 200, 404, 500.

  • StatusRange(Int)

    A wildcard range, e.g. 2XX is StatusRange(2), 4XX is StatusRange(4).

  • DefaultStatus

    The OpenAPI “default” catch-all response.

API metadata.

pub type Info {
  Info(
    title: String,
    description: option.Option(String),
    version: String,
    summary: option.Option(String),
    terms_of_service: option.Option(String),
    contact: option.Option(Contact),
    license: option.Option(License),
  )
}

Constructors

A JSON-compatible value type for preserving arbitrary data from OpenAPI specs. Used for example, default, const, and other values that aren’t necessarily strings.

pub type JsonValue {
  JsonNull
  JsonBool(Bool)
  JsonInt(Int)
  JsonFloat(Float)
  JsonString(String)
  JsonArray(List(JsonValue))
  JsonObject(dict.Dict(String, JsonValue))
}

Constructors

  • JsonNull
  • JsonBool(Bool)
  • JsonInt(Int)
  • JsonFloat(Float)
  • JsonString(String)
  • JsonArray(List(JsonValue))
  • JsonObject(dict.Dict(String, JsonValue))

License information for the API.

pub type License {
  License(name: String, url: option.Option(String))
}

Constructors

Link definition for responses or components.

pub type Link {
  Link(
    operation_id: option.Option(String),
    description: option.Option(String),
  )
}

Constructors

Media type definition.

pub type MediaType {
  MediaType(
    schema: option.Option(SchemaRef),
    example: option.Option(JsonValue),
    examples: dict.Dict(String, JsonValue),
    encoding: dict.Dict(String, Encoding),
  )
}

Constructors

An OAuth2 flow definition.

pub type OAuth2Flow {
  OAuth2Flow(
    authorization_url: option.Option(String),
    token_url: option.Option(String),
    refresh_url: option.Option(String),
    scopes: dict.Dict(String, String),
  )
}

Constructors

Top-level OpenAPI 3.x specification.

pub type OpenApiSpec(stage) {
  OpenApiSpec(
    openapi: String,
    info: Info,
    paths: dict.Dict(String, RefOr(PathItem(stage))),
    components: option.Option(Components(stage)),
    servers: List(Server),
    security: List(SecurityRequirement),
    webhooks: dict.Dict(String, RefOr(PathItem(stage))),
    tags: List(Tag),
    external_docs: option.Option(ExternalDoc),
    json_schema_dialect: option.Option(String),
  )
}

Constructors

An API operation (endpoint).

pub type Operation(stage) {
  Operation(
    operation_id: option.Option(String),
    summary: option.Option(String),
    description: option.Option(String),
    tags: List(String),
    parameters: List(RefOr(Parameter(stage))),
    request_body: option.Option(RefOr(RequestBody(stage))),
    responses: dict.Dict(HttpStatusCode, RefOr(Response(stage))),
    deprecated: Bool,
    security: option.Option(List(SecurityRequirement)),
    callbacks: dict.Dict(String, RefOr(Callback(stage))),
    servers: List(Server),
    external_docs: option.Option(ExternalDoc),
  )
}

Constructors

Where a schema came from. Schemas read from the document are UserAuthored; the other variants mark schemas oaspec lifted out of an inline definition while generating code, and say where they were.

pub type OriginKind {
  UserAuthored
  HoistedProperty(parent: String, property: String)
  HoistedArrayItem(parent: String)
  HoistedOneOfVariant(parent: String, index: Int)
  HoistedAnyOfVariant(parent: String, index: Int)
  HoistedAllOfPart(parent: String, index: Int)
  HoistedRequestBody(operation_id: String)
  HoistedResponse(operation_id: String, status: String)
  HoistedParameter(operation_id: String, name: String)
  HoistedAdditionalProperties(parent: String)
}

Constructors

  • UserAuthored
  • HoistedProperty(parent: String, property: String)
  • HoistedArrayItem(parent: String)
  • HoistedOneOfVariant(parent: String, index: Int)
  • HoistedAnyOfVariant(parent: String, index: Int)
  • HoistedAllOfPart(parent: String, index: Int)
  • HoistedRequestBody(operation_id: String)
  • HoistedResponse(operation_id: String, status: String)
  • HoistedParameter(operation_id: String, name: String)
  • HoistedAdditionalProperties(parent: String)

An API parameter. Stage parameter is phantom.

pub type Parameter(stage) {
  Parameter(
    name: String,
    in_: ParameterIn,
    description: option.Option(String),
    required: Bool,
    payload: ParameterPayload,
    style: option.Option(ParameterStyle),
    explode: option.Option(Bool),
    deprecated: Bool,
    allow_reserved: Bool,
    examples: dict.Dict(String, JsonValue),
  )
}

Constructors

Parameter location.

pub type ParameterIn {
  InPath
  InQuery
  InHeader
  InCookie
}

Constructors

  • InPath
  • InQuery
  • InHeader
  • InCookie

How a parameter carries its type information.

pub type ParameterPayload {
  ParameterSchema(SchemaRef)
  ParameterContent(dict.Dict(String, MediaType))
}

Constructors

  • ParameterSchema(SchemaRef)

    Parameter defined via a JSON Schema.

  • ParameterContent(dict.Dict(String, MediaType))

    Parameter defined via content media type map (mutually exclusive with schema).

Parameter serialization style (OpenAPI 3.x).

pub type ParameterStyle {
  FormStyle
  SimpleStyle
  DeepObjectStyle
  MatrixStyle
  LabelStyle
  SpaceDelimitedStyle
  PipeDelimitedStyle
}

Constructors

  • FormStyle
  • SimpleStyle
  • DeepObjectStyle
  • MatrixStyle
  • LabelStyle
  • SpaceDelimitedStyle
  • PipeDelimitedStyle

A path item containing operations for each HTTP method.

pub type PathItem(stage) {
  PathItem(
    summary: option.Option(String),
    description: option.Option(String),
    get: option.Option(Operation(stage)),
    post: option.Option(Operation(stage)),
    put: option.Option(Operation(stage)),
    delete: option.Option(Operation(stage)),
    patch: option.Option(Operation(stage)),
    head: option.Option(Operation(stage)),
    options: option.Option(Operation(stage)),
    trace: option.Option(Operation(stage)),
    parameters: List(RefOr(Parameter(stage))),
    servers: List(Server),
  )
}

Constructors

The step of parsing or generation that reported a diagnostic.

pub type Phase {
  PhaseParse
  PhaseNormalize
  PhaseResolve
  PhaseCapabilityCheck
  PhaseValidate
  PhaseCodegen
}

Constructors

  • PhaseParse
  • PhaseNormalize
  • PhaseResolve
  • PhaseCapabilityCheck
  • PhaseValidate
  • PhaseCodegen

A definition given inline (Value) or by $ref (Ref).

pub type RefOr(a) {
  Ref(String)
  Value(a)
}

Constructors

  • Ref(String)
  • Value(a)

A request body definition. Stage parameter is phantom.

pub type RequestBody(stage) {
  RequestBody(
    description: option.Option(String),
    content: dict.Dict(String, MediaType),
    required: Bool,
  )
}

Constructors

Marks a document returned by oaspec.resolve, whose RefOr values are all Value.

pub type Resolved {
  Resolved
}

Constructors

  • Resolved

A response definition. Stage parameter is phantom.

pub type Response(stage) {
  Response(
    description: option.Option(String),
    content: dict.Dict(String, MediaType),
    headers: dict.Dict(String, Header),
    links: dict.Dict(String, Link),
  )
}

Constructors

The keywords every schema can carry. Every SchemaObject variant has it as its metadata field, so schema.metadata works on any variant.

pub type SchemaMetadata {
  SchemaMetadata(
    description: option.Option(String),
    nullable: Bool,
    deprecated: Bool,
    title: option.Option(String),
    read_only: Bool,
    write_only: Bool,
    default: option.Option(JsonValue),
    example: option.Option(JsonValue),
    const_value: option.Option(JsonValue),
    raw_type: option.Option(List(String)),
    unsupported_keywords: List(String),
    internal: Bool,
    provenance: OriginKind,
  )
}

Constructors

Represents a JSON Schema object within OpenAPI 3.x. This is the core building block for all type generation. All variants carry shared metadata for description, nullable, deprecated.

pub type SchemaObject {
  StringSchema(
    metadata: SchemaMetadata,
    format: option.Option(String),
    enum_values: List(String),
    min_length: option.Option(Int),
    max_length: option.Option(Int),
    pattern: option.Option(String),
  )
  IntegerSchema(
    metadata: SchemaMetadata,
    format: option.Option(String),
    minimum: option.Option(Int),
    maximum: option.Option(Int),
    exclusive_minimum: option.Option(Int),
    exclusive_maximum: option.Option(Int),
    multiple_of: option.Option(Int),
  )
  NumberSchema(
    metadata: SchemaMetadata,
    format: option.Option(String),
    minimum: option.Option(Float),
    maximum: option.Option(Float),
    exclusive_minimum: option.Option(Float),
    exclusive_maximum: option.Option(Float),
    multiple_of: option.Option(Float),
  )
  BooleanSchema(metadata: SchemaMetadata)
  ArraySchema(
    metadata: SchemaMetadata,
    items: SchemaRef,
    min_items: option.Option(Int),
    max_items: option.Option(Int),
    unique_items: Bool,
  )
  ObjectSchema(
    metadata: SchemaMetadata,
    properties: dict.Dict(String, SchemaRef),
    required: List(String),
    additional_properties: AdditionalProperties,
    min_properties: option.Option(Int),
    max_properties: option.Option(Int),
  )
  AllOfSchema(metadata: SchemaMetadata, schemas: List(SchemaRef))
  OneOfSchema(
    metadata: SchemaMetadata,
    schemas: List(SchemaRef),
    discriminator: option.Option(Discriminator),
  )
  AnyOfSchema(
    metadata: SchemaMetadata,
    schemas: List(SchemaRef),
    discriminator: option.Option(Discriminator),
  )
}

Constructors

A schema given inline or by $ref. Reference keeps the whole $ref string and its last segment as name, such as "Pet" for "#/components/schemas/Pet".

pub type SchemaRef {
  Inline(SchemaObject)
  Reference(ref: String, name: String)
}

Constructors

A security requirement.

pub type SecurityRequirement {
  SecurityRequirement(schemes: List(SecuritySchemeRef))
}

Constructors

Security scheme definition.

pub type SecurityScheme {
  ApiKeyScheme(name: String, in_: SecuritySchemeIn)
  HttpScheme(
    scheme: String,
    bearer_format: option.Option(String),
  )
  OAuth2Scheme(
    description: option.Option(String),
    flows: dict.Dict(String, OAuth2Flow),
  )
  OpenIdConnectScheme(
    open_id_connect_url: String,
    description: option.Option(String),
  )
  UnsupportedScheme(scheme_type: String)
}

Constructors

  • ApiKeyScheme(name: String, in_: SecuritySchemeIn)
  • HttpScheme(scheme: String, bearer_format: option.Option(String))
  • OAuth2Scheme(
      description: option.Option(String),
      flows: dict.Dict(String, OAuth2Flow),
    )
  • OpenIdConnectScheme(
      open_id_connect_url: String,
      description: option.Option(String),
    )
  • UnsupportedScheme(scheme_type: String)

    A scheme type oaspec cannot generate code for. Parsing produces it only for OAS 3.1 mutualTLS; other unknown types fail to parse, and oaspec.generate rejects this variant.

Location for apiKey security scheme.

pub type SecuritySchemeIn {
  SchemeInHeader
  SchemeInQuery
  SchemeInCookie
}

Constructors

  • SchemeInHeader
  • SchemeInQuery
  • SchemeInCookie

A single scheme reference within a security requirement (AND element).

pub type SecuritySchemeRef {
  SecuritySchemeRef(scheme_name: String, scopes: List(String))
}

Constructors

  • SecuritySchemeRef(scheme_name: String, scopes: List(String))

Server object.

pub type Server {
  Server(
    url: String,
    description: option.Option(String),
    variables: dict.Dict(String, ServerVariable),
  )
}

Constructors

A server variable for server URL template substitution.

pub type ServerVariable {
  ServerVariable(
    default: String,
    enum_values: List(String),
    description: option.Option(String),
  )
}

Constructors

  • ServerVariable(
      default: String,
      enum_values: List(String),
      description: option.Option(String),
    )

Whether a diagnostic stops generation (SeverityError) or is only reported (SeverityWarning).

pub type Severity {
  SeverityError
  SeverityWarning
}

Constructors

  • SeverityError
  • SeverityWarning

The line and column in the YAML document a diagnostic points at. JSON documents and checks that run after parsing give NoSourceLoc.

pub type SourceLoc {
  SourceLoc(line: Int, column: Int)
  NoSourceLoc
}

Constructors

  • SourceLoc(line: Int, column: Int)
  • NoSourceLoc

A tag for API documentation control.

pub type Tag {
  Tag(
    name: String,
    description: option.Option(String),
    external_docs: option.Option(ExternalDoc),
  )
}

Constructors

Which generated side a diagnostic applies to. Diagnostics for the side a config does not generate are dropped.

pub type Target {
  TargetBoth
  TargetClient
  TargetServer
}

Constructors

  • TargetBoth
  • TargetClient
  • TargetServer

Marks a document whose RefOr values may still be Ref.

pub type Unresolved {
  Unresolved
}

Constructors

  • Unresolved

Values

pub fn diagnostic_to_string(d: Diagnostic) -> String

Returns a one-line description of a diagnostic, with its phase, severity, pointer, location and hint.

Examples

openapi.Diagnostic(
  code: "missing_field",
  phase: openapi.PhaseParse,
  severity: openapi.SeverityError,
  target: openapi.TargetBoth,
  pointer: "info",
  source_loc: openapi.SourceLoc(line: 3, column: 3),
  message: "Missing required field: title",
  hint: None,
)
|> openapi.diagnostic_to_string
// -> "[Parse] Error at info: Missing required field: title (line 3, column 3)"
✨ Search Document