1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! 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#"
//! })
//! );
//! ```
pub use Error;
pub use ;
use Value;
/// Convert an OpenAPI 3.0 Schema Object to a JSON Schema draft-04 document.
///
/// The root of the returned value carries `$schema`. The input is not mutated.
/// The root must be an object or an array. An array passes through unchanged.
///
/// # Errors
///
/// Returns [`Error::InvalidType`] when strict mode is on and any node carries a
/// `type` value outside the draft-04 type set. Returns [`Error::InvalidInput`]
/// when the root is a scalar or null.
///
/// # Example
///
/// ```
/// use openapi_schema_to_json_schema::{from_schema, Options};
/// use serde_json::json;
///
/// let out = from_schema(json!({ "type": "integer", "format": "int32" }), &Options::new()).unwrap();
/// assert_eq!(out["minimum"], json!(-2147483648_i64));
/// assert_eq!(out["maximum"], json!(2147483647_i64));
/// ```
/// Convert an OpenAPI 3.0 Parameter Object or Response Object.
///
/// With a `schema` member the result is a single JSON Schema. With a `content`
/// member the result is a map keyed by MIME type, where each value is a JSON
/// Schema with its own `$schema`. The outer map has no `$schema`.
///
/// A truthy `description` on the parameter is copied onto each result.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] when strict mode is on and the parameter has
/// neither a `schema` nor a `content` member. Returns [`Error::InvalidType`]
/// under the same conditions as [`from_schema`].
///
/// # Example
///
/// ```
/// use openapi_schema_to_json_schema::{from_parameter, Options};
/// use serde_json::json;
///
/// let param = json!({
/// "name": "id",
/// "in": "query",
/// "schema": { "type": "string", "nullable": true }
/// });
/// let out = from_parameter(param, &Options::new()).unwrap();
/// assert_eq!(out["type"], json!(["string", "null"]));
/// ```