openapi-deref 0.1.0

Lightweight OpenAPI/JSON Schema $ref resolver. Inline-expands all $ref pointers into a self-contained JSON value.
Documentation
  • Coverage
  • 100%
    25 out of 25 items documented6 out of 6 items with examples
  • Size
  • Source code size: 72.95 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 4.08 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 23s Average build duration of successful builds.
  • all releases: 23s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ynishi

openapi-deref

Lightweight OpenAPI / JSON Schema $ref resolver for Rust.

Recursively expands all $ref pointers in a serde_json::Value, producing a self-contained document with no unresolved references.

Features

  • Resolves internal $ref via JSON Pointer (RFC 6901)
  • OpenAPI 3.1 compatible: sibling keys alongside $ref are merged
  • Cycle detection with a visited-path set
  • Typed error model: fatal ResolveError vs non-fatal RefError
  • resolve_strict for zero-tolerance mode
  • Diagnostics: inspect cycles, missing refs, and external refs individually
  • Zero dependencies beyond serde_json and thiserror

Quick start

use serde_json::json;

let value = openapi_deref::resolve_strict(&json!({
    "components": { "schemas": { "Id": { "type": "integer" } } },
    "field": { "$ref": "#/components/schemas/Id" }
})).unwrap();

assert_eq!(value["field"]["type"], "integer");

API levels

Function Returns Use when
resolve_strict Result<Value, StrictResolveError> Any unresolved ref is unacceptable
resolve Result<ResolvedDoc, ResolveError> You need to inspect partial results or diagnostics
resolve_with_root Result<ResolvedDoc, ResolveError> The ref lookup root differs from the target value

Error handling

Non-fatal ref errors are collected in ResolvedDoc and can be inspected:

use serde_json::json;
use openapi_deref::resolve;

let spec = json!({
    "components": { "schemas": {
        "Node": { "properties": { "child": { "$ref": "#/components/schemas/Node" } } }
    }},
    "root": { "$ref": "#/components/schemas/Node" }
});

let doc = resolve(&spec).unwrap();
assert_eq!(doc.cycles().count(), 1);
assert_eq!(doc.missing_refs().count(), 0);
assert_eq!(doc.external_refs().count(), 0);

License

Licensed under either of

at your option.