aperture-cli 0.1.9

Dynamic CLI generator for OpenAPI specifications
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
use crate::cache::models::CachedSpec;
use crate::config::models::{ApiConfig, GlobalConfig};
use crate::config::server_variable_resolver::ServerVariableResolver;
#[allow(unused_imports)]
use crate::error::{Error, ErrorKind};

/// Resolves the base URL for an API based on a priority hierarchy
pub struct BaseUrlResolver<'a> {
    /// The cached API specification
    spec: &'a CachedSpec,
    /// Global configuration containing API overrides
    global_config: Option<&'a GlobalConfig>,
    /// Optional environment override (if None, reads from `APERTURE_ENV` at resolve time)
    environment_override: Option<String>,
}

impl<'a> BaseUrlResolver<'a> {
    /// Creates a new URL resolver for the given spec
    #[must_use]
    pub const fn new(spec: &'a CachedSpec) -> Self {
        Self {
            spec,
            global_config: None,
            environment_override: None,
        }
    }

    /// Sets the global configuration for API-specific overrides
    #[must_use]
    #[allow(clippy::missing_const_for_fn)]
    pub fn with_global_config(mut self, config: &'a GlobalConfig) -> Self {
        self.global_config = Some(config);
        self
    }

    /// Sets the environment explicitly (overrides `APERTURE_ENV`)
    #[must_use]
    pub fn with_environment(mut self, env: Option<String>) -> Self {
        self.environment_override = env;
        self
    }

    /// Resolves the base URL according to the priority hierarchy:
    /// 1. Explicit parameter (for testing)
    /// 2. Per-API config override with environment support
    /// 3. Environment variable: `APERTURE_BASE_URL`
    /// 4. Cached spec default
    /// 5. Fallback: <https://api.example.com>
    #[must_use]
    pub fn resolve(&self, explicit_url: Option<&str>) -> String {
        self.resolve_with_variables(explicit_url, &[])
            .unwrap_or_else(|err| {
                match err {
                    // For validation errors, log and fallback to basic resolution
                    // This maintains backward compatibility while providing visibility
                    Error::Internal {
                        kind: crate::error::ErrorKind::ServerVariable,
                        ..
                    } => {
                        // ast-grep-ignore: no-println
                        eprintln!(
                            "{} Server variable error: {err}",
                            crate::constants::MSG_WARNING_PREFIX
                        );
                        self.resolve_basic(explicit_url)
                    }
                    // Fallback for all other errors (template resolution, missing variables, etc.)
                    _ => self.resolve_basic(explicit_url),
                }
            })
    }

    /// Resolves the base URL with server variable substitution support
    ///
    /// # Arguments
    /// * `explicit_url` - Explicit URL override (for testing)
    /// * `server_var_args` - Server variable arguments from CLI (e.g., `["region=us", "env=prod"]`)
    ///
    /// # Returns
    /// * `Ok(String)` - Resolved URL with variables substituted
    /// * `Err(Error)` - Server variable validation or substitution errors
    ///
    /// # Errors
    /// Returns errors for:
    /// - Invalid server variable format or values
    /// - Missing required server variables
    /// - URL template substitution failures
    pub fn resolve_with_variables(
        &self,
        explicit_url: Option<&str>,
        server_var_args: &[String],
    ) -> Result<String, Error> {
        // First resolve the base URL using the standard priority hierarchy
        let base_url = self.resolve_basic(explicit_url);

        // If the URL doesn't contain template variables, return as-is
        if !base_url.contains('{') {
            return Ok(base_url);
        }

        // If no server variables are defined in the spec but URL has templates,
        // this indicates a backward compatibility issue - the spec has template
        // URLs but no server variable definitions
        if self.spec.server_variables.is_empty() {
            let template_vars = extract_template_variables(&base_url);

            let Some(first_var) = template_vars.first() else {
                return Ok(base_url);
            };

            return Err(Error::unresolved_template_variable(first_var, &base_url));
        }

        // Resolve server variables and apply template substitution
        let resolver = ServerVariableResolver::new(self.spec);
        let resolved_variables = resolver.resolve_variables(server_var_args)?;
        resolver.substitute_url(&base_url, &resolved_variables)
    }

