openapi-interfaces 0.4.0

Generate OpenAPI schemas for related GET, POST, PUT and JSON Merge Patch types
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
//! OpenAPI schemas.
//!
//! The types in this file are based on [JSON Schema Specification Draft
//! 2020-12](https://tools.ietf.org/html/draft-bhutton-json-schema-00#section-4.2.1).

use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet};

use crate::openapi::serde_helpers::deserialize_enum_helper;

use super::{
    ref_or::{ExpectedWhenParsing, RefOr},
    set_or_scalar, Scope, Transpile,
};

/// Interface for schema types that might be able to match against `null`.
pub trait Nullable: Sized {
    /// Construct a schema which matches only `null`.
    ///
    /// Include documentation fields included that makes the auto-generated
    /// documentation look nice when used in a `MergePatch`.
    fn new_schema_matching_only_null_for_merge_patch() -> Self;

    /// Construct a version of this schema that has documentation suitable for
    /// use inside `oneOf` inside a `MergePatch` type, and return the original
    /// `description` if any.
    fn new_schema_with_merge_patch_documentation(&self) -> (Self, Option<String>);

    /// Construct a version of this schema that allows `null`, as well as any
    /// other values it might have allowed before.
    fn new_schema_matching_current_or_null_for_merge_patch(
        &self,
        scope: &Scope,
    ) -> Self;

    /// (Normally internal.) Does this schema allow a null value without
    /// resolving `$ref` or `$interface` links?
    ///
    /// (Most OpenAPI-based code generators rely on not resolving `$ref`, and
    /// `$interface` compiles to `$ref`.)
    fn allows_local_null(&self) -> bool;
}

/// Different possibilities for a schema.
pub type Schema = RefOr<BasicSchema>;

impl Schema {
    /// Does this schema match only an empty object?
    pub fn matches_only_empty_object(&self) -> bool {
        match self {
            RefOr::Ref(_) | RefOr::InterfaceRef(_) => false,
            RefOr::Value(s) => s.matches_only_empty_object(),
        }
    }
}

impl Nullable for Schema {
    fn new_schema_matching_only_null_for_merge_patch() -> Self {
        RefOr::Value(BasicSchema::new_schema_matching_only_null_for_merge_patch())
    }

    fn new_schema_matching_current_or_null_for_merge_patch(
        &self,
        scope: &Scope,
    ) -> Schema {
        match self {
            RefOr::Ref(_) | RefOr::InterfaceRef(_)
                if scope.use_nullable_for_merge_patch =>
            {
                RefOr::Value(BasicSchema::OneOf(
                    OneOf::new_nullable_schema_for_merge_patch(self),
                ))
            }
            RefOr::Ref(_) | RefOr::InterfaceRef(_) => RefOr::Value(
                BasicSchema::OneOf(OneOf::new_schema_or_null_for_merge_patch(self)),
            ),
            RefOr::Value(val) => RefOr::Value(
                val.new_schema_matching_current_or_null_for_merge_patch(scope),
            ),
        }
    }

    fn new_schema_with_merge_patch_documentation(&self) -> (Self, Option<String>) {
        match self {
            RefOr::Ref(_) | RefOr::InterfaceRef(_) => (self.clone(), None),
            RefOr::Value(val) => {
                let (schema, description) =
                    val.new_schema_with_merge_patch_documentation();
                (RefOr::Value(schema), description)
            }
        }
    }

    fn allows_local_null(&self) -> bool {
        match self {
            RefOr::Ref(_) | RefOr::InterfaceRef(_) => false,
            RefOr::Value(value) => value.allows_local_null(),
        }
    }
}

#[test]
fn allowing_null_turns_refs_into_oneof() {
    use super::ref_or::Ref;

    let schema =
        RefOr::<BasicSchema>::Ref(Ref::new("#/components/schemas/widget", None));
    let scope = Scope::default();
    assert_eq!(
        schema.new_schema_matching_current_or_null_for_merge_patch(&scope),
        RefOr::Value(BasicSchema::OneOf(OneOf {
            r#type: None,
            schemas: vec![
                schema,
                Schema::new_schema_matching_only_null_for_merge_patch()
            ],
            description: None,
            title: None,
            discriminator: None,
            nullable: None,
            unknown_fields: Default::default(),
        }))
    )
}

