roas-asyncapi 0.2.0

Rust implementation of the AsyncAPI Specification v2.6 / v3.0 / v3.1 — parse and validate
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
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! AsyncAPI v3.1 `Security Scheme` object and its OAuth 2 flows.
//!
//! Per [Security Scheme Object](https://www.asyncapi.com/docs/reference/specification/v3.1.0#securitySchemeObject).
//!
//! The schema spells this as a `oneOf` over nine shapes that share a
//! `type` discriminator, each with `additionalProperties: false`. It is
//! modeled here as one struct with a typed `type` plus the union of the
//! optional fields; the validator enforces both directions of each
//! variant's contract — the fields the `type` requires, and the fields
//! it forbids — so a wrong combination is a precise diagnostic rather
//! than an opaque "matched no variant" parse error.

use crate::validation::{Context, ValidateWithContext};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// The `type` discriminator of a security scheme.
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, Default)]
pub enum SecuritySchemeType {
    #[default]
    #[serde(rename = "userPassword")]
    UserPassword,
    #[serde(rename = "apiKey")]
    ApiKey,
    #[serde(rename = "X509")]
    X509,
    #[serde(rename = "symmetricEncryption")]
    SymmetricEncryption,
    #[serde(rename = "asymmetricEncryption")]
    AsymmetricEncryption,
    #[serde(rename = "httpApiKey")]
    HttpApiKey,
    #[serde(rename = "http")]
    Http,
    #[serde(rename = "oauth2")]
    OAuth2,
    #[serde(rename = "openIdConnect")]
    OpenIdConnect,
    #[serde(rename = "plain")]
    Plain,
    #[serde(rename = "scramSha256")]
    ScramSha256,
    #[serde(rename = "scramSha512")]
    ScramSha512,
    #[serde(rename = "gssapi")]
    Gssapi,
}

/// Where an API key lives.
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ApiKeyLocation {
    User,
    Password,
    Query,
    Header,
    Cookie,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
pub struct SecurityScheme {
    /// **Required** The type of the security scheme.
    #[serde(rename = "type")]
    pub scheme_type: SecuritySchemeType,

    /// A short description for the security scheme.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// **Required for `apiKey` / `httpApiKey`** — where the key is
    /// carried. `apiKey` takes `user` / `password`; `httpApiKey` takes
    /// `query` / `header` / `cookie`.
    #[serde(rename = "in", skip_serializing_if = "Option::is_none")]
    pub in_: Option<ApiKeyLocation>,

    /// **Required for `httpApiKey`** — the header / query / cookie name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// **Required for `http`** — the HTTP Authorization scheme.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scheme: Option<String>,

    /// A hint to the client to identify how the bearer token is
    /// formatted (`http` with the `bearer` scheme).
    #[serde(rename = "bearerFormat", skip_serializing_if = "Option::is_none")]
    pub bearer_format: Option<String>,

    /// **Required for `oauth2`** — the supported flows.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flows: Option<OAuthFlows>,

    /// **Required for `openIdConnect`** — the OpenID Connect discovery
    /// URL.
    #[serde(rename = "openIdConnectUrl", skip_serializing_if = "Option::is_none")]
    pub open_id_connect_url: Option<String>,

    /// The list of scopes required by `oauth2` / `openIdConnect`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub scopes: Vec<String>,

    /// `x-`-prefixed Specification Extensions.
    #[serde(flatten)]
    #[serde(with = "crate::common::extensions")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extensions: Option<BTreeMap<String, serde_json::Value>>,
}

impl SecurityScheme {
    /// The fields this scheme's `type` may carry, beyond `type`,
    /// `description`, and `x-` extensions.
    fn allowed_fields(&self) -> &'static [&'static str] {
        match self.scheme_type {
            SecuritySchemeType::ApiKey => &["in"],
            SecuritySchemeType::HttpApiKey => &["in", "name"],
            SecuritySchemeType::Http => &["scheme", "bearerFormat"],
            SecuritySchemeType::OAuth2 => &["flows", "scopes"],
            SecuritySchemeType::OpenIdConnect => &["openIdConnectUrl", "scopes"],
            _ => &[],
        }
    }

    /// Report any field that belongs to a different variant. Each
    /// variant is `additionalProperties: false` in the schema, so
    /// carrying a foreign field breaks the `oneOf`.
    fn check_forbidden_fields(&self, ctx: &mut Context) {
        let allowed = self.allowed_fields();
        let present: [(&str, bool); 6] = [
            ("in", self.in_.is_some()),
            ("name", self.name.is_some()),
            ("scheme", self.scheme.is_some()),
            ("bearerFormat", self.bearer_format.is_some()),
            ("flows", self.flows.is_some()),
            ("openIdConnectUrl", self.open_id_connect_url.is_some()),
        ];
        for (field, is_present) in present {
            if is_present && !allowed.contains(&field) {
                ctx.error_field(
                    field,
                    format!(
                        "is not allowed on a `{}` security scheme",
                        self.scheme_type.as_str()
                    ),
                );
            }
        }
        if !self.scopes.is_empty() && !allowed.contains(&"scopes") {
            ctx.error_field(
                "scopes",
                format!(
                    "is not allowed on a `{}` security scheme",
                    self.scheme_type.as_str()
                ),
            );
        }
    }
}