    /// Basic URL resolution without server variable processing (internal helper)
    fn resolve_basic(&self, explicit_url: Option<&str>) -> String {
        // Priority 1: Explicit parameter (for testing)
        if let Some(url) = explicit_url {
            return url.to_string();
        }

        // Priority 2: Per-API config override
        let Some(config) = self.global_config else {
            // No global config, proceed to next priority
            return self.resolve_env_or_spec_or_fallback();
        };

        let Some(api_config) = config.api_configs.get(&self.spec.name) else {
            // No API-specific config, proceed to next priority
            return self.resolve_env_or_spec_or_fallback();
        };

        // Check environment-specific URL first
        let env_to_check = self.environment_override.as_ref().map_or_else(
            || std::env::var(crate::constants::ENV_APERTURE_ENV).unwrap_or_default(),
            std::clone::Clone::clone,
        );

        // Early return if environment is set and has a URL
        if !env_to_check.is_empty() && api_config.environment_urls.contains_key(&env_to_check) {
            return api_config.environment_urls[&env_to_check].clone();
        }

        // Then check general override
        if let Some(override_url) = &api_config.base_url_override {
            return override_url.clone();
        }

        // No config override found, proceed to next priority
        self.resolve_env_or_spec_or_fallback()
    }

    /// Helper method to resolve URL from environment variable, spec default, or fallback
    fn resolve_env_or_spec_or_fallback(&self) -> String {
        // Priority 3: Environment variable
        if let Ok(url) = std::env::var(crate::constants::ENV_APERTURE_BASE_URL) {
            return url;
        }

        // Priority 4: Cached spec default
        if let Some(base_url) = &self.spec.base_url {
            return base_url.clone();
        }

        // Priority 5: Fallback
        "https://api.example.com".to_string()
    }

    /// Gets the API config if available
    #[must_use]
    pub fn get_api_config(&self) -> Option<&ApiConfig> {
        self.global_config
            .and_then(|config| config.api_configs.get(&self.spec.name))
    }
}

