appctl 0.12.0

CLI: sync OpenAPI, databases, and frameworks into LLM tool definitions; chat, run, and HTTP serve.
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
use std::collections::BTreeMap;

use anyhow::{Context, Result, anyhow, bail};
use reqwest::header::HeaderName;
use serde_json::{Map, Value, json};
use url::Url;

use crate::schema::{
    Action, ApiKeyLocation, AuthStrategy, Field, FieldType, HttpMethod, ParameterLocation,
    Resource, Safety, Schema, SyncSource, Transport, Verb,
};

use super::SyncPlugin;

const OPENAPI_UA: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));

/// Well-known paths tried after the first URL when the base URL is the site root and the
/// initial document GET returns 404.
const OPENAPI_PROBE_PATHS: &[&str] = &[
    "openapi.json",
    "v3/api-docs",
    "v2/api-docs",
    "api-docs",
    "swagger.json",
    "api/openapi.json",
];

pub struct OpenApiSync {
    source: String,
    /// `Header: value` for HTTP(S) fetches of the spec (e.g. `Authorization: Bearer env:STAGING_TOKEN`).
    auth_header: Option<String>,
}

impl OpenApiSync {
    pub fn new(source: String, auth_header: Option<String>) -> Self {
        Self {
            source,
            auth_header,
        }
    }
}

/// Fetches (or reads) the raw OpenAPI / Swagger text. `auth_header` is only used for `http`/`https` URLs
/// (same `Header: value` form as `appctl sync --auth-header`, including `env:NAME` in the value).
pub async fn load_openapi_source(source: &str, auth_header: Option<&str>) -> Result<String> {
    if source.starts_with("http://") || source.starts_with("https://") {
        fetch_openapi_url(source, auth_header)
            .await
            .with_context(|| format!("failed to load OpenAPI from {}", source))
    } else {
        tokio::fs::read_to_string(source)
            .await
            .with_context(|| format!("failed to read {}", source))
    }
}

fn build_http_client() -> Result<reqwest::Client> {
    reqwest::Client::builder()
        .user_agent(OPENAPI_UA)
        .redirect(reqwest::redirect::Policy::default())
        .build()
        .map_err(Into::into)
}

fn expand_header_value_resolved(value: &str) -> Result<String> {
    let t = value.trim();
    if let Some(var) = t.strip_prefix("env:") {
        return std::env::var(var.trim()).with_context(|| {
            format!(
                "environment variable '{}' (from --auth-header value) is not set",
                var.trim()
            )
        });
    }
    if let Some(rest) = t.strip_prefix("Bearer ") {
        if let Some(var) = rest.trim().strip_prefix("env:") {
            let v = std::env::var(var.trim()).with_context(|| {
                format!(
                    "environment variable '{}' (from --auth-header Bearer) is not set",
                    var.trim()
                )
            })?;
            return Ok(format!("Bearer {v}"));
        }
    }
    Ok(t.to_string())
}

fn resolve_auth_line_resolved(line: &str) -> Result<(HeaderName, String)> {
    let (name, value) = line.split_once(':').ok_or_else(|| {
        anyhow!("auth header must look like 'Header-Name: value' (e.g. Authorization: Bearer …)")
    })?;
    let name = name.trim();
    if name.is_empty() {
        bail!("empty header name in --auth-header");
    }
    let hname = HeaderName::from_bytes(name.as_bytes())
        .map_err(|e| anyhow!("invalid header name {name}: {e}"))?;
    let value = expand_header_value_resolved(value.trim())?;
    Ok((hname, value))
}

fn is_root_path_url(u: &Url) -> bool {
    let p = u.path();
    p.is_empty() || p == "/"
}

/// Extra URLs to try (deduped) when the user gives a root base and the first response is 404.
fn open_api_probe_urls(initial: &Url) -> Vec<String> {
    if !is_root_path_url(initial) {
        return Vec::new();
    }
    let mut out: Vec<String> = Vec::new();
    for seg in OPENAPI_PROBE_PATHS {
        if let Ok(j) = initial.join(seg) {
            let s = j.to_string();
            if !out.contains(&s) {
                out.push(s);
            }
        }
    }
    out
}

async fn fetch_openapi_get(
    client: &reqwest::Client,
    url: &str,
    auth: Option<&str>,
) -> Result<reqwest::Response> {
    let mut req = client.get(url);
    if let Some(line) = auth.map(str::trim).filter(|s| !s.is_empty()) {
        let (k, v) = resolve_auth_line_resolved(line)?;
        req = req.header(k, v);
    }
    req = req.header(
        reqwest::header::ACCEPT,
        "application/json, application/yaml, text/yaml, */*;q=0.1",
    );
    let res = req.send().await.with_context(|| format!("GET {url}"))?;
    Ok(res)
}

