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
#![cfg(feature = "integration")]
// These lints are overly pedantic for test code
#![allow(clippy::too_many_lines)]

use aperture_cli::config::context_name::ApiContextName;
use aperture_cli::config::manager::ConfigManager;

/// Helper to create a validated `ApiContextName` from a string literal in tests
fn name(s: &str) -> ApiContextName {
    ApiContextName::new(s).expect("test name should be valid")
}
use aperture_cli::fs::FileSystem;
mod common;

use common::aperture_cmd;
use predicates::prelude::*;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

struct TestFS {
    temp_dir: TempDir,
}

impl TestFS {
    fn new() -> Self {
        Self {
            temp_dir: TempDir::new().unwrap(),
        }
    }

    fn write_spec(&self, content: &str) -> PathBuf {
        let spec_path = self.temp_dir.path().join("test-spec.yaml");
        fs::write(&spec_path, content).unwrap();
        spec_path
    }

    fn config_dir(&self) -> PathBuf {
        self.temp_dir.path().join(".config")
    }
}

impl FileSystem for TestFS {
    fn read_to_string(&self, path: &std::path::Path) -> std::io::Result<String> {
        fs::read_to_string(path)
    }

    fn write_all(&self, path: &std::path::Path, contents: &[u8]) -> std::io::Result<()> {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }
        fs::write(path, contents)
    }

    fn exists(&self, path: &std::path::Path) -> bool {
        path.exists()
    }

    fn create_dir_all(&self, path: &std::path::Path) -> std::io::Result<()> {
        fs::create_dir_all(path)
    }

    fn remove_file(&self, path: &std::path::Path) -> std::io::Result<()> {
        fs::remove_file(path)
    }

    fn remove_dir_all(&self, path: &std::path::Path) -> std::io::Result<()> {
        fs::remove_dir_all(path)
    }

    fn is_dir(&self, path: &std::path::Path) -> bool {
        path.is_dir()
    }

    fn is_file(&self, path: &std::path::Path) -> bool {
        path.is_file()
    }

    fn canonicalize(&self, path: &std::path::Path) -> std::io::Result<PathBuf> {
        path.canonicalize()
    }

    fn read_dir(&self, path: &std::path::Path) -> std::io::Result<Vec<PathBuf>> {
        Ok(fs::read_dir(path)?
            .filter_map(std::result::Result::ok)
            .map(|entry| entry.path())
            .collect())
    }
}

#[test]
fn test_add_spec_with_parameter_references() {
    let fs = TestFS::new();
    let spec_with_refs = r"
openapi: 3.0.0
info:
  title: Test API with Parameter References
  version: 1.0.0
servers:
  - url: https://api.example.com
components:
  parameters:
    userId:
      name: userId
      in: path
      required: true
      description: Unique identifier of the user
      schema:
        type: string
        format: uuid
    limit:
      name: limit
      in: query
      required: false
      description: Maximum number of items to return
      schema:
        type: integer
        default: 10
        minimum: 1
        maximum: 100
paths:
  /users/{userId}:
    get:
      operationId: getUserById
      summary: Get user by ID
      tags:
        - users
      parameters:
        - $ref: '#/components/parameters/userId'
      responses:
        '200':
          description: User found
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                  name:
                    type: string
  /users:
    get:
      operationId: getUsers
      summary: List all users
      tags:
        - users
      parameters:
        - $ref: '#/components/parameters/limit'
      responses:
        '200':
          description: List of users
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    id:
                      type: string
                    name:
                      type: string
";

    let spec_path = fs.write_spec(spec_with_refs);
    let config_dir = fs.config_dir();
    let cache_file = config_dir.join(".cache").join("test-api.bin");
    let config_manager = ConfigManager::with_fs(fs, config_dir);

    // Should successfully add the spec with parameter references
    let result = config_manager.add_spec(&name("test-api"), &spec_path, false, true);
    assert!(
        result.is_ok(),
        "Should successfully add spec with parameter references: {:?}",
        result.err()
    );

    // Verify the spec was cached
    assert!(cache_file.exists(), "Cache file should exist");

    // Load and verify the cached spec
    let cached_content = std::fs::read(&cache_file).unwrap();
    let cached_spec: aperture_cli::cache::models::CachedSpec =
        postcard::from_bytes(&cached_content).unwrap();

    // Verify commands were created with resolved parameters
    assert_eq!(cached_spec.commands.len(), 2);

    // Check getUserById command
    let get_user_cmd = cached_spec
        .commands
        .iter()
        .find(|c| c.operation_id == "getUserById")
        .expect("getUserById command should exist");

    assert_eq!(get_user_cmd.parameters.len(), 1);
    let user_id_param = &get_user_cmd.parameters[0];
    assert_eq!(user_id_param.name, "userId");
    assert_eq!(user_id_param.location, "path");
    assert!(user_id_param.required);
    assert_eq!(
        user_id_param.description,
        Some("Unique identifier of the user".to_string())
    );

    // Check getUsers command
    let get_users_cmd = cached_spec
        .commands
        .iter()
        .find(|c| c.operation_id == "getUsers")
        .expect("getUsers command should exist");

    assert_eq!(get_users_cmd.parameters.len(), 1);
    let limit_param = &get_users_cmd.parameters[0];
    assert_eq!(limit_param.name, "limit");
    assert_eq!(limit_param.location, "query");
    assert!(!limit_param.required);
    assert_eq!(
        limit_param.description,
        Some("Maximum number of items to return".to_string())
    );
    assert_eq!(limit_param.default_value, Some("10".to_string()));
}