impl SecuritySchemeType {
    /// The `type` value as it appears in a document.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::UserPassword => "userPassword",
            Self::ApiKey => "apiKey",
            Self::X509 => "X509",
            Self::SymmetricEncryption => "symmetricEncryption",
            Self::AsymmetricEncryption => "asymmetricEncryption",
            Self::HttpApiKey => "httpApiKey",
            Self::Http => "http",
            Self::OAuth2 => "oauth2",
            Self::OpenIdConnect => "openIdConnect",
            Self::Plain => "plain",
            Self::ScramSha256 => "scramSha256",
            Self::ScramSha512 => "scramSha512",
            Self::Gssapi => "gssapi",
        }
    }
}

impl ValidateWithContext for SecurityScheme {
    fn validate_with_context(&self, ctx: &mut Context) {
        self.check_forbidden_fields(ctx);
        match self.scheme_type {
            SecuritySchemeType::ApiKey => match self.in_ {
                None => ctx.error_field("in", "is required for the `apiKey` type"),
                Some(ApiKeyLocation::User | ApiKeyLocation::Password) => {}
                Some(_) => ctx.error_field("in", "must be `user` or `password` for `apiKey`"),
            },
            SecuritySchemeType::HttpApiKey => {
                match self.in_ {
                    None => ctx.error_field("in", "is required for the `httpApiKey` type"),
                    Some(
                        ApiKeyLocation::Query | ApiKeyLocation::Header | ApiKeyLocation::Cookie,
                    ) => {}
                    Some(_) => ctx.error_field(
                        "in",
                        "must be `query`, `header` or `cookie` for `httpApiKey`",
                    ),
                }
                match &self.name {
                    None => ctx.error_field("name", "is required for the `httpApiKey` type"),
                    Some(name) => ctx.require_non_empty("name", name),
                }
            }
            SecuritySchemeType::Http => {
                match &self.scheme {
                    None => ctx.error_field("scheme", "is required for the `http` type"),
                    Some(scheme) => ctx.require_non_empty("scheme", scheme),
                }
                // Only the `bearer` branch of the HTTP `oneOf` declares
                // `bearerFormat`; every other scheme forbids it.
                if self.bearer_format.is_some()
                    && self.scheme.as_deref().is_some_and(|s| s != "bearer")
                {
                    ctx.error_field("bearerFormat", "is only allowed when `scheme` is `bearer`");
                }
            }
            SecuritySchemeType::OAuth2 => match &self.flows {
                None => ctx.error_field("flows", "is required for the `oauth2` type"),
                Some(flows) => ctx.in_field("flows", |ctx| flows.validate_with_context(ctx)),
            },
            SecuritySchemeType::OpenIdConnect => match &self.open_id_connect_url {
                None => ctx.error_field(
                    "openIdConnectUrl",
                    "is required for the `openIdConnect` type",
                ),
                Some(url) => ctx.require_non_empty("openIdConnectUrl", url),
            },
            _ => {}
        }
    }
}