#[test]
fn deserializes_array_schema() {
    let yaml = r#"
type: array
items:
  $interface: "Dataset"
"#;
    serde_yaml::from_str::<Schema>(yaml).unwrap();
}

/// Different possibilities for a schema.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(untagged)]
pub enum BasicSchema {
    /// A value must match all of the specified schemas.
    AllOf(AllOf),
    /// A value must match at least one of the specified schemas.
    OneOf(OneOf),
    /// A basic schema containing `type` and additional fields.
    Primitive(Box<PrimitiveSchema>),
}

impl BasicSchema {
    /// Does this schema match only an empty object?
    fn matches_only_empty_object(&self) -> bool {
        match self {
            BasicSchema::AllOf(all_of) => {
                all_of.schemas.iter().any(|s| s.matches_only_empty_object())
            }
            BasicSchema::OneOf(one_of) => {
                one_of.schemas.iter().all(|s| s.matches_only_empty_object())
            }
            BasicSchema::Primitive(s) => s.matches_only_empty_object(),
        }
    }
}

impl<'de> Deserialize<'de> for BasicSchema {
    // Manually deserialize for slightly better error messages. See
    // https://github.com/faradayio/openapi-interfaces/issues/28 for the whole
    // horrifying story.
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de::Error;
        use serde_yaml::{Mapping, Value};

        // Parse it as raw YAML.
        let yaml = Mapping::deserialize(deserializer)?;

        // Helper to construct YAML hash keys.
        let yaml_str = |s| Value::String(String::from(s));

        // Look for `$includes`.
        if yaml.contains_key(&yaml_str("allOf")) {
            Ok(BasicSchema::AllOf(deserialize_enum_helper::<D, _>(
                "allOf schema",
                Value::Mapping(yaml),
            )?))
        } else if yaml.contains_key(&yaml_str("oneOf")) {
            Ok(BasicSchema::OneOf(deserialize_enum_helper::<D, _>(
                "oneOf schema",
                Value::Mapping(yaml),
            )?))
        } else if yaml.contains_key(&yaml_str("type")) {
            Ok(BasicSchema::Primitive(deserialize_enum_helper::<D, _>(
                "schema",
                Value::Mapping(yaml),
            )?))
        } else {
            Err(D::Error::custom(format!(
                "one of allOf, oneOf, or type in:\n{}",
                serde_yaml::to_string(&yaml).expect("error serializing YAML")
            )))
        }
    }
}

impl ExpectedWhenParsing for BasicSchema {
    fn expected_when_parsing() -> &'static str {
        "a schema with one of allOf, oneOf, or type"
    }
}

impl Nullable for BasicSchema {
    fn new_schema_matching_only_null_for_merge_patch() -> Self {
        BasicSchema::Primitive(Box::new(PrimitiveSchema::null_for_merge_patch()))
    }

    fn new_schema_with_merge_patch_documentation(&self) -> (Self, Option<String>) {
        match self {
            BasicSchema::AllOf(_) | BasicSchema::OneOf(_) => (self.clone(), None),
            BasicSchema::Primitive(base) => {
                let (base, description) = base.with_merge_patch_documentation();
                (BasicSchema::Primitive(Box::new(base)), description)
            }
        }
    }

    fn allows_local_null(&self) -> bool {
        match self {
            BasicSchema::AllOf(all_of) => {
                all_of.schemas.iter().all(|s| s.allows_local_null())
            }
            BasicSchema::OneOf(one_of) => {
                one_of.schemas.iter().any(|s| s.allows_local_null())
            }
            BasicSchema::Primitive(base) => base.types.contains(&Type::Null),
        }
    }

