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: truebecomes"null"added totype, andnullappended to anenumwhen present.- OpenAPI-only keywords are stripped:
nullable,discriminator,readOnly,writeOnly,xml,externalDocs,example,deprecated. - Numeric formats (
int32,int64,float,double) becomeminimum/maximumbounds.bytebecomes a base64pattern.dateoptionally becomesdate-time. - Combiner and struct keywords recurse:
allOf,anyOf,oneOf,not,items,additionalProperties, andproperties. requireddrops names whose property was removed during conversion, and keeps names that have nopropertiesentry.- In strict mode, an invalid
typeraises 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. - Resolved
Options - 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§
- After
Transform - A hook that runs on each node after keyword processing.
- Before
Transform - A hook that runs on each node before keyword processing.
- Pattern
Properties Handler - A handler that rewrites a node after
x-patternPropertiesbecomespatternProperties. Its return value replaces the node.