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
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
use crate::cache::models::{CachedSpec, ServerVariable};
use crate::error::Error;
use std::collections::HashMap;

/// Resolves server template variables by parsing CLI arguments and applying validation
pub struct ServerVariableResolver<'a> {
    spec: &'a CachedSpec,
}

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

    /// Parses and validates server variables from CLI arguments
    ///
    /// # Arguments
    /// * `server_var_args` - Command line arguments in format "key=value"
    ///
    /// # Returns
    /// * `Ok(HashMap<String, String>)` - Resolved server variables ready for URL substitution
    /// * `Err(Error)` - Validation errors or parsing failures
    ///
    /// # Errors
    /// Returns errors for:
    /// - Invalid key=value format
    /// - Unknown server variables not defined in `OpenAPI` spec
    /// - Enum constraint violations
    /// - Missing required variables (when defaults are not available)
    pub fn resolve_variables(
        &self,
        server_var_args: &[String],
    ) -> Result<HashMap<String, String>, Error> {
        let mut resolved_vars = HashMap::new();

        // Parse CLI arguments
        for arg in server_var_args {
            let (key, value) = Self::parse_key_value(arg)?;
            resolved_vars.insert(key, value);
        }

        // Validate and apply defaults
        let mut final_vars = HashMap::new();

        for (var_name, var_def) in &self.spec.server_variables {
            // Check if user provided a value
            if let Some(provided_value) = resolved_vars.get(var_name) {
                // Validate provided value against enum constraints
                Self::validate_enum_constraint(var_name, provided_value, var_def)?;
                final_vars.insert(var_name.clone(), provided_value.clone());
                continue;
            }

            // Check if there's a default value
            if let Some(default_value) = &var_def.default {
                // Validate default value against enum constraints
                Self::validate_enum_constraint(var_name, default_value, var_def)?;
                // Use default value
                final_vars.insert(var_name.clone(), default_value.clone());
                continue;
            }

            // Required variable with no default - this is an error
            return Err(Error::missing_server_variable(var_name));
        }

        // Check for unknown variables provided by user
        for provided_var in resolved_vars.keys() {
            if !self.spec.server_variables.contains_key(provided_var) {
                return Err(Error::unknown_server_variable(
                    provided_var,
                    &self
                        .spec
                        .server_variables
                        .keys()
                        .cloned()
                        .collect::<Vec<_>>(),
                ));
            }
        }

        Ok(final_vars)
    }

    /// Substitutes server variables in a URL template
    ///
    /// # Arguments
    /// * `url_template` - URL with template variables like `<https://{region}.api.com>`
    /// * `variables` - Resolved variable values from `resolve_variables`
    ///
    /// # Returns
    /// * `Ok(String)` - URL with all variables substituted
    /// * `Err(Error)` - If template contains variables not in the provided map
    ///
    /// # Errors
    /// Returns errors for:
    /// - Unresolved template variables not found in the provided variables map
    /// - Invalid template variable names (malformed or too long)
    pub fn substitute_url(
        &self,
        url_template: &str,
        variables: &HashMap<String, String>,
    ) -> Result<String, Error> {
        let mut result = url_template.to_string();
        let mut start = 0;

        while let Some((open_pos, close_pos)) = find_next_template(&result, start) {
            let var_name = &result[open_pos + 1..close_pos];
            Self::validate_template_variable_name(var_name)?;

            let value = Self::get_variable_value(var_name, variables, url_template)?;

            // Perform minimal URL encoding to preserve URL structure while escaping dangerous characters
            // We don't encode forward slashes as server variables often contain path segments
            let encoded_value = Self::encode_server_variable(value);

            result.replace_range(open_pos..=close_pos, &encoded_value);
            start = open_pos + encoded_value.len();
        }

        Ok(result)
    }

    /// Gets the value for a template variable, returning an error if not found
    fn get_variable_value<'b>(
        var_name: &str,
        variables: &'b HashMap<String, String>,
        url_template: &str,
    ) -> Result<&'b String, Error> {
        variables
            .get(var_name)
            .ok_or_else(|| Error::unresolved_template_variable(var_name, url_template))
    }

    /// Parses a key=value string from CLI arguments
    fn parse_key_value(arg: &str) -> Result<(String, String), Error> {
        let Some(eq_pos) = arg.find('=') else {
            return Err(Error::invalid_server_var_format(
                arg,
                "Expected format: key=value",
            ));
        };

        let key = arg[..eq_pos].trim();
        let value = arg[eq_pos + 1..].trim();

        if key.is_empty() {
            return Err(Error::invalid_server_var_format(arg, "Empty variable name"));
        }

        if value.is_empty() {
            return Err(Error::invalid_server_var_format(
                arg,
                "Empty variable value",
            ));
        }

        Ok((key.to_string(), value.to_string()))
    }

    /// Validates a value against enum constraints if defined
    fn validate_enum_constraint(
        var_name: &str,
        value: &str,
        var_def: &ServerVariable,
    ) -> Result<(), Error> {
        if !var_def.enum_values.is_empty() && !var_def.enum_values.contains(&value.to_string()) {
            return Err(Error::invalid_server_var_value(
                var_name,
                value,
                &var_def.enum_values,
            ));
        }
        Ok(())
    }

    /// Validates a template variable name according to `OpenAPI` identifier rules
    fn validate_template_variable_name(name: &str) -> Result<(), Error> {
        if name.is_empty() {
            return Err(Error::invalid_server_var_format(
                "{}",
                "Empty template variable name",
            ));
        }

        if name.len() > 64 {
            return Err(Error::invalid_server_var_format(
                format!("{{{name}}}"),
                "Template variable name too long (max 64 chars)",
            ));
        }

        // OpenAPI identifier rules: must start with letter or underscore,
        // followed by letters, digits, or underscores
        let mut chars = name.chars();
        let Some(first_char) = chars.next() else {
            return Ok(()); // Already checked for empty above
        };

        if !first_char.is_ascii_alphabetic() && first_char != '_' {
            return Err(Error::invalid_server_var_format(
                format!("{{{name}}}"),
                "Template variable names must start with a letter or underscore",
            ));
        }

        for char in chars {
            if !char.is_ascii_alphanumeric() && char != '_' {
                return Err(Error::invalid_server_var_format(
                    format!("{{{name}}}"),
                    "Template variable names must contain only letters, digits, or underscores",
                ));
            }
        }

        Ok(())
    }

    /// Encodes a server variable value for safe inclusion in URLs
    /// This performs selective encoding to preserve URL structure while escaping problematic characters
    fn encode_server_variable(value: &str) -> String {
        // Characters that should be encoded in server variable values
        // We preserve forward slashes as they're often used in path segments
        // but encode other special characters that could break URL parsing
        value
            .chars()
            .map(|c| match c {
                // Preserve forward slashes and common URL-safe characters
                '/' | '-' | '_' | '.' | '~' => c.to_string(),
                // Encode spaces and other special characters
                ' ' => "%20".to_string(),
                '?' => "%3F".to_string(),
                '#' => "%23".to_string(),
                '[' => "%5B".to_string(),
                ']' => "%5D".to_string(),
                '@' => "%40".to_string(),
                '!' => "%21".to_string(),
                '$' => "%24".to_string(),
                '&' => "%26".to_string(),
                '\'' => "%27".to_string(),
                '(' => "%28".to_string(),
                ')' => "%29".to_string(),
                '*' => "%2A".to_string(),
                '+' => "%2B".to_string(),
                ',' => "%2C".to_string(),
                ';' => "%3B".to_string(),
                '=' => "%3D".to_string(),
                '{' => "%7B".to_string(),
                '}' => "%7D".to_string(),
                // Keep alphanumeric and other unreserved characters as-is
                c if c.is_ascii_alphanumeric() => c.to_string(),
                // Encode any other characters
                c => urlencoding::encode(&c.to_string()).to_string(),
            })
            .collect()
    }
}

