roas-codegen 0.2.0

Generate Rust and Go types from OpenAPI 2.0 / 3.0 / 3.1 / 3.2 descriptions
Documentation
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Where a description comes from, and the one type that holds it.

use crate::diagnostic::Diagnostic;
use crate::report;
use roas::{v2, v3_0, v3_1, v3_2};
use std::fmt;
use thiserror::Error;
use url::Url;

/// The bytes or value a description arrives as.
///
/// The variant records *where the numbers came from*, because exactness
/// cannot be reconstructed after the fact: only [`Input::Json`], which
/// this crate parses itself, can yield [`Fidelity::Exact`]. The YAML
/// reader may round through `f64`, and a ready-made `Value` has unknown
/// provenance whatever its caller believes about it. There is
/// deliberately no way to assert exactness on a `Value`.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Input {
    /// JSON text. The only way to obtain exact numbers.
    Json(Vec<u8>),
    /// YAML text, read with `serde-saphyr`. An integer within the 64-bit
    /// range is kept exactly; a fraction, an exponent, or an untagged
    /// decimal integer past `u64::MAX` or below `i64::MIN` is read through
    /// `f64`.
    Yaml(Vec<u8>),
    /// An already-parsed value of unknown provenance.
    Value(serde_json::Value),
}

/// How faithfully the numbers in a document reflect what was written.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Fidelity {
    /// Every number is the literal that was written. Requires the
    /// `exact-numbers` feature and JSON input; without the feature even
    /// JSON input is [`Fidelity::F64`], since `serde_json` rounds.
    Exact,
    /// Numbers went through `f64` on the way in.
    F64,
    /// The provenance is not known.
    Unknown,
}

/// The OpenAPI version a description was written in.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum SourceVersion {
    V2,
    V3_0,
    V3_1,
    V3_2,
}

impl SourceVersion {
    fn detect(raw: &serde_json::Value) -> Result<Self, SourceError> {
        let Some(object) = raw.as_object() else {
            return Err(SourceError::NotAnObject);
        };
        if let Some(swagger) = object.get("swagger") {
            return match swagger.as_str() {
                Some(v) if v.starts_with("2.0") => Ok(SourceVersion::V2),
                _ => Err(SourceError::UnknownVersion {
                    field: "swagger",
                    value: swagger.to_string(),
                }),
            };
        }
        if let Some(openapi) = object.get("openapi") {
            return match openapi.as_str() {
                Some(v) if v.starts_with("3.0.") || v == "3.0" => Ok(SourceVersion::V3_0),
                Some(v) if v.starts_with("3.1.") || v == "3.1" => Ok(SourceVersion::V3_1),
                Some(v) if v.starts_with("3.2.") || v == "3.2" => Ok(SourceVersion::V3_2),
                _ => Err(SourceError::UnknownVersion {
                    field: "openapi",
                    value: openapi.to_string(),
                }),
            };
        }
        Err(SourceError::NoVersion)
    }
}

impl fmt::Display for SourceVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            SourceVersion::V2 => "OpenAPI 2.0",
            SourceVersion::V3_0 => "OpenAPI 3.0",
            SourceVersion::V3_1 => "OpenAPI 3.1",
            SourceVersion::V3_2 => "OpenAPI 3.2",
        })
    }
}