async fn fetch_openapi_url(user_url: &str, auth_header: Option<&str>) -> Result<String> {
    let client = build_http_client()?;
    let primary =
        Url::parse(user_url).with_context(|| format!("invalid OpenAPI URL {user_url}"))?;

    let mut candidates: Vec<String> = vec![user_url.to_string()];
    for extra in open_api_probe_urls(&primary) {
        if !candidates.contains(&extra) {
            candidates.push(extra);
        }
    }

    let mut last_err: Option<anyhow::Error> = None;
    for u in &candidates {
        let res = match fetch_openapi_get(&client, u, auth_header).await {
            Ok(r) => r,
            Err(e) => {
                last_err = Some(e);
                continue;
            }
        };
        let status = res.status();
        if status == reqwest::StatusCode::NOT_FOUND {
            last_err = Some(anyhow!("{u} -> 404"));
            continue;
        }
        if status == reqwest::StatusCode::UNAUTHORIZED || status == reqwest::StatusCode::FORBIDDEN {
            bail!(
                "OpenAPI at {u} -> {status}. Pass --auth-header (e.g. 'Authorization: Bearer <token>' or 'Authorization: Bearer env:STAGING_API_TOKEN')."
            );
        }
        if !status.is_success() {
            last_err = Some(anyhow!("{u} -> {status}"));
            continue;
        }
        let text = res.text().await.unwrap_or_default();
        if text.trim().is_empty() {
            last_err = Some(anyhow!("{u} returned empty body"));
            continue;
        }
        return Ok(text);
    }

    if let Some(e) = last_err {
        if candidates.len() > 1 {
            bail!(
                "could not load OpenAPI from {user_url} (candidates: {}); last error: {e:#}",
                candidates.join(", ")
            );
        }
        return Err(e.context(format!("failed to fetch OpenAPI from {user_url}")));
    }
    Err(anyhow!(
        "no OpenAPI document found (candidates: {})",
        candidates.join(", ")
    ))
}

#[async_trait::async_trait]
impl SyncPlugin for OpenApiSync {
    async fn introspect(&self) -> Result<Schema> {
        let raw = load_openapi_source(&self.source, self.auth_header.as_deref()).await?;

        let document: Value = serde_json::from_str(&raw)
            .or_else(|_| serde_yaml::from_str(&raw))
            .with_context(|| {
                format!(
                    "failed to parse OpenAPI or Swagger document from {}",
                    self.source
                )
            })?;

        let base_url = detect_base_url(&document);
        let auth = detect_auth(&document);
        let resources = build_resources(&document)?;

        Ok(Schema {
            source: SyncSource::Openapi,
            base_url,
            auth,
            resources,
            metadata: Map::new(),
        })
    }
}

fn build_resources(document: &Value) -> Result<Vec<Resource>> {
    let paths = document
        .get("paths")
        .and_then(Value::as_object)
        .context("document missing paths object")?;

    let mut grouped: BTreeMap<String, Resource> = BTreeMap::new();

    for (path, path_item) in paths {
        let path_parameters = path_item
            .get("parameters")
            .and_then(Value::as_array)
            .cloned()
            .unwrap_or_default();

        let path_item_obj = path_item
            .as_object()
            .ok_or_else(|| anyhow!("path item for '{}' is not an object", path))?;

        for (method_name, operation) in path_item_obj {
            let Some(method) = parse_method(method_name) else {
                continue;
            };

            let op = operation
                .as_object()
                .ok_or_else(|| anyhow!("operation {} {} is not an object", method_name, path))?;

            let resource_name = operation_resource_name(path, op);
            let resource = grouped
                .entry(resource_name.clone())
                .or_insert_with(|| Resource {
                    name: resource_name.clone(),
                    description: op
                        .get("summary")
                        .and_then(Value::as_str)
                        .map(ToString::to_string),
                    fields: Vec::new(),
                    actions: Vec::new(),
                    metadata: Map::new(),
                });

            let parameters = collect_parameters(document, &path_parameters, op)?;
            let action_name = operation_name(path, method_name, op, &resource.name);

            let action = Action {
                name: action_name,
                description: op
                    .get("summary")
                    .or_else(|| op.get("description"))
                    .and_then(Value::as_str)
                    .map(ToString::to_string),
                verb: verb_from_method(&method),
                transport: Transport::Http {
                    method,
                    path: path.clone(),
                    query: parameters
                        .iter()
                        .filter(|field| matches!(field.location, Some(ParameterLocation::Query)))
                        .map(|field| field.name.clone())
                        .collect(),
                },
                parameters,
                safety: safety_from_method_name(method_name),
                resource: Some(resource.name.clone()),
                provenance: crate::schema::Provenance::Declared,
                metadata: Map::new(),
            };

            resource.actions.push(action);
        }
    }

    Ok(grouped.into_values().collect())
}