/// Finds the next template variable boundaries (opening and closing braces)
fn find_next_template(s: &str, start: usize) -> Option<(usize, usize)> {
    let open_pos = s[start..].find('{').map(|pos| start + pos)?;
    let close_pos = s[open_pos..].find('}').map(|pos| open_pos + pos)?;
    Some((open_pos, close_pos))
}

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

    fn create_test_spec_with_variables() -> CachedSpec {
        let mut server_variables = HashMap::new();

        // Required variable with enum constraint
        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()),
            },
        );

        // Required variable without default
        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: "test-api".to_string(),
            version: "1.0.0".to_string(),
            commands: vec![],
            base_url: Some("https://{region}-{env}.api.example.com".to_string()),
            servers: vec!["https://{region}-{env}.api.example.com".to_string()],
            security_schemes: HashMap::new(),
            skipped_endpoints: vec![],
            server_variables,
        }
    }

    #[test]
    fn test_resolve_variables_with_all_provided() {
        let spec = create_test_spec_with_variables();
        let resolver = ServerVariableResolver::new(&spec);

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

        assert_eq!(result.get("region"), Some(&"eu".to_string()));
        assert_eq!(result.get("env"), Some(&"staging".to_string()));
    }

    #[test]
    fn test_resolve_variables_with_defaults() {
        let spec = create_test_spec_with_variables();
        let resolver = ServerVariableResolver::new(&spec);

        let args = vec!["env=prod".to_string()]; // Only provide required var, let region use default
        let result = resolver.resolve_variables(&args).unwrap();

        assert_eq!(result.get("region"), Some(&"us".to_string())); // Default value
        assert_eq!(result.get("env"), Some(&"prod".to_string()));
    }

    #[test]
    fn test_invalid_enum_value() {
        let spec = create_test_spec_with_variables();
        let resolver = ServerVariableResolver::new(&spec);

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

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

    #[test]
    fn test_missing_required_variable() {
        let spec = create_test_spec_with_variables();
        let resolver = ServerVariableResolver::new(&spec);

        let args = vec!["region=us".to_string()]; // Missing required 'env'
        let result = resolver.resolve_variables(&args);

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

    #[test]
    fn test_unknown_variable() {
        let spec = create_test_spec_with_variables();
        let resolver = ServerVariableResolver::new(&spec);

        let args = vec![
            "region=us".to_string(),
            "env=prod".to_string(),
            "unknown=value".to_string(),
        ];
        let result = resolver.resolve_variables(&args);

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

    #[test]
    fn test_invalid_format() {
        let spec = create_test_spec_with_variables();
        let resolver = ServerVariableResolver::new(&spec);

        let args = vec!["invalid-format".to_string()];
        let result = resolver.resolve_variables(&args);

        assert!(result.is_err());
        match result.unwrap_err() {
            Error::Internal {
                kind: ErrorKind::ServerVariable,
                ..
            } => {
                // Expected
            }
            _ => panic!("Expected Internal ServerVariable error"),
        }
    }

    #[test]
    fn test_substitute_url() {
        let spec = create_test_spec_with_variables();
        let resolver = ServerVariableResolver::new(&spec);

        let mut variables = HashMap::new();
        variables.insert("region".to_string(), "eu".to_string());
        variables.insert("env".to_string(), "staging".to_string());

        let result = resolver
            .substitute_url("https://{region}-{env}.api.example.com", &variables)
            .unwrap();
        assert_eq!(result, "https://eu-staging.api.example.com");
    }

    #[test]
    fn test_substitute_url_missing_variable() {
        let spec = create_test_spec_with_variables();
        let resolver = ServerVariableResolver::new(&spec);

        let mut variables = HashMap::new();
        variables.insert("region".to_string(), "eu".to_string());
        // Missing 'env' variable

        let result = resolver.substitute_url("https://{region}-{env}.api.example.com", &variables);

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

    #[test]
    fn test_template_variable_name_validation_empty() {
        let spec = create_test_spec_with_variables();
        let resolver = ServerVariableResolver::new(&spec);

        let variables = HashMap::new();
        let result = resolver.substitute_url("https://{}.api.example.com", &variables);

        assert!(result.is_err());
        match result.unwrap_err() {
            Error::Internal {
                kind: ErrorKind::ServerVariable,
                message,
                ..
            } => {
                assert!(message.contains("Empty template variable name") || message.contains("{}"));
            }
            _ => panic!("Expected Internal ServerVariable error"),
        }
    }

    #[test]
    fn test_template_variable_name_validation_invalid_chars() {
        let spec = create_test_spec_with_variables();
        let resolver = ServerVariableResolver::new(&spec);

        let variables = HashMap::new();
        let result = resolver.substitute_url("https://{invalid-name}.api.example.com", &variables);

        assert!(result.is_err());
        match result.unwrap_err() {
            Error::Internal {
                kind: ErrorKind::ServerVariable,
                message,
                ..
            } => {
                assert!(
                    message.contains("invalid-name")
                        || message.contains("letters, digits, or underscores")
                );
            }
            _ => panic!("Expected Internal ServerVariable error"),
        }
    }

    #[test]
    fn test_template_variable_name_validation_too_long() {
        let spec = create_test_spec_with_variables();
        let resolver = ServerVariableResolver::new(&spec);

        let long_name = "a".repeat(65); // Longer than 64 chars
        let variables = HashMap::new();
        let result = resolver.substitute_url(
            &format!("https://{{{long_name}}}.api.example.com"),
            &variables,
        );

        assert!(result.is_err());
        match result.unwrap_err() {
            Error::Internal {
                kind: ErrorKind::ServerVariable,
                message,
                ..
            } => {
                assert!(message.contains("too long"));
            }
            _ => panic!("Expected Internal ServerVariable error"),
        }
    }

    #[test]
    fn test_template_variable_name_validation_valid_names() {
        let spec = create_test_spec_with_variables();
        let resolver = ServerVariableResolver::new(&spec);

        let mut variables = HashMap::new();
        variables.insert("valid_name".to_string(), "test".to_string());
        variables.insert("_underscore".to_string(), "test".to_string());
        variables.insert("name123".to_string(), "test".to_string());

        // These should all pass validation (though they'll fail with UnresolvedTemplateVariable)
        let test_cases = vec![
            "https://{valid_name}.api.com",
            "https://{_underscore}.api.com",
            "https://{name123}.api.com",
        ];

        for test_case in test_cases {
            let result = resolver.substitute_url(test_case, &variables);
            // Should not fail with InvalidServerVarFormat
            if let Err(Error::Internal {
                kind: ErrorKind::ServerVariable,
                ..
            }) = result
            {
                panic!("Template variable name validation failed for: {test_case}");
            }
        }
    }

    #[test]
    fn test_empty_default_value() {
        let mut server_variables = HashMap::new();

        // Variable with empty string default
        server_variables.insert(
            "prefix".to_string(),
            ServerVariable {
                default: Some(String::new()),
                enum_values: vec![],
                description: Some("Optional prefix".to_string()),
            },
        );

        let spec = CachedSpec {
            cache_format_version: crate::cache::models::CACHE_FORMAT_VERSION,
            name: "test-api".to_string(),
            version: "1.0.0".to_string(),
            commands: vec![],
            base_url: Some("https://{prefix}api.example.com".to_string()),
            servers: vec!["https://{prefix}api.example.com".to_string()],
            security_schemes: HashMap::new(),
            skipped_endpoints: vec![],
            server_variables,
        };

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

        // Test with no args - should use empty string default
        let result = resolver.resolve_variables(&[]).unwrap();
        assert_eq!(result.get("prefix"), Some(&String::new()));

        // Test substitution with empty string default
        let url = resolver
            .substitute_url("https://{prefix}api.example.com", &result)
            .unwrap();
        assert_eq!(url, "https://api.example.com");

        // Test with explicit override
        let args = vec!["prefix=staging-".to_string()];
        let result = resolver.resolve_variables(&args).unwrap();
        assert_eq!(result.get("prefix"), Some(&"staging-".to_string()));

        let url = resolver
            .substitute_url("https://{prefix}api.example.com", &result)
            .unwrap();
        assert_eq!(url, "https://staging-api.example.com");
    }

    #[test]
    fn test_url_encoding_in_substitution() {
        let mut server_variables = HashMap::new();
        server_variables.insert(
            "path".to_string(),
            ServerVariable {
                default: Some("api/v1".to_string()),
                enum_values: vec![],
                description: Some("API path".to_string()),
            },
        );

        let spec = CachedSpec {
            cache_format_version: crate::cache::models::CACHE_FORMAT_VERSION,
            name: "test-api".to_string(),
            version: "1.0.0".to_string(),
            commands: vec![],
            base_url: Some("https://example.com/{path}".to_string()),
            servers: vec!["https://example.com/{path}".to_string()],
            security_schemes: HashMap::new(),
            skipped_endpoints: vec![],
            server_variables,
        };

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

        // Test with value containing special characters
        let args = vec!["path=api/v2/test&debug=true".to_string()];
        let result = resolver.resolve_variables(&args).unwrap();

        let url = resolver
            .substitute_url("https://example.com/{path}", &result)
            .unwrap();

        // The ampersand and equals sign should be URL-encoded, but forward slashes preserved
        assert_eq!(url, "https://example.com/api/v2/test%26debug%3Dtrue");

        // Test with spaces
        let args = vec!["path=api/test endpoint".to_string()];
        let result = resolver.resolve_variables(&args).unwrap();

        let url = resolver
            .substitute_url("https://example.com/{path}", &result)
            .unwrap();

        // Spaces should be encoded as %20, but forward slashes preserved
        assert_eq!(url, "https://example.com/api/test%20endpoint");

        // Test with various special characters
        let args = vec!["path=test?query=1#anchor".to_string()];
        let result = resolver.resolve_variables(&args).unwrap();

        let url = resolver
            .substitute_url("https://example.com/{path}", &result)
            .unwrap();

        // Query and anchor characters should be encoded
        assert_eq!(url, "https://example.com/test%3Fquery%3D1%23anchor");
    }
}