/// Extracts template variable names from a URL string
fn extract_template_variables(url: &str) -> Vec<String> {
    let mut template_vars = Vec::new();
    let mut start = 0;

    while let Some(open) = url[start..].find('{') {
        let open_pos = start + open;

        let Some(close) = url[open_pos..].find('}') else {
            break;
        };

        let close_pos = open_pos + close;
        let var_name = &url[open_pos + 1..close_pos];

        if !var_name.is_empty() {
            template_vars.push(var_name.to_string());
        }

        start = close_pos + 1;
    }

    template_vars
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cache::models::{CachedSpec, ServerVariable};
    use crate::error::ErrorKind;
    use std::collections::HashMap;
    use std::sync::Mutex;

    // Static mutex to ensure only one test can modify environment variables at a time
    static ENV_TEST_MUTEX: Mutex<()> = Mutex::new(());

    fn create_test_spec(name: &str, base_url: Option<&str>) -> CachedSpec {
        CachedSpec {
            cache_format_version: crate::cache::models::CACHE_FORMAT_VERSION,
            name: name.to_string(),
            version: "1.0.0".to_string(),
            commands: vec![],
            base_url: base_url.map(std::string::ToString::to_string),
            servers: base_url.map(|s| vec![s.to_string()]).unwrap_or_default(),
            security_schemes: HashMap::new(),
            skipped_endpoints: vec![],
            server_variables: HashMap::new(),
        }
    }

    fn create_test_spec_with_variables(name: &str, base_url: Option<&str>) -> CachedSpec {
        let mut server_variables = HashMap::new();

        // Add test server variables
        server_variables.insert(
            "region".to_string(),
            ServerVariable {
                default: Some("us".to_string()),
                enum_values: vec!["us".to_string(), "eu".to_string(), "ap".to_string()],
                description: Some("API region".to_string()),
            },
        );

        server_variables.insert(
            "env".to_string(),
            ServerVariable {
                default: None,
                enum_values: vec!["dev".to_string(), "staging".to_string(), "prod".to_string()],
                description: Some("Environment".to_string()),
            },
        );

        CachedSpec {
            cache_format_version: crate::cache::models::CACHE_FORMAT_VERSION,
            name: name.to_string(),
            version: "1.0.0".to_string(),
            commands: vec![],
            base_url: base_url.map(std::string::ToString::to_string),
            servers: base_url.map(|s| vec![s.to_string()]).unwrap_or_default(),
            security_schemes: HashMap::new(),
            skipped_endpoints: vec![],
            server_variables,
        }
    }

    /// Test harness to isolate environment variable changes with mutual exclusion
    fn test_with_env_isolation<F>(test_fn: F)
    where
        F: FnOnce() + std::panic::UnwindSafe,
    {
        // Acquire mutex to prevent parallel env var access
        let guard = ENV_TEST_MUTEX.lock().unwrap();

        // Store original value
        let original_value = std::env::var(crate::constants::ENV_APERTURE_BASE_URL).ok();

        // Clean up first
        std::env::remove_var(crate::constants::ENV_APERTURE_BASE_URL);

        // Run the test with panic protection
        let result = std::panic::catch_unwind(test_fn);

        // Always restore original state, even if test panicked
        if let Some(original) = original_value {
            std::env::set_var(crate::constants::ENV_APERTURE_BASE_URL, original);
        } else {
            std::env::remove_var(crate::constants::ENV_APERTURE_BASE_URL);
        }

        // Drop the guard before re-panicking to release the mutex
        drop(guard);

        // Re-panic if the test failed
        if let Err(panic_info) = result {
            std::panic::resume_unwind(panic_info);
        }
    }

    #[test]
    fn test_priority_1_explicit_url() {
        test_with_env_isolation(|| {
            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
            let resolver = BaseUrlResolver::new(&spec);

            assert_eq!(
                resolver.resolve(Some("https://explicit.example.com")),
                "https://explicit.example.com"
            );
        });
    }

    #[test]
    fn test_priority_2_api_config_override() {
        test_with_env_isolation(|| {
            let spec = create_test_spec("test-api", Some("https://spec.example.com"));

            let mut api_configs = HashMap::new();
            api_configs.insert(
                "test-api".to_string(),
                ApiConfig {
                    base_url_override: Some("https://config.example.com".to_string()),
                    environment_urls: HashMap::new(),
                    strict_mode: false,
                    secrets: HashMap::new(),
                    command_mapping: None,
                },
            );

            let global_config = GlobalConfig {
                api_configs,
                ..Default::default()
            };

            let resolver = BaseUrlResolver::new(&spec).with_global_config(&global_config);

            assert_eq!(resolver.resolve(None), "https://config.example.com");
        });
    }

    #[test]
    fn test_priority_2_environment_specific() {
        test_with_env_isolation(|| {
            let spec = create_test_spec("test-api", Some("https://spec.example.com"));

            let mut environment_urls = HashMap::new();
            environment_urls.insert(
                "staging".to_string(),
                "https://staging.example.com".to_string(),
            );
            environment_urls.insert("prod".to_string(), "https://prod.example.com".to_string());

            let mut api_configs = HashMap::new();
            api_configs.insert(
                "test-api".to_string(),
                ApiConfig {
                    base_url_override: Some("https://config.example.com".to_string()),
                    environment_urls,
                    strict_mode: false,
                    secrets: HashMap::new(),
                    command_mapping: None,
                },
            );

            let global_config = GlobalConfig {
                api_configs,
                ..Default::default()
            };

            let resolver = BaseUrlResolver::new(&spec)
                .with_global_config(&global_config)
                .with_environment(Some("staging".to_string()));

            assert_eq!(resolver.resolve(None), "https://staging.example.com");
        });
    }

    #[test]
    fn test_priority_config_override_beats_env_var() {
        // Test that config override takes precedence over environment variable
        test_with_env_isolation(|| {
            let spec = create_test_spec("test-api", Some("https://spec.example.com"));

            // Set env var
            std::env::set_var(
                crate::constants::ENV_APERTURE_BASE_URL,
                "https://env.example.com",
            );

            let mut api_configs = HashMap::new();
            api_configs.insert(
                "test-api".to_string(),
                ApiConfig {
                    base_url_override: Some("https://config.example.com".to_string()),
                    environment_urls: HashMap::new(),
                    strict_mode: false,
                    secrets: HashMap::new(),
                    command_mapping: None,
                },
            );

            let global_config = GlobalConfig {
                api_configs,
                ..Default::default()
            };

            let resolver = BaseUrlResolver::new(&spec).with_global_config(&global_config);

            // Config override should win over env var
            assert_eq!(resolver.resolve(None), "https://config.example.com");
        });
    }

    #[test]
    fn test_priority_3_env_var() {
        // Use a custom test harness to isolate environment variables
        test_with_env_isolation(|| {
            let spec = create_test_spec("test-api", Some("https://spec.example.com"));

            // Set env var
            std::env::set_var(
                crate::constants::ENV_APERTURE_BASE_URL,
                "https://env.example.com",
            );

            let resolver = BaseUrlResolver::new(&spec);

            assert_eq!(resolver.resolve(None), "https://env.example.com");
        });
    }

    #[test]
    fn test_priority_4_spec_default() {
        test_with_env_isolation(|| {
            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
            let resolver = BaseUrlResolver::new(&spec);

            assert_eq!(resolver.resolve(None), "https://spec.example.com");
        });
    }

    #[test]
    fn test_priority_5_fallback() {
        test_with_env_isolation(|| {
            let spec = create_test_spec("test-api", None);
            let resolver = BaseUrlResolver::new(&spec);

            assert_eq!(resolver.resolve(None), "https://api.example.com");
        });
    }

    #[test]
    fn test_server_variable_resolution_with_all_provided() {
        test_with_env_isolation(|| {
            let spec = create_test_spec_with_variables(
                "test-api",
                Some("https://{region}-{env}.api.example.com"),
            );
            let resolver = BaseUrlResolver::new(&spec);

            let server_vars = vec!["region=eu".to_string(), "env=staging".to_string()];
            let result = resolver.resolve_with_variables(None, &server_vars).unwrap();

            assert_eq!(result, "https://eu-staging.api.example.com");
        });
    }

    #[test]
    fn test_server_variable_resolution_with_defaults() {
        test_with_env_isolation(|| {
            let spec = create_test_spec_with_variables(
                "test-api",
                Some("https://{region}-{env}.api.example.com"),
            );
            let resolver = BaseUrlResolver::new(&spec);

            // Only provide required variable, let region use default
            let server_vars = vec!["env=prod".to_string()];
            let result = resolver.resolve_with_variables(None, &server_vars).unwrap();

            assert_eq!(result, "https://us-prod.api.example.com");
        });
    }

    #[test]
    fn test_server_variable_resolution_missing_required() {
        test_with_env_isolation(|| {
            let spec = create_test_spec_with_variables(
                "test-api",
                Some("https://{region}-{env}.api.example.com"),
            );
            let resolver = BaseUrlResolver::new(&spec);

            // Missing required 'env' variable
            let server_vars = vec!["region=us".to_string()];
            let result = resolver.resolve_with_variables(None, &server_vars);

            assert!(result.is_err());
        });
    }

    #[test]
    fn test_server_variable_resolution_invalid_enum() {
        test_with_env_isolation(|| {
            let spec = create_test_spec_with_variables(
                "test-api",
                Some("https://{region}-{env}.api.example.com"),
            );
            let resolver = BaseUrlResolver::new(&spec);

            let server_vars = vec!["region=invalid".to_string(), "env=prod".to_string()];
            let result = resolver.resolve_with_variables(None, &server_vars);

            assert!(result.is_err());
        });
    }

    #[test]
    fn test_non_template_url_with_server_variables() {
        test_with_env_isolation(|| {
            let spec = create_test_spec_with_variables("test-api", Some("https://api.example.com"));
            let resolver = BaseUrlResolver::new(&spec);

            // Non-template URL should be returned as-is even with server variables defined
            let server_vars = vec!["region=eu".to_string(), "env=prod".to_string()];
            let result = resolver.resolve_with_variables(None, &server_vars).unwrap();

            assert_eq!(result, "https://api.example.com");
        });
    }

    #[test]
    fn test_no_server_variables_defined() {
        test_with_env_isolation(|| {
            let spec = create_test_spec("test-api", Some("https://{region}.api.example.com"));
            let resolver = BaseUrlResolver::new(&spec);

            // Template URL but no server variables defined in spec should error
            let server_vars = vec!["region=eu".to_string()];
            let result = resolver.resolve_with_variables(None, &server_vars);

            // Should fail with UnresolvedTemplateVariable error
            assert!(result.is_err());
            match result.unwrap_err() {
                Error::Internal {
                    kind: ErrorKind::ServerVariable,
                    message,
                    ..
                } => {
                    assert!(message.contains("region"));
                }
                _ => panic!("Expected Internal ServerVariable error"),
            }
        });
    }

    #[test]
    fn test_server_variable_fallback_compatibility() {
        test_with_env_isolation(|| {
            let spec = create_test_spec_with_variables(
                "test-api",
                Some("https://{region}-{env}.api.example.com"),
            );
            let resolver = BaseUrlResolver::new(&spec);

            // resolve() method should gracefully fallback when server variables fail
            // This tests backward compatibility - when server variables are missing
            // required values, it should fallback to basic resolution
            let result = resolver.resolve(None);

            // Should return the basic URL resolution (original template URL)
            assert_eq!(result, "https://{region}-{env}.api.example.com");
        });
    }

    #[test]
    fn test_server_variable_with_config_override() {
        test_with_env_isolation(|| {
            let spec =
                create_test_spec_with_variables("test-api", Some("https://{region}.original.com"));

            let mut api_configs = HashMap::new();
            api_configs.insert(
                "test-api".to_string(),
                ApiConfig {
                    base_url_override: Some("https://{region}-override.example.com".to_string()),
                    environment_urls: HashMap::new(),
                    strict_mode: false,
                    secrets: HashMap::new(),
                    command_mapping: None,
                },
            );

            let global_config = GlobalConfig {
                api_configs,
                ..Default::default()
            };

            let resolver = BaseUrlResolver::new(&spec).with_global_config(&global_config);

            let server_vars = vec!["env=prod".to_string()]; // region should use default 'us'
            let result = resolver.resolve_with_variables(None, &server_vars).unwrap();

            // Should use config override as base, then apply server variable substitution
            assert_eq!(result, "https://us-override.example.com");
        });
    }

    #[test]
    fn test_malformed_templates_pass_through() {
        test_with_env_isolation(|| {
            // Test URLs with empty braces or malformed templates
            let spec = create_test_spec("test-api", Some("https://api.example.com/path{}"));
            let resolver = BaseUrlResolver::new(&spec);

            let result = resolver.resolve_with_variables(None, &[]).unwrap();
            // Empty braces should pass through as they're not valid template variables
            assert_eq!(result, "https://api.example.com/path{}");
        });
    }

    #[test]
    fn test_backward_compatibility_no_server_vars_non_template() {
        test_with_env_isolation(|| {
            // Non-template URL with no server variables should work normally
            let spec = create_test_spec("test-api", Some("https://api.example.com"));
            let resolver = BaseUrlResolver::new(&spec);

            let result = resolver.resolve_with_variables(None, &[]).unwrap();
            assert_eq!(result, "https://api.example.com");
        });
    }
}