    fn new_schema_matching_current_or_null_for_merge_patch(
        &self,
        scope: &Scope,
    ) -> BasicSchema {
        if scope.use_nullable_for_merge_patch {
            match self {
                // We have a type that supports `nullable`, so do that.
                BasicSchema::Primitive(base) => {
                    let mut base = base.clone();
                    base.nullable = Some(true);
                    BasicSchema::Primitive(base)
                }

                // We already have a `oneOf`, which also supports nullable.
                BasicSchema::OneOf(one_of) => {
                    let mut base = one_of.clone();
                    base.nullable = Some(true);
                    BasicSchema::OneOf(base)
                }

                // We already have an `allOf`, which also supports nullable.
                BasicSchema::AllOf(all_of) => {
                    let mut base = all_of.clone();
                    base.nullable = Some(true);
                    BasicSchema::AllOf(base)
                }
            }
        } else {
            match self {
                // We already allow `null` (without following refs), so do nothing.
                schema if schema.allows_local_null() => schema.to_owned(),

                // We have a `BaseSchema`, so we can just add `null` to our existing
                // `type` list.
                //
                // However, `openapi-typescript` (which we care about) does not
                // currently support a list for `types`, so we're careful not to
                // introduce an extra element if we have exactly one.
                BasicSchema::Primitive(base) if base.types.len() != 1 => {
                    let mut base = base.as_ref().to_owned();
                    base.types.insert(Type::Null);
                    BasicSchema::Primitive(Box::new(base))
                }

                // We have a `OneOf` schema, so just add `null` **at the end**.
                BasicSchema::OneOf(one_of) => {
                    let mut one_of = one_of.to_owned();
                    one_of
                        .schemas
                        .push(Schema::new_schema_matching_only_null_for_merge_patch());
                    BasicSchema::OneOf(one_of)
                }

                // We have some other schema type, so we'll need to create a `OneOf` node.
                _ => BasicSchema::OneOf(OneOf::new_schema_or_null_for_merge_patch(
                    &RefOr::Value(self.to_owned()),
                )),
            }
        }
    }
}

impl Transpile for BasicSchema {
    type Output = Self;

    fn transpile(&self, scope: &Scope) -> anyhow::Result<Self::Output> {
        match self {
            BasicSchema::AllOf(all_of) => {
                Ok(BasicSchema::AllOf(all_of.transpile(scope)?))
            }
            BasicSchema::OneOf(one_of) => {
                Ok(BasicSchema::OneOf(one_of.transpile(scope)?))
            }
            BasicSchema::Primitive(schema) => {
                Ok(BasicSchema::Primitive(Box::new(schema.transpile(scope)?)))
            }
        }
    }
}

/// An `allOf` schema.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct AllOf {
    /// Our child schemas.
    #[serde(rename = "allOf")]
    schemas: Vec<Schema>,

    /// Is this field nullable? Used for the OpenAPI 3.0.X spec.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub nullable: Option<bool>,

    /// YAML fields we want to pass through blindly.
    #[serde(flatten)]
    unknown_fields: BTreeMap<String, Value>,
}

impl Transpile for AllOf {
    type Output = Self;

    fn transpile(&self, scope: &Scope) -> anyhow::Result<Self::Output> {
        Ok(Self {
            schemas: self.schemas.transpile(scope)?,
            nullable: self.nullable,
            unknown_fields: self.unknown_fields.clone(),
        })
    }
}

/// A `oneOf` schema.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct OneOf {
    /// If `discriminator.is_some()`, then this should always be
    /// `Some(Type::Object)`, because `oneOf` with a `discriminator` only works
    /// for object types. OpenAPI 3.1 allows us to omit `type: object`, but
    /// Readme.com requires it.
    #[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
    pub r#type: Option<Type>,

    /// Our child schemas.
    #[serde(rename = "oneOf")]
    pub schemas: Vec<Schema>,

    /// Optional description of the schema.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// An optional human-readable title. Used in documentation
    /// for cases where the resource name, which is generally used
    /// by default, is not desired.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    /// How to differentiate between our child schemas.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub discriminator: Option<Discriminator>,

    /// Is this field nullable? Used for the OpenAPI 3.0.X spec.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub nullable: Option<bool>,

    /// YAML fields we want to pass through blindly.
    #[serde(flatten)]
    pub unknown_fields: BTreeMap<String, Value>,
}