/// Why a description could not become a [`SourceDocument`].
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum SourceError {
    #[error("the input is not valid JSON")]
    Json(#[source] serde_json::Error),
    /// The YAML reader rejected the input: a syntax error, a duplicate
    /// mapping key, or a document past the reader's size and alias budget.
    #[error("the input is not valid YAML")]
    Yaml(#[source] serde_saphyr::Error),
    #[error("a description must be a JSON object")]
    NotAnObject,
    #[error("neither `swagger` nor `openapi` is present, so the version is unknown")]
    NoVersion,
    #[error("`{field}: {value}` is not a supported version")]
    UnknownVersion { field: &'static str, value: String },
    #[error("the input is not a valid {version} description")]
    Parse {
        version: SourceVersion,
        #[source]
        source: serde_json::Error,
    },
    /// The description parsed, but `roas` could not convert it to OpenAPI 3.2: a gap in the
    /// conversion rather than in the description.
    #[error("the {version} description could not be converted to OpenAPI 3.2")]
    Convert {
        version: SourceVersion,
        #[source]
        source: roas::conversion::ConversionError,
    },
}

/// A description, parsed and normalized to OpenAPI 3.2, together with
/// the source it came from.
///
/// The raw and typed views are the same document by construction: the
/// fields are private and [`SourceDocument::parse`] is the only way to
/// make one, so a caller cannot pair the raw JSON of one document with
/// the typed model of another. The raw view keeps what the typed model
/// cannot express — whether `type` was written at all, the exact text a
/// diagnostic should quote — and every diagnostic pointer indexes it,
/// in the version it was written in.
#[derive(Debug, Clone)]
pub struct SourceDocument {
    uri: Url,
    version: SourceVersion,
    fidelity: Fidelity,
    raw: serde_json::Value,
    spec: v3_2::spec::Spec,
    normalization: Vec<Diagnostic>,
}

impl SourceDocument {
    /// Detect the version, parse the typed model at that version, record
    /// what upconverting to 3.2 loses, and upconvert.
    ///
    /// `uri` identifies the document in every [`SchemaId`](crate::SchemaId)
    /// it produces. Input with no natural URI takes a synthetic one, by
    /// convention `stdin:///`.
    pub fn parse(input: Input, uri: Url) -> Result<SourceDocument, SourceError> {
        let (raw, fidelity) = match input {
            Input::Json(bytes) => {
                let value = serde_json::from_slice(&bytes).map_err(SourceError::Json)?;
                let fidelity = if cfg!(feature = "exact-numbers") {
                    Fidelity::Exact
                } else {
                    Fidelity::F64
                };
                (value, fidelity)
            }
            Input::Yaml(bytes) => (
                parse_yaml(&bytes).map_err(SourceError::Yaml)?,
                Fidelity::F64,
            ),
            Input::Value(value) => (value, Fidelity::Unknown),
        };
        let version = SourceVersion::detect(&raw)?;
        let parse = |source| SourceError::Parse { version, source };
        let convert = |source| SourceError::Convert { version, source };
        let mut normalization = Vec::new();
        let spec = match version {
            SourceVersion::V2 => {
                let spec: v2::spec::Spec = serde_json::from_value(raw.clone()).map_err(parse)?;
                report::v2_losses(&raw, &uri, &mut normalization);
                let v30 = v3_0::spec::Spec::try_from(spec).map_err(convert)?;
                let v31 = v3_1::spec::Spec::try_from(v30).map_err(convert)?;
                v3_2::spec::Spec::try_from(v31).map_err(convert)?
            }
            SourceVersion::V3_0 => {
                let spec: v3_0::spec::Spec = serde_json::from_value(raw.clone()).map_err(parse)?;
                let v31 = v3_1::spec::Spec::try_from(spec).map_err(convert)?;
                v3_2::spec::Spec::try_from(v31).map_err(convert)?
            }
            SourceVersion::V3_1 => {
                let spec: v3_1::spec::Spec = serde_json::from_value(raw.clone()).map_err(parse)?;
                v3_2::spec::Spec::try_from(spec).map_err(convert)?
            }
            SourceVersion::V3_2 => serde_json::from_value(raw.clone()).map_err(parse)?,
        };
        Ok(SourceDocument {
            uri,
            version,
            fidelity,
            raw,
            spec,
            normalization,
        })
    }

    /// The document's identity.
    pub fn uri(&self) -> &Url {
        &self.uri
    }

    /// The version the description was written in — not the version
    /// [`spec`](Self::spec) is at, which is always 3.2.
    pub fn version(&self) -> SourceVersion {
        self.version
    }

    /// How faithfully the numbers reflect what was written.
    pub fn fidelity(&self) -> Fidelity {
        self.fidelity
    }

    /// The description exactly as given, before any conversion. Every
    /// diagnostic pointer indexes this value.
    pub fn raw(&self) -> &serde_json::Value {
        &self.raw
    }

    /// The description normalized to OpenAPI 3.2.
    pub fn spec(&self) -> &v3_2::spec::Spec {
        &self.spec
    }

    /// What normalizing to 3.2 lost. Recorded by the source-version
    /// adapter before the lossy conversion runs, since a 2.0
    /// `collectionFormat: tsv` that 3.0 drops cannot be recovered
    /// afterwards.
    pub fn normalization(&self) -> &[Diagnostic] {
        &self.normalization
    }
}

/// Read YAML with the options every roas crate uses, so a document means
/// the same thing here as in the fetchers and the CLI.
///
/// - YAML 1.2 booleans only: `yes` / `on` / `no` / `off` stay strings.
/// - Integer spellings `017` and `1_000` stay strings rather than becoming numbers.
/// - `!!binary` scalars are read as their base64 text, not decoded.
/// - `.inf` / `.nan` pass through (a `serde_json::Value` holds them as `null`).
/// - `<<: *anchor` merge keys are expanded.
/// - A duplicate mapping key is an error rather than a silent last-wins.
///
/// A [`serde_saphyr::Budget`] stays on, raised so large specs fit: it rejects
/// alias bombs, nesting past 64 levels, documents past 10M nodes or
/// 64 MiB of scalar text, and an anchored node past 1M events.
fn parse_yaml(bytes: &[u8]) -> Result<serde_json::Value, serde_saphyr::Error> {
    let options = serde_saphyr::options! {
        // Same options in every roas crate; keep the copies in step
        // (`grep -rn 'strict_booleans: true' crates`). The budget is raised
        // from the defaults (250k nodes, 1M events, 50k aliases, 50k anchors,
        // 10k merge keys), which reject ordinary large or generated specs. Depth stays at 64: deeper nesting overflows a 2 MiB
        // thread stack in debug builds. The alias-to-anchor ratio check is off:
        // it rejects any document reusing one anchor 100+ times, a common
        // pattern in specs. Alias replay is raised to 10M events so a large
        // schema can be aliased thousands of times; the 1M cap on events
        // recorded under anchors still stops an alias bomb, and also caps
        // how large one anchored node can be. `!!binary` is
        // read as its text, as before, not decoded.
        budget: serde_saphyr::budget! {
            max_nodes: 10_000_000,
            max_events: 25_000_000,
            max_aliases: 1_000_000,
            max_anchors: 1_000_000,
            max_merge_keys: 1_000_000,
            enforce_alias_anchor_ratio: false,
        },
        alias_limits: serde_saphyr::alias_limits! {
            max_total_replayed_events: 10_000_000,
        },
        strict_booleans: true,
        ignore_binary_tag_for_string: true,
        leading_zero_numbers_as_strings: true,
        underscored_numbers_as_strings: true,
        non_finite_float_policy: serde_saphyr::NonFiniteFloatPolicy::PassThrough,
        merge_keys: serde_saphyr::MergeKeyPolicy::Merge,
        duplicate_keys: serde_saphyr::DuplicateKeyPolicy::Error,
    };
    serde_saphyr::from_slice_with_options::<crate::yaml_value::JsonValue>(bytes, options)
        .map(crate::yaml_value::JsonValue::into_inner)
}

#[cfg(test)]
mod yaml_tests {
    use super::*;
    use serde_json::{Value, json};

    fn parse(yaml: &str) -> Result<Value, String> {
        parse_yaml(yaml.as_bytes()).map_err(|e| e.to_string())
    }

    /// Each scalar whose meaning depends on the YAML options, next to what
    /// it must read as.
    #[test]
    fn yaml_scalars_read_as_yaml_1_2() {
        for (scalar, expected) in [
            ("yes", json!("yes")),
            ("on", json!("on")),
            ("no", json!("no")),
            ("off", json!("off")),
            ("true", json!(true)),
            ("False", json!(false)),
            ("017", json!("017")),
            ("1_000", json!("1_000")),
            ("17", json!(17)),
            ("0x11", json!(17)),
            ("1.5", json!(1.5)),
            (".inf", json!(null)),
            ("3.1.0", json!("3.1.0")),
            ("!!binary SGVsbG8=", json!("SGVsbG8=")),
            ("!!binary /wAB", json!("/wAB")),
            ("1e400", json!(null)),
            ("!include other.yaml", json!("other.yaml")),
        ] {
            assert_eq!(
                parse(&format!("v: {scalar}\n")).unwrap(),
                json!({ "v": expected }),
                "{scalar}",
            );
        }
    }

    #[test]
    fn yaml_merge_keys_expand_through_an_anchor() {
        let yaml = "base: &base { type: string, minLength: 1 }\n\
                    name:\n  <<: *base\n  minLength: 3\n";
        assert_eq!(
            parse(yaml).unwrap(),
            json!({
                "base": { "type": "string", "minLength": 1 },
                "name": { "type": "string", "minLength": 3 },
            }),
        );
    }

    #[test]
    fn yaml_duplicate_key_is_an_error() {
        let err = parse("a: 1\na: 2\n").unwrap_err();
        assert!(err.contains("duplicate mapping key"), "{err}");
    }

    /// `200` and `"200"` are different YAML keys but the same JSON one.
    /// Through a `<<` merge the inherited entry would otherwise overwrite the
    /// explicit one, in either direction.
    #[test]
    fn yaml_keys_colliding_as_json_are_an_error() {
        for yaml in [
            "base: &b { 200: { description: inherited } }\n\
             responses:\n  <<: *b\n  \"200\": { description: explicit }\n",
            "base: &b { \"200\": { description: inherited } }\n\
             responses:\n  <<: *b\n  200: { description: explicit }\n",
            "responses:\n  200: a\n  \"200\": b\n",
        ] {
            let err = parse(yaml).unwrap_err();
            assert!(err.contains("appears twice once read as JSON"), "{err}");
        }
    }

    #[test]
    fn yaml_merge_keeps_the_explicit_entry_when_keys_match() {
        let yaml = "base: &b { 200: { description: inherited }, 404: { description: nf } }\n\
                    responses:\n  <<: *b\n  200: { description: explicit }\n";
        assert_eq!(
            parse(yaml).unwrap()["responses"],
            json!({ "200": { "description": "explicit" }, "404": { "description": "nf" } }),
        );
    }

    /// Keys are read as strings, so a number-looking key keeps its spelling
    /// and two spellings of one number are two keys.
    #[test]
    fn yaml_keys_keep_their_spelling() {
        assert_eq!(
            parse("r: { 1.10: a, 1.1: b, 0x10: c, 1e3: d, True: e, 017: f }\n").unwrap(),
            json!({ "r": { "1.10": "a", "1.1": "b", "0x10": "c", "1e3": "d", "True": "e", "017": "f" } }),
        );
    }

    #[test]
    fn yaml_null_and_collection_keys_are_an_error() {
        for yaml in [
            "r: { null: a }\n",
            "r: { ~: a }\n",
            "r:\n  ? [1, 2]\n  : a\n",
        ] {
            parse(yaml).unwrap_err();
        }
    }

    /// The number map YAML output carries under `arbitrary_precision` reads
    /// back the way `serde_json` reads it, with the feature on or off.
    #[test]
    fn yaml_arbitrary_precision_number_map_matches_serde_json() {
        let json = r#"{"n":{"$serde_json::private::Number":"12.50"}}"#;
        let expected: Value = serde_json::from_str(json).unwrap();
        assert_eq!(
            parse("n: { \"$serde_json::private::Number\": \"12.50\" }\n").unwrap(),
            expected
        );
    }

    #[test]
    fn yaml_alias_bomb_is_rejected_by_the_budget() {
        // Billion laughs: each level aliases the previous one ten times, so
        // full expansion is 10^7 scalars. The anchor-replay limits stop it.
        let mut yaml = String::from("l0: &l0 [lol, lol, lol, lol, lol, lol, lol, lol, lol, lol]\n");
        for i in 1..7 {
            let prev = format!("*l{}", i - 1);
            yaml.push_str(&format!("l{i}: &l{i} [{}]\n", vec![prev; 10].join(", ")));
        }
        let err = parse(&yaml).unwrap_err();
        assert!(err.contains("budget breached"), "{err}");
    }

    /// One shared anchor reused many times, as specs do for common
    /// responses, is not a bomb.
    #[test]
    fn yaml_anchor_reused_many_times_is_accepted() {
        // 40 properties aliased 6,000 times replays over a million events.
        let props: String = (0..40)
            .map(|i| format!("    p{i}: {{ type: string }}\n"))
            .collect();
        let mut yaml = format!("schema: &s\n  type: object\n  properties:\n{props}responses:\n");
        for i in 0..6_000 {
            yaml.push_str(&format!("  r{i}: *s\n"));
        }
        let value = parse(&yaml).unwrap();
        assert_eq!(
            value["responses"]["r5999"]["properties"]["p39"]["type"],
            "string"
        );
    }

    /// Past the library's default caps of 50k aliases and 50k anchors,
    /// which generated specs (one anchor per shared object) can reach.
    #[test]
    fn yaml_many_aliases_and_anchors_fit_the_budget() {
        let mut yaml = String::from("shared: &s x\n");
        for i in 0..60_000 {
            yaml.push_str(&format!("a{i}: *s\nb{i}: &b{i} y\n"));
        }
        let value = parse(&yaml).unwrap();
        assert_eq!(value["a59999"], "x");
        assert_eq!(value["b59999"], "y");
    }

    /// Past the library's default cap of 10k merge keys, which only applies
    /// because merge keys are expanded.
    #[test]
    fn yaml_many_merge_keys_fit_the_budget() {
        let mut yaml = String::from("base: &b { type: string }\n");
        for i in 0..10_001 {
            yaml.push_str(&format!("s{i}: {{ <<: *b, minLength: 1 }}\n"));
        }
        let value = parse(&yaml).unwrap();
        assert_eq!(value["s10000"]["type"], "string");
    }

    #[test]
    fn yaml_nesting_is_capped_at_64_levels() {
        let nested = |depth: usize| format!("{}1{}", "[".repeat(depth), "]".repeat(depth));
        parse(&nested(64)).unwrap();
        let err = parse(&nested(65)).unwrap_err();
        assert!(err.contains("budget breached"), "{err}");
    }

    /// Past the library's default budget of 250k nodes, which rejects
    /// ordinary large specs.
    #[test]
    fn yaml_large_document_fits_the_budget() {
        let yaml: String = (0..60_000).map(|i| format!("k{i}: [a, b, c]\n")).collect();
        let value = parse(&yaml).unwrap();
        assert_eq!(value.as_object().unwrap().len(), 60_000);
    }
}