Skip to main content

Crate openapi_schema_to_json_schema

Crate openapi_schema_to_json_schema 

Source
Expand description

Convert OpenAPI 3.0 schema objects to JSON Schema draft-04.

OpenAPI 3.0 describes schemas with an extended subset of JSON Schema that is not itself valid JSON Schema draft-04. This crate bridges the gap. It takes an OpenAPI 3.0 Schema Object as a serde_json::Value and returns a draft-04 document.

The conversion is pure and in memory. It performs no I/O, no network access, and no $ref resolution. References pass through untouched, so resolve them before calling if you need them inlined.

§What it does

  • nullable: true becomes "null" added to type, and null appended to an enum when present.
  • OpenAPI-only keywords are stripped: nullable, discriminator, readOnly, writeOnly, xml, externalDocs, example, deprecated.
  • Numeric formats (int32, int64, float, double) become minimum/maximum bounds. byte becomes a base64 pattern. date optionally becomes date-time.
  • Combiner and struct keywords recurse: allOf, anyOf, oneOf, not, items, additionalProperties, and properties.
  • required drops names whose property was removed during conversion, and keeps names that have no properties entry.
  • In strict mode, an invalid type raises an error.

The root of the result carries "$schema": "http://json-schema.org/draft-04/schema#". Nested schemas do not.

§Example

use openapi_schema_to_json_schema::{from_schema, Options};
use serde_json::json;

let input = json!({ "type": "string", "nullable": true });
let output = from_schema(input, &Options::new()).unwrap();
assert_eq!(
    output,
    json!({
        "type": ["string", "null"],
        "$schema": "http://json-schema.org/draft-04/schema#"
    })
);

Structs§

Options
Caller-facing options. Every field is optional. Unset fields take the defaults described on ResolvedOptions.
ResolvedOptions
Options with defaults applied and internal fields derived.

Enums§

Error
Errors raised during conversion.

Functions§

from_parameter
Convert an OpenAPI 3.0 Parameter Object or Response Object.
from_schema
Convert an OpenAPI 3.0 Schema Object to a JSON Schema draft-04 document.

Type Aliases§

AfterTransform
A hook that runs on each node after keyword processing.
BeforeTransform
A hook that runs on each node before keyword processing.
PatternPropertiesHandler
A handler that rewrites a node after x-patternProperties becomes patternProperties. Its return value replaces the node.