impl OneOf {
    /// Create a `oneOf` schema allowing either `schema` or a `null` value, and
    /// set up the `description` and `title` fields on everything in a way that
    /// looks good in a merge patch type.
    fn new_schema_or_null_for_merge_patch(schema: &Schema) -> OneOf {
        let (schema, description) = schema.new_schema_with_merge_patch_documentation();
        let schemas = vec![
            schema,
            Schema::new_schema_matching_only_null_for_merge_patch(),
        ];
        OneOf {
            r#type: None,
            schemas,
            description,
            title: None,
            discriminator: None,
            nullable: None,
            unknown_fields: Default::default(),
        }
    }

    /// Create a `oneOf` schema with the provided schema, setting the nullable field on the
    /// returned `oneOf`.
    fn new_nullable_schema_for_merge_patch(schema: &Schema) -> OneOf {
        let (schema, description) = schema.new_schema_with_merge_patch_documentation();
        let schemas = vec![schema];
        OneOf {
            r#type: None,
            schemas,
            description,
            title: None,
            discriminator: None,
            nullable: Some(true),
            unknown_fields: Default::default(),
        }
    }
}

impl Transpile for OneOf {
    type Output = Self;

    fn transpile(&self, scope: &Scope) -> anyhow::Result<Self::Output> {
        Ok(Self {
            r#type: self.r#type.clone(),
            schemas: self.schemas.transpile(scope)?,
            description: self.description.clone(),
            title: self.title.clone(),
            discriminator: self.discriminator.clone(),
            nullable: self.nullable,
            unknown_fields: self.unknown_fields.clone(),
        })
    }
}

/// Information about the discriminator for a `OneOf`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Discriminator {
    /// The property name that distinguishes the types.
    pub property_name: String,

    /// If the values in the field specified by `property_name` do not match the
    /// names of the schemas, you can override them using `mapping`.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub mapping: BTreeMap<String, String>,

    /// YAML fields we want to pass through blindly.
    #[serde(flatten)]
    pub unknown_fields: BTreeMap<String, Value>,
}

/// A basic JSON Schema fragment.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PrimitiveSchema {
    /// A set of value types which this schema will match.
    #[serde(rename = "type", with = "set_or_scalar")]
    pub types: BTreeSet<Type>,

    /// For `Type::Object`, a list of properties which must always be present.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub required: Vec<String>,

    /// For `Type::Object`, a list of property schemas.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub properties: BTreeMap<String, Schema>,

    /// A schema describing any additional properties not in `properties`.
    #[serde(default, skip_serializing_if = "AdditionalProperties::is_default")]
    pub additional_properties: AdditionalProperties,

    /// Array item type.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub items: Option<Schema>,

    /// Older OpenAPI way of specifying nullable fields.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub nullable: Option<bool>,

    /// A description of this type.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// A title, typically used to label the choices in a `oneOf`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    /// Constant data value which must always appear.
    ///
    /// The `r#` allows us to use the reserved word `const` as a regular
    /// identifier.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub r#const: Option<Value>,

    /// Example data for this type.
    ///
    /// TODO: We'll need multiple versions for different variants, sadly.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub example: Option<Value>,

    /// YAML fields we want to pass through blindly.
    #[serde(flatten)]
    pub unknown_fields: BTreeMap<String, Value>,
}

impl PrimitiveSchema {
    /// Does this schema match only an empty object?
    fn matches_only_empty_object(&self) -> bool {
        self.types.contains(&Type::Object)
            && self.types.len() == 1
            && self.properties.is_empty()
            && self.additional_properties == AdditionalProperties::Bool(false)
    }

    /// Construct a `BaseSchema` that matches `null`.
    fn null() -> PrimitiveSchema {
        let mut types = BTreeSet::new();
        types.insert(Type::Null);
        PrimitiveSchema {
            types,
            required: Default::default(),
            properties: Default::default(),
            additional_properties: Default::default(),
            items: Default::default(),
            nullable: None,
            description: Default::default(),
            title: Default::default(),
            r#const: Default::default(),
            example: Default::default(),
            unknown_fields: Default::default(),
        }
    }