/// The OAuth 2 flows a scheme supports.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
pub struct OAuthFlows {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub implicit: Option<OAuthFlow>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub password: Option<OAuthFlow>,

    #[serde(rename = "clientCredentials", skip_serializing_if = "Option::is_none")]
    pub client_credentials: Option<OAuthFlow>,

    #[serde(rename = "authorizationCode", skip_serializing_if = "Option::is_none")]
    pub authorization_code: Option<OAuthFlow>,

    /// `x-`-prefixed Specification Extensions.
    #[serde(flatten)]
    #[serde(with = "crate::common::extensions")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extensions: Option<BTreeMap<String, serde_json::Value>>,
}

impl ValidateWithContext for OAuthFlows {
    fn validate_with_context(&self, ctx: &mut Context) {
        if self.implicit.is_none()
            && self.password.is_none()
            && self.client_credentials.is_none()
            && self.authorization_code.is_none()
        {
            ctx.error("must define at least one OAuth flow");
        }

        // Each grant type pins which URLs it requires *and* which it
        // forbids; `availableScopes` is required by all four.
        for (field, flow, required, forbidden) in [
            (
                "implicit",
                self.implicit.as_ref(),
                &["authorizationUrl"][..],
                &["tokenUrl"][..],
            ),
            (
                "password",
                self.password.as_ref(),
                &["tokenUrl"][..],
                &["authorizationUrl"][..],
            ),
            (
                "clientCredentials",
                self.client_credentials.as_ref(),
                &["tokenUrl"][..],
                &["authorizationUrl"][..],
            ),
            (
                "authorizationCode",
                self.authorization_code.as_ref(),
                &["authorizationUrl", "tokenUrl"][..],
                &[][..],
            ),
        ] {
            let Some(flow) = flow else { continue };
            ctx.in_field(field, |ctx| {
                flow.validate_grant(ctx, field, required, forbidden)
            });
        }
    }
}

/// Configuration for one OAuth 2 flow.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
pub struct OAuthFlow {
    #[serde(rename = "authorizationUrl", skip_serializing_if = "Option::is_none")]
    pub authorization_url: Option<String>,

    #[serde(rename = "tokenUrl", skip_serializing_if = "Option::is_none")]
    pub token_url: Option<String>,

    #[serde(rename = "refreshUrl", skip_serializing_if = "Option::is_none")]
    pub refresh_url: Option<String>,

    /// **Required** The available scopes, keyed by scope name. Renamed
    /// from `scopes` in AsyncAPI 2.x.
    ///
    /// Every grant type requires it, so presence is what the validator
    /// checks — an empty map is a legitimate value, and an absent one
    /// stays absent rather than being invented on serialization.
    #[serde(rename = "availableScopes", skip_serializing_if = "Option::is_none")]
    pub available_scopes: Option<BTreeMap<String, String>>,

    /// `x-`-prefixed Specification Extensions.
    #[serde(flatten)]
    #[serde(with = "crate::common::extensions")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extensions: Option<BTreeMap<String, serde_json::Value>>,
}

impl OAuthFlow {
    fn url(&self, field: &str) -> Option<&String> {
        match field {
            "authorizationUrl" => self.authorization_url.as_ref(),
            "tokenUrl" => self.token_url.as_ref(),
            _ => None,
        }
    }