fn collect_parameters(
    document: &Value,
    path_parameters: &[Value],
    operation: &Map<String, Value>,
) -> Result<Vec<Field>> {
    let mut params = Vec::new();

    for parameter in path_parameters.iter().chain(
        operation
            .get("parameters")
            .and_then(Value::as_array)
            .into_iter()
            .flatten(),
    ) {
        params.push(parameter_to_field(document, parameter)?);
    }

    if let Some(body) = operation.get("requestBody") {
        params.extend(request_body_fields(document, body)?);
    } else if let Some(body_param) = operation.get("consumes").and_then(Value::as_array) {
        let _ = body_param;
    }

    Ok(dedup_fields(params))
}

fn parameter_to_field(document: &Value, parameter: &Value) -> Result<Field> {
    let resolved = resolve_ref(document, parameter);
    let name = resolved
        .get("name")
        .and_then(Value::as_str)
        .context("parameter missing name")?;
    let location = match resolved
        .get("in")
        .and_then(Value::as_str)
        .unwrap_or("query")
    {
        "path" => ParameterLocation::Path,
        "query" => ParameterLocation::Query,
        "header" => ParameterLocation::Header,
        _ => ParameterLocation::Body,
    };

    let schema = resolved.get("schema").unwrap_or(&Value::Null);
    let field = if schema.is_null() {
        Field {
            name: name.to_string(),
            description: resolved
                .get("description")
                .and_then(Value::as_str)
                .map(ToString::to_string),
            field_type: FieldType::from_openapi_type(
                resolved.get("type").and_then(Value::as_str),
                resolved.get("format").and_then(Value::as_str),
            ),
            required: resolved
                .get("required")
                .and_then(Value::as_bool)
                .unwrap_or(false),
            location: Some(location),
            default: None,
            enum_values: resolved
                .get("enum")
                .and_then(Value::as_array)
                .cloned()
                .unwrap_or_default(),
        }
    } else {
        schema_to_field(
            document,
            name,
            schema,
            Some(location),
            resolved.get("required"),
        )
    };

    Ok(field)
}

fn request_body_fields(document: &Value, body: &Value) -> Result<Vec<Field>> {
    let resolved = resolve_ref(document, body);
    let required = resolved
        .get("required")
        .and_then(Value::as_bool)
        .unwrap_or(false);
    let Some(content) = resolved.get("content").and_then(Value::as_object) else {
        return Ok(Vec::new());
    };

    let schema = content
        .get("application/json")
        .or_else(|| content.values().next())
        .and_then(|entry| entry.get("schema"))
        .unwrap_or(&Value::Null);

    if schema.is_null() {
        return Ok(Vec::new());
    }

    let schema = resolve_ref(document, schema);
    if let Some(properties) = schema.get("properties").and_then(Value::as_object) {
        let required_fields = schema
            .get("required")
            .and_then(Value::as_array)
            .cloned()
            .unwrap_or_default();

        let mut out = Vec::new();
        for (name, property) in properties {
            let mut field = schema_to_field(
                document,
                name,
                property,
                Some(ParameterLocation::Body),
                None,
            );
            field.required = required || required_fields.contains(&Value::String(name.clone()));
            out.push(field);
        }
        Ok(out)
    } else {
        Ok(vec![schema_to_field(
            document,
            "body",
            schema,
            Some(ParameterLocation::Body),
            Some(&Value::Bool(required)),
        )])
    }
}

fn schema_to_field(
    document: &Value,
    name: &str,
    schema: &Value,
    location: Option<ParameterLocation>,
    required: Option<&Value>,
) -> Field {
    let schema = resolve_ref(document, schema);
    let field_type = FieldType::from_openapi_type(
        schema.get("type").and_then(Value::as_str),
        schema.get("format").and_then(Value::as_str),
    );

    Field {
        name: name.to_string(),
        description: schema
            .get("description")
            .and_then(Value::as_str)
            .map(ToString::to_string),
        field_type,
        required: required.and_then(Value::as_bool).unwrap_or(false),
        location,
        default: schema.get("default").cloned(),
        enum_values: schema
            .get("enum")
            .and_then(Value::as_array)
            .cloned()
            .unwrap_or_default(),
    }
}