#[test]
fn test_parameter_references_with_special_characters() {
    let fs = TestFS::new();
    let spec_with_special_chars = r"
openapi: 3.0.0
info:
  title: Test API with Special Parameter Names
  version: 1.0.0
servers:
  - url: https://api.example.com
components:
  parameters:
    user-id:
      name: user-id
      in: path
      required: true
      description: User identifier with hyphen
      schema:
        type: string
    user.name:
      name: user.name
      in: query
      required: false
      description: User name with dot
      schema:
        type: string
    user_email:
      name: user_email
      in: query
      required: false
      description: User email with underscore
      schema:
        type: string
        format: email
    search%20query:
      name: search query
      in: query
      required: false
      description: Search query with URL encoded name
      schema:
        type: string
paths:
  /users/{user-id}:
    get:
      operationId: getUserWithSpecialParams
      tags:
        - users
      parameters:
        - $ref: '#/components/parameters/user-id'
        - $ref: '#/components/parameters/user.name'
        - $ref: '#/components/parameters/user_email'
        - $ref: '#/components/parameters/search%20query'
      responses:
        '200':
          description: User details
          content:
            application/json:
              schema:
                type: object
";

    let spec_path = fs.write_spec(spec_with_special_chars);
    let config_dir = fs.config_dir();
    let cache_file = config_dir.join(".cache").join("test-special-api.bin");
    let config_manager = ConfigManager::with_fs(fs, config_dir);

    // Should successfully add the spec with special parameter names
    let result = config_manager.add_spec(&name("test-special-api"), &spec_path, false, true);
    assert!(
        result.is_ok(),
        "Should successfully add spec with special parameter names: {:?}",
        result.err()
    );

    // Verify the spec was cached
    assert!(cache_file.exists(), "Cache file should exist");

    // Load and verify the cached spec
    let cached_content = std::fs::read(&cache_file).unwrap();
    let cached_spec: aperture_cli::cache::models::CachedSpec =
        postcard::from_bytes(&cached_content).unwrap();

    // Verify command was created with all special parameters
    assert_eq!(cached_spec.commands.len(), 1);
    let cmd = &cached_spec.commands[0];
    assert_eq!(cmd.operation_id, "getUserWithSpecialParams");
    assert_eq!(cmd.parameters.len(), 4);

    // Check parameters were resolved correctly
    let param_names: Vec<&str> = cmd.parameters.iter().map(|p| p.name.as_str()).collect();

    assert!(
        param_names.contains(&"user-id"),
        "Should contain user-id parameter"
    );
    assert!(
        param_names.contains(&"user.name"),
        "Should contain user.name parameter"
    );
    assert!(
        param_names.contains(&"user_email"),
        "Should contain user_email parameter"
    );
    assert!(
        param_names.contains(&"search query"),
        "Should contain search query parameter"
    );

    // Verify parameter details
    let user_id_param = cmd.parameters.iter().find(|p| p.name == "user-id").unwrap();
    assert_eq!(user_id_param.location, "path");
    assert!(user_id_param.required);
    assert_eq!(
        user_id_param.description,
        Some("User identifier with hyphen".to_string())
    );

    let search_param = cmd
        .parameters
        .iter()
        .find(|p| p.name == "search query")
        .unwrap();
    assert_eq!(search_param.location, "query");
    assert!(!search_param.required);
    assert_eq!(
        search_param.description,
        Some("Search query with URL encoded name".to_string())
    );
}