    /// Create a version of the schema returned by [`Self::null()`], but with
    /// special `title` and `description` fields for use when we're injecting
    /// `null` support into `MergePatch` types.
    fn null_for_merge_patch() -> Self {
        let mut null_schema = Self::null();
        null_schema.title = Some("Clear".to_owned());
        null_schema.description =
            Some("Pass `null` to clear this field's existing value.".to_owned());
        null_schema
    }

    /// Construct a version of this schema for use in a `oneOf` variant inside
    /// of a `MergePatch`. This involves adding some fields that look nice in
    /// the generated API docs, and changing the description.
    ///
    /// We return the old description.
    fn with_merge_patch_documentation(&self) -> (Self, Option<String>) {
        // "The name of this widget.",
        // "Pass this value to overwrite the existing value.",
        // title: None,
        // title: Some("Overwrite"
        let mut merge_patch_schema = self.clone();
        merge_patch_schema.title = Some("Overwrite".to_owned());
        merge_patch_schema.description =
            Some("Pass this value to overwrite the existing value.".to_owned());
        (merge_patch_schema, self.description.clone())
    }
}

impl Transpile for PrimitiveSchema {
    type Output = Self;

    fn transpile(&self, scope: &Scope) -> anyhow::Result<Self::Output> {
        // Fix old-style nullability.
        let mut types = self.types.clone();
        if self.nullable == Some(true) {
            types.insert(Type::Null);
        }

        Ok(Self {
            types,
            required: self.required.clone(),
            properties: self.properties.transpile(scope)?,
            additional_properties: self.additional_properties.transpile(scope)?,
            items: self.items.transpile(scope)?,
            nullable: None,
            description: self.description.clone(),
            title: self.title.clone(),
            r#const: self.r#const.clone(),
            example: self.example.clone(),
            unknown_fields: self.unknown_fields.clone(),
        })
    }
}

/// Primitive JSON types. These serialize as lowercase strings, as per the JSON
/// Schema conventions.
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "lowercase")]
#[allow(clippy::missing_docs_in_private_items)]
pub enum Type {
    String,
    Number,
    Integer,
    Object,
    Array,
    Boolean,
    Null,
}

impl ExpectedWhenParsing for Type {
    fn expected_when_parsing() -> &'static str {
        "one of `string`, `number`, `integer`, `object`, `array`, `boolean` or `\"null\"`"
    }
}

/// An `additionalProperties` value.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(untagged)]
pub enum AdditionalProperties {
    /// `true` (allowing any property) or `false` (allowing none).
    Bool(bool),
    /// All unknown property values must match the specified schema.
    Schema(Schema),
}

impl AdditionalProperties {
    /// Is this the default value for `additionalProperties`?
    fn is_default(&self) -> bool {
        matches!(self, &AdditionalProperties::Bool(true))
    }
}

impl Default for AdditionalProperties {
    fn default() -> Self {
        AdditionalProperties::Bool(true)
    }
}

impl<'de> Deserialize<'de> for AdditionalProperties {
    // Manually deserialize for slightly better error messages. See
    // https://github.com/faradayio/openapi-interfaces/issues/28 for the whole
    // horrifying story.
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde_yaml::Value;

        // Parse it as raw YAML.
        let yaml = Value::deserialize(deserializer)?;

        // If this is a vec, handle it as such.
        if let Value::Bool(b) = &yaml {
            Ok(AdditionalProperties::Bool(*b))
        } else {
            Ok(AdditionalProperties::Schema(
                deserialize_enum_helper::<D, _>("additionalProperties", yaml)?,
            ))
        }
    }
}

impl Transpile for AdditionalProperties {
    type Output = Self;

    fn transpile(&self, scope: &Scope) -> anyhow::Result<Self::Output> {
        match self {
            AdditionalProperties::Bool(b) => Ok(AdditionalProperties::Bool(*b)),
            AdditionalProperties::Schema(s) => {
                Ok(AdditionalProperties::Schema(s.transpile(scope)?))
            }
        }
    }
}