fn dedup_fields(fields: Vec<Field>) -> Vec<Field> {
    let mut seen = BTreeMap::new();
    for field in fields {
        seen.insert(field.name.clone(), field);
    }
    seen.into_values().collect()
}

fn detect_base_url(document: &Value) -> Option<String> {
    document
        .get("servers")
        .and_then(Value::as_array)
        .and_then(|servers| servers.first())
        .and_then(|server| server.get("url"))
        .and_then(Value::as_str)
        .map(ToString::to_string)
        .or_else(|| {
            let host = document.get("host").and_then(Value::as_str)?;
            let base_path = document
                .get("basePath")
                .and_then(Value::as_str)
                .unwrap_or_default();
            let scheme = document
                .get("schemes")
                .and_then(Value::as_array)
                .and_then(|schemes| schemes.first())
                .and_then(Value::as_str)
                .unwrap_or("https");
            Some(format!("{scheme}://{host}{base_path}"))
        })
}

fn detect_auth(document: &Value) -> AuthStrategy {
    let schemes = document
        .get("components")
        .and_then(|value| value.get("securitySchemes"))
        .or_else(|| document.get("securityDefinitions"))
        .and_then(Value::as_object);

    let Some(schemes) = schemes else {
        return AuthStrategy::None;
    };

    for name in preferred_security_scheme_names(document, schemes) {
        let Some(scheme) = schemes.get(&name) else {
            continue;
        };
        if let Some(strategy) = auth_strategy_from_scheme(&name, scheme) {
            return strategy;
        }
    }

    AuthStrategy::None
}

fn preferred_security_scheme_names(document: &Value, schemes: &Map<String, Value>) -> Vec<String> {
    let mut names = Vec::new();
    if let Some(security) = document.get("security").and_then(Value::as_array) {
        for requirement in security {
            let Some(requirement) = requirement.as_object() else {
                continue;
            };
            for name in requirement.keys() {
                if !names.contains(name) {
                    names.push(name.clone());
                }
            }
        }
    }
    for name in schemes.keys() {
        if !names.contains(name) {
            names.push(name.clone());
        }
    }
    names
}

fn auth_strategy_from_scheme(name: &str, scheme: &Value) -> Option<AuthStrategy> {
    match scheme
        .get("type")
        .and_then(Value::as_str)
        .unwrap_or_default()
    {
        "apiKey" => {
            let header = scheme
                .get("name")
                .and_then(Value::as_str)
                .unwrap_or("Authorization")
                .to_string();
            let location = match scheme.get("in").and_then(Value::as_str) {
                Some("query") => ApiKeyLocation::Query,
                Some("cookie") => ApiKeyLocation::Cookie,
                _ => ApiKeyLocation::Header,
            };
            Some(AuthStrategy::ApiKey {
                header,
                env_ref: name.to_string(),
                location,
            })
        }
        "http" if scheme.get("scheme").and_then(Value::as_str) == Some("bearer") => {
            Some(AuthStrategy::Bearer {
                env_ref: name.to_string(),
            })
        }
        "http" if scheme.get("scheme").and_then(Value::as_str) == Some("basic") => {
            Some(AuthStrategy::Basic {
                username_ref: format!("{name}_username"),
                password_ref: format!("{name}_password"),
            })
        }
        "basic" => Some(AuthStrategy::Basic {
            username_ref: format!("{name}_username"),
            password_ref: format!("{name}_password"),
        }),
        "oauth2" => {
            let auth_url = scheme
                .pointer("/flows/authorizationCode/authorizationUrl")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_string();
            let token_url = scheme
                .pointer("/flows/authorizationCode/tokenUrl")
                .or_else(|| scheme.pointer("/flows/clientCredentials/tokenUrl"))
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_string();
            let scopes = scheme
                .pointer("/flows/authorizationCode/scopes")
                .and_then(Value::as_object)
                .map(|scopes| scopes.keys().cloned().collect())
                .unwrap_or_default();
            Some(AuthStrategy::OAuth2 {
                provider: Some(name.to_string()),
                client_id_ref: format!("{name}_client_id"),
                client_secret_ref: Some(format!("{name}_client_secret")),
                auth_url,
                token_url,
                scopes,
                redirect_port: 8421,
            })
        }
        _ => None,
    }
}