#[test]
fn test_cli_with_parameter_references() {
    let temp_dir = TempDir::new().unwrap();
    let config_dir = temp_dir.path().join(".aperture");
    let spec_path = temp_dir.path().join("api-with-refs.yaml");

    // Write OpenAPI spec with parameter references
    fs::write(
        &spec_path,
        r"
openapi: 3.0.0
info:
  title: Test API
  version: 1.0.0
servers:
  - url: https://api.example.com
components:
  parameters:
    petId:
      name: petId
      in: path
      required: true
      description: ID of the pet
      schema:
        type: integer
paths:
  /pets/{petId}:
    get:
      operationId: getPetById
      tags:
        - pets
      parameters:
        - $ref: '#/components/parameters/petId'
      responses:
        '200':
          description: Pet details
",
    )
    .unwrap();

    // Add the spec using the CLI
    let output = aperture_cmd()
        .env("APERTURE_CONFIG_DIR", config_dir.to_str().unwrap())
        .args(["config", "add", "test-api", spec_path.to_str().unwrap()])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    assert!(
        output.status.success(),
        "Command should succeed. stdout: {stdout}, stderr: {stderr}"
    );

    // Verify the spec was added
    aperture_cmd()
        .env("APERTURE_CONFIG_DIR", config_dir.to_str().unwrap())
        .args(["config", "list"])
        .assert()
        .success()
        .stdout(predicate::str::contains("test-api"));

    // Verify we can use the generated command with the resolved parameter
    // Check if the command structure is correct by looking at the error output
    let output = aperture_cmd()
        .env("APERTURE_CONFIG_DIR", config_dir.to_str().unwrap())
        .args(["api", "test-api", "pets", "get-pet-by-id", "--help"])
        .output()
        .unwrap();

    // The command might be showing help on stderr with exit code 1
    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Check that parameter was resolved correctly
    assert!(
        stdout.contains("--pet-id") || stderr.contains("--pet-id"),
        "Output should contain --pet-id parameter. stdout: {stdout}, stderr: {stderr}"
    );
    assert!(
        stdout.contains("ID of the pet")
            || stderr.contains("ID of the pet")
            || stdout.contains("Path parameter: petId")
            || stderr.contains("Path parameter: petId"),
        "Output should contain parameter description. stdout: {stdout}, stderr: {stderr}"
    );
}

#[test]
fn test_invalid_parameter_reference_rejected() {
    let fs = TestFS::new();
    let invalid_spec = r"
openapi: 3.0.0
info:
  title: Test API
  version: 1.0.0
servers:
  - url: https://api.example.com
paths:
  /users/{userId}:
    get:
      operationId: getUserById
      parameters:
        - $ref: '#/components/parameters/nonExistentParam'
      responses:
        '200':
          description: Success
";

    let spec_path = fs.write_spec(invalid_spec);
    let config_dir = fs.config_dir();
    let config_manager = ConfigManager::with_fs(fs, config_dir);

    // Should fail to add the spec with invalid reference
    let result = config_manager.add_spec(&name("test-api"), &spec_path, false, true);
    assert!(
        result.is_err(),
        "Should fail to add spec with invalid parameter reference"
    );

    match result.unwrap_err() {
        aperture_cli::error::Error::Internal {
            kind: aperture_cli::error::ErrorKind::Validation,
            message: msg,
            ..
        } => {
            assert!(
                msg.contains("not found in components") || msg.contains("no components section"),
                "Error should mention missing parameter. Actual error: {msg}"
            );
        }
        _ => panic!("Expected Validation error"),
    }
}

#[test]
fn test_cli_with_special_character_parameters() {
    let temp_dir = TempDir::new().unwrap();
    let config_dir = temp_dir.path().join(".aperture");
    let spec_path = temp_dir.path().join("api-special-chars.yaml");

    // Write OpenAPI spec with special character parameters
    fs::write(
        &spec_path,
        r"
openapi: 3.0.0
info:
  title: Test API Special Chars
  version: 1.0.0
servers:
  - url: https://api.example.com
components:
  parameters:
    user-id:
      name: user-id
      in: path
      required: true
      schema:
        type: string
    include.fields:
      name: include.fields
      in: query
      required: false
      schema:
        type: string
paths:
  /users/{user-id}:
    get:
      operationId: getUser
      tags:
        - users
      parameters:
        - $ref: '#/components/parameters/user-id'
        - $ref: '#/components/parameters/include.fields'
      responses:
        '200':
          description: User details
",
    )
    .unwrap();

    // Add the spec
    let mut cmd = aperture_cmd();
    cmd.env("APERTURE_CONFIG_DIR", config_dir.to_str().unwrap())
        .args(["config", "add", "special-api", spec_path.to_str().unwrap()])
        .assert()
        .success();

    // Test the help output shows the special parameters correctly
    let mut cmd = aperture_cmd();
    let output = cmd
        .env("APERTURE_CONFIG_DIR", config_dir.to_str().unwrap())
        .args(["api", "special-api", "users", "get-user", "--help"])
        .output()
        .unwrap();

    // Help might be in stdout or stderr
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let combined = format!("{stdout}{stderr}");

    assert!(
        combined.contains("--user-id") && combined.contains("--include-fields"),
        "Help should show both special character parameters"
    );

    // Test dry-run with special character parameters
    let mut cmd = aperture_cmd();
    let output = cmd
        .env("APERTURE_CONFIG_DIR", config_dir.to_str().unwrap())
        .args([
            "api",
            "special-api",
            "--dry-run",
            "users",
            "get-user",
            "--user-id",
            "123",
            "--include-fields",
            "name,email",
        ])
        .output()
        .unwrap();

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        panic!("Command failed with stderr: {stderr}");
    }

    let stdout = String::from_utf8(output.stdout).unwrap();

    // The URL might have the parameter in the path
    assert!(
        stdout.contains("https://api.example.com/users/123"),
        "Should contain the URL with path parameter. Actual output: {stdout}"
    );

    // Query parameters might be shown separately or URL-encoded
    assert!(
        stdout.contains("include.fields") || stdout.contains("include%2Efields"),
        "Should contain the query parameter. Actual output: {stdout}"
    );
}