    /// Validate this flow against its grant type's contract: the URLs
    /// it must carry, the URLs it must not, and `availableScopes`.
    fn validate_grant(
        &self,
        ctx: &mut Context,
        grant: &str,
        required: &[&str],
        forbidden: &[&str],
    ) {
        for field in required {
            match self.url(field) {
                None => ctx.error_field(field, format!("is required for the `{grant}` flow")),
                Some(url) => ctx.require_non_empty(field, url),
            }
        }
        for field in forbidden {
            if self.url(field).is_some() {
                ctx.error_field(field, format!("is not allowed on the `{grant}` flow"));
            }
        }
        if self.available_scopes.is_none() {
            ctx.error_field(
                "availableScopes",
                format!("is required for the `{grant}` flow"),
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use enumset::EnumSet;
    use serde_json::json;

    fn validate(value: serde_json::Value) -> Vec<String> {
        let scheme: SecurityScheme = serde_json::from_value(value).unwrap();
        let mut ctx = Context::with_path(EnumSet::empty(), "#.securitySchemes.s");
        scheme.validate_with_context(&mut ctx);
        ctx.errors.iter().map(ToString::to_string).collect()
    }

    #[test]
    fn simple_types_need_nothing_beyond_the_discriminator() {
        for scheme_type in [
            "userPassword",
            "X509",
            "symmetricEncryption",
            "asymmetricEncryption",
            "plain",
            "scramSha256",
            "scramSha512",
            "gssapi",
        ] {
            let errors = validate(json!({ "type": scheme_type }));
            assert!(errors.is_empty(), "{scheme_type}: {errors:?}");
        }
    }

    #[test]
    fn round_trips_an_oauth2_scheme() {
        let value = json!({
            "type": "oauth2",
            "description": "OAuth",
            "scopes": ["read"],
            "flows": {
                "authorizationCode": {
                    "authorizationUrl": "https://example.com/auth",
                    "tokenUrl": "https://example.com/token",
                    "availableScopes": { "read": "Read access" }
                }
            }
        });
        let scheme: SecurityScheme = serde_json::from_value(value.clone()).unwrap();
        assert_eq!(scheme.scheme_type, SecuritySchemeType::OAuth2);
        assert_eq!(serde_json::to_value(&scheme).unwrap(), value);
        assert!(validate(value).is_empty());
    }

    #[test]
    fn api_key_location_is_constrained_to_user_or_password() {
        assert!(validate(json!({ "type": "apiKey", "in": "user" })).is_empty());

        let errors = validate(json!({ "type": "apiKey", "in": "header" }));
        assert!(
            errors
                .iter()
                .any(|e| e.contains("must be `user` or `password`")),
            "got: {errors:?}"
        );

        let errors = validate(json!({ "type": "apiKey" }));
        assert!(
            errors
                .iter()
                .any(|e| e.contains("is required for the `apiKey`"))
        );
    }

    #[test]
    fn http_api_key_requires_name_and_a_transport_location() {
        assert!(
            validate(json!({ "type": "httpApiKey", "name": "X-Key", "in": "header" })).is_empty()
        );

        let errors = validate(json!({ "type": "httpApiKey", "in": "user", "name": "" }));
        assert!(
            errors
                .iter()
                .any(|e| e.contains("must be `query`, `header` or `cookie`"))
        );
        assert!(errors.iter().any(|e| e.contains("name: must not be empty")));

        let errors = validate(json!({ "type": "httpApiKey" }));
        assert!(
            errors
                .iter()
                .any(|e| e.contains("in` is required") || e.contains("in: is required"))
        );
        assert!(errors.iter().any(|e| e.contains("name: is required")));
    }

    #[test]
    fn http_requires_a_scheme_and_openid_a_url() {
        assert!(validate(json!({ "type": "http", "scheme": "bearer" })).is_empty());
        assert!(validate(json!({ "type": "http" }))[0].contains("scheme: is required"));
        assert!(validate(json!({ "type": "http", "scheme": "" }))[0].contains("must not be empty"));

        assert!(
            validate(json!({ "type": "openIdConnect", "openIdConnectUrl": "https://e/x" }))
                .is_empty()
        );
        assert!(validate(json!({ "type": "openIdConnect" }))[0].contains("openIdConnectUrl"));
    }

    #[test]
    fn oauth2_requires_flows_and_each_flow_its_urls() {
        assert!(validate(json!({ "type": "oauth2" }))[0].contains("flows: is required"));

        let errors = validate(json!({ "type": "oauth2", "flows": {} }));
        assert!(errors.iter().any(|e| e.contains("at least one OAuth flow")));

        let errors = validate(json!({
            "type": "oauth2",
            "flows": {
                "implicit": { "availableScopes": {} },
                "password": { "availableScopes": {} },
                "clientCredentials": { "availableScopes": {} },
                "authorizationCode": { "availableScopes": {} }
            }
        }));
        assert!(
            errors
                .iter()
                .any(|e| e.contains("implicit.authorizationUrl"))
        );
        assert!(errors.iter().any(|e| e.contains("password.tokenUrl")));
        assert!(
            errors
                .iter()
                .any(|e| e.contains("clientCredentials.tokenUrl"))
        );
        assert!(
            errors
                .iter()
                .any(|e| e.contains("authorizationCode.authorizationUrl"))
        );
        assert!(
            errors
                .iter()
                .any(|e| e.contains("authorizationCode.tokenUrl"))
        );
    }

    #[test]
    fn fields_belonging_to_another_variant_are_rejected() {
        // Each `oneOf` branch is `additionalProperties: false`, so a
        // `userPassword` scheme carrying `flows` breaks the contract.
        let errors = validate(json!({ "type": "userPassword", "flows": {} }));
        assert!(
            errors.iter().any(|e| e
                == "#.securitySchemes.s.flows: is not allowed on a `userPassword` security scheme"),
            "got: {errors:?}"
        );

        let errors = validate(json!({
            "type": "http",
            "scheme": "bearer",
            "in": "header",
            "name": "X-Key",
            "openIdConnectUrl": "https://e/x",
            "scopes": ["read"]
        }));
        for field in ["in", "name", "openIdConnectUrl", "scopes"] {
            assert!(
                errors
                    .iter()
                    .any(|e| e.contains(&format!(".{field}: is not allowed"))),
                "expected {field} to be rejected, got: {errors:?}"
            );
        }
        // `bearerFormat` belongs to `http`, so it is not reported.
        assert!(!errors.iter().any(|e| e.contains("bearerFormat")));
    }

    #[test]
    fn each_variant_keeps_its_own_fields() {
        for value in [
            json!({ "type": "apiKey", "in": "user" }),
            json!({ "type": "httpApiKey", "in": "header", "name": "X-Key" }),
            json!({ "type": "http", "scheme": "bearer", "bearerFormat": "JWT" }),
            json!({
                "type": "oauth2",
                "scopes": ["read"],
                "flows": { "clientCredentials": { "tokenUrl": "https://e/t", "availableScopes": {} } }
            }),
            json!({ "type": "openIdConnect", "openIdConnectUrl": "https://e/x", "scopes": ["read"] }),
        ] {
            let errors = validate(value.clone());
            assert!(errors.is_empty(), "{value}: {errors:?}");
        }
    }

    #[test]
    fn type_renders_as_its_document_spelling() {
        for (scheme_type, text) in [
            (SecuritySchemeType::UserPassword, "userPassword"),
            (SecuritySchemeType::ApiKey, "apiKey"),
            (SecuritySchemeType::X509, "X509"),
            (
                SecuritySchemeType::SymmetricEncryption,
                "symmetricEncryption",
            ),
            (
                SecuritySchemeType::AsymmetricEncryption,
                "asymmetricEncryption",
            ),
            (SecuritySchemeType::HttpApiKey, "httpApiKey"),
            (SecuritySchemeType::Http, "http"),
            (SecuritySchemeType::OAuth2, "oauth2"),
            (SecuritySchemeType::OpenIdConnect, "openIdConnect"),
            (SecuritySchemeType::Plain, "plain"),
            (SecuritySchemeType::ScramSha256, "scramSha256"),
            (SecuritySchemeType::ScramSha512, "scramSha512"),
            (SecuritySchemeType::Gssapi, "gssapi"),
        ] {
            assert_eq!(scheme_type.as_str(), text);
            assert_eq!(serde_json::to_value(scheme_type).unwrap(), json!(text));
        }
    }

    #[test]
    fn each_grant_type_forbids_the_url_it_does_not_use() {
        // `implicit` has `not: {required: ["tokenUrl"]}`; `password` and
        // `clientCredentials` forbid `authorizationUrl`.
        let errors = validate(json!({
            "type": "oauth2",
            "flows": {
                "implicit": {
                    "authorizationUrl": "https://e/a",
                    "tokenUrl": "https://e/t",
                    "availableScopes": {}
                }
            }
        }));
        assert!(
            errors
                .iter()
                .any(|e| e
                    .contains("flows.implicit.tokenUrl: is not allowed on the `implicit` flow")),
            "got: {errors:?}"
        );

        for grant in ["password", "clientCredentials"] {
            let errors = validate(json!({
                "type": "oauth2",
                "flows": {
                    grant: {
                        "tokenUrl": "https://e/t",
                        "authorizationUrl": "https://e/a",
                        "availableScopes": {}
                    }
                }
            }));
            assert!(
                errors
                    .iter()
                    .any(|e| e.contains(&format!("{grant}.authorizationUrl: is not allowed"))),
                "{grant}: {errors:?}"
            );
        }

        // `authorizationCode` legitimately uses both.
        let errors = validate(json!({
            "type": "oauth2",
            "flows": {
                "authorizationCode": {
                    "authorizationUrl": "https://e/a",
                    "tokenUrl": "https://e/t",
                    "availableScopes": {}
                }
            }
        }));
        assert!(errors.is_empty(), "got: {errors:?}");
    }

    #[test]
    fn every_grant_type_requires_available_scopes() {
        for grant in [
            "implicit",
            "password",
            "clientCredentials",
            "authorizationCode",
        ] {
            let errors = validate(json!({
                "type": "oauth2",
                "flows": {
                    grant: { "authorizationUrl": "https://e/a", "tokenUrl": "https://e/t" }
                }
            }));
            assert!(
                errors
                    .iter()
                    .any(|e| e.contains(&format!("{grant}.availableScopes: is required"))),
                "{grant}: {errors:?}"
            );
        }
    }

    #[test]
    fn an_absent_available_scopes_is_not_invented_on_serialization() {
        let value = json!({
            "type": "oauth2",
            "flows": { "implicit": { "authorizationUrl": "https://e/a" } }
        });
        let scheme: SecurityScheme = serde_json::from_value(value.clone()).unwrap();
        assert!(
            scheme
                .flows
                .as_ref()
                .and_then(|f| f.implicit.as_ref())
                .is_some_and(|f| f.available_scopes.is_none())
        );
        assert_eq!(serde_json::to_value(&scheme).unwrap(), value);

        // An empty map is a legitimate value and stays one.
        let empty = json!({
            "type": "oauth2",
            "flows": { "implicit": { "authorizationUrl": "https://e/a", "availableScopes": {} } }
        });
        let scheme: SecurityScheme = serde_json::from_value(empty.clone()).unwrap();
        assert_eq!(serde_json::to_value(&scheme).unwrap(), empty);
    }

    #[test]
    fn bearer_format_is_only_allowed_with_the_bearer_scheme() {
        let errors = validate(json!({
            "type": "http",
            "scheme": "basic",
            "bearerFormat": "JWT"
        }));
        assert!(
            errors.iter().any(|e| e
                == "#.securitySchemes.s.bearerFormat: is only allowed when `scheme` is `bearer`"),
            "got: {errors:?}"
        );

        assert!(
            validate(json!({ "type": "http", "scheme": "bearer", "bearerFormat": "JWT" }))
                .is_empty()
        );
    }

    #[test]
    fn empty_flow_urls_are_reported_as_empty() {
        let errors = validate(json!({
            "type": "oauth2",
            "flows": { "password": { "tokenUrl": "", "availableScopes": {} } }
        }));
        assert!(
            errors
                .iter()
                .any(|e| e == "#.securitySchemes.s.flows.password.tokenUrl: must not be empty"),
            "got: {errors:?}"
        );
    }
}