fn operation_resource_name(path: &str, operation: &Map<String, Value>) -> String {
    operation
        .get("tags")
        .and_then(Value::as_array)
        .and_then(|tags| tags.first())
        .and_then(Value::as_str)
        .map(sanitize_name)
        .unwrap_or_else(|| {
            path.trim_start_matches('/')
                .split('/')
                .find(|segment| !segment.starts_with('{') && !segment.is_empty())
                .map(sanitize_name)
                .unwrap_or_else(|| "resource".to_string())
        })
}

fn operation_name(
    path: &str,
    method_name: &str,
    operation: &Map<String, Value>,
    resource: &str,
) -> String {
    if let Some(id) = operation.get("operationId").and_then(Value::as_str) {
        return sanitize_name(id);
    }

    let has_id = path.contains('{');
    let prefix = match (method_name, has_id) {
        ("get", false) => "list",
        ("get", true) => "get",
        ("post", _) => "create",
        ("put", _) | ("patch", _) => "update",
        ("delete", _) => "delete",
        _ => "call",
    };

    format!("{prefix}_{}", sanitize_name(resource))
}

fn sanitize_name(input: &str) -> String {
    input
        .chars()
        .map(|ch| if ch.is_ascii_alphanumeric() { ch } else { '_' })
        .collect::<String>()
        .trim_matches('_')
        .to_lowercase()
}

fn parse_method(name: &str) -> Option<HttpMethod> {
    Some(match name {
        "get" => HttpMethod::GET,
        "post" => HttpMethod::POST,
        "put" => HttpMethod::PUT,
        "patch" => HttpMethod::PATCH,
        "delete" => HttpMethod::DELETE,
        _ => return None,
    })
}

fn verb_from_method(method: &HttpMethod) -> Verb {
    match method {
        HttpMethod::GET => Verb::Get,
        HttpMethod::POST => Verb::Create,
        HttpMethod::PUT | HttpMethod::PATCH => Verb::Update,
        HttpMethod::DELETE => Verb::Delete,
    }
}

fn safety_from_method_name(method: &str) -> Safety {
    match method {
        "get" => Safety::ReadOnly,
        "delete" => Safety::Destructive,
        _ => Safety::Mutating,
    }
}

fn resolve_ref<'a>(document: &'a Value, value: &'a Value) -> &'a Value {
    let Some(reference) = value.get("$ref").and_then(Value::as_str) else {
        return value;
    };
    if !reference.starts_with("#/") {
        return value;
    }
    document.pointer(&reference[1..]).unwrap_or(value)
}

#[allow(dead_code)]
fn _example() -> Value {
    json!({})
}

#[cfg(test)]
mod openapi_fetch_tests {
    use super::*;

    #[test]
    fn probe_urls_include_openapi_json_for_root_base() {
        let u = Url::parse("https://a.example:8443/").unwrap();
        let v = open_api_probe_urls(&u);
        assert!(v.iter().any(|s| s.ends_with("/openapi.json")));
        assert!(v.iter().any(|s| s.contains("v3/api-docs")));
    }

    #[test]
    fn probe_urls_empty_when_path_is_not_root() {
        let u = Url::parse("https://a.example/foo/bar.json").unwrap();
        assert!(open_api_probe_urls(&u).is_empty());
    }

    #[test]
    fn detect_auth_prefers_declared_security_and_query_api_keys() {
        let document = json!({
            "openapi": "3.0.0",
            "security": [{ "queryKey": [] }],
            "components": {
                "securitySchemes": {
                    "unusedBearer": { "type": "http", "scheme": "bearer" },
                    "queryKey": { "type": "apiKey", "in": "query", "name": "api_key" }
                }
            }
        });

        let AuthStrategy::ApiKey {
            header, location, ..
        } = detect_auth(&document)
        else {
            panic!("expected query api key");
        };
        assert_eq!(header, "api_key");
        assert_eq!(location, ApiKeyLocation::Query);
    }

    #[test]
    fn detect_auth_supports_openapi3_http_basic() {
        let document = json!({
            "openapi": "3.0.0",
            "components": {
                "securitySchemes": {
                    "basicAuth": { "type": "http", "scheme": "basic" }
                }
            }
        });

        assert!(matches!(detect_auth(&document), AuthStrategy::Basic { .. }));
    }
}