sekkei 0.1.1

Sekkei (設計) — canonical OpenAPI 3.0 serde types, multi-format loading, and ref resolution
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
//! `SpecVisitor` trait and `walk_spec` for walking `OpenAPI` specs.

use crate::types::{Operation, Parameter, PathItem, Schema};
use crate::OpenApiSpec;

/// Visitor trait for walking `OpenAPI` specs.
/// Implement specific methods to process only what you need.
/// Default implementations are no-ops, enabling selective visiting.
pub trait SpecVisitor {
    /// Called for each path item in the spec.
    fn visit_path(&mut self, _path: &str, _item: &PathItem) {}

    /// Called for each operation (GET, POST, PUT, DELETE, PATCH) in a path.
    fn visit_operation(&mut self, _method: &str, _path: &str, _op: &Operation) {}

    /// Called for each named schema in `components.schemas`.
    fn visit_schema(&mut self, _name: &str, _schema: &Schema) {}

    /// Called for each parameter (both path-level and operation-level).
    fn visit_parameter(&mut self, _param: &Parameter) {}
}

/// Walk a spec, calling visitor methods for each element.
pub fn walk_spec(spec: &OpenApiSpec, visitor: &mut dyn SpecVisitor) {
    for (path, item) in &spec.paths {
        visitor.visit_path(path, item);
        for (method, op) in item.operations() {
            visitor.visit_operation(method, path, op);
            for param in &op.parameters {
                visitor.visit_parameter(param);
            }
        }
        for param in &item.parameters {
            visitor.visit_parameter(param);
        }
    }
    if let Some(components) = &spec.components {
        for (name, schema) in &components.schemas {
            visitor.visit_schema(name, schema);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_fixtures::FULL_SPEC_YAML;

    #[test]
    fn visitor_counts_operations() {
        struct Counter {
            ops: usize,
            schemas: usize,
        }
        impl SpecVisitor for Counter {
            fn visit_operation(&mut self, _: &str, _: &str, _: &Operation) {
                self.ops += 1;
            }
            fn visit_schema(&mut self, _: &str, _: &Schema) {
                self.schemas += 1;
            }
        }
        let spec: OpenApiSpec = serde_yaml_ng::from_str(FULL_SPEC_YAML).unwrap();
        let mut counter = Counter {
            ops: 0,
            schemas: 0,
        };
        walk_spec(&spec, &mut counter);
        assert_eq!(counter.ops, 4); // listPets, createPet, getPet, deletePet
        assert_eq!(counter.schemas, 3); // Pet, PetStatus, CreatePetRequest
    }

    #[test]
    fn visitor_counts_paths() {
        struct PathCounter {
            paths: usize,
        }
        impl SpecVisitor for PathCounter {
            fn visit_path(&mut self, _: &str, _: &PathItem) {
                self.paths += 1;
            }
        }
        let spec: OpenApiSpec = serde_yaml_ng::from_str(FULL_SPEC_YAML).unwrap();
        let mut counter = PathCounter { paths: 0 };
        walk_spec(&spec, &mut counter);
        assert_eq!(counter.paths, 2); // /pets, /pets/{petId}
    }

    #[test]
    fn visitor_counts_parameters() {
        struct ParamCounter {
            params: usize,
        }
        impl SpecVisitor for ParamCounter {
            fn visit_parameter(&mut self, _: &Parameter) {
                self.params += 1;
            }
        }
        let spec: OpenApiSpec = serde_yaml_ng::from_str(FULL_SPEC_YAML).unwrap();
        let mut counter = ParamCounter { params: 0 };
        walk_spec(&spec, &mut counter);
        // 1 operation-level param (limit on GET /pets) + 1 path-level param (petId on /pets/{petId})
        assert_eq!(counter.params, 2);
    }

    #[test]
    fn visitor_collects_operation_ids() {
        struct IdCollector {
            ids: Vec<String>,
        }
        impl SpecVisitor for IdCollector {
            fn visit_operation(&mut self, _: &str, _: &str, op: &Operation) {
                if let Some(id) = &op.operation_id {
                    self.ids.push(id.clone());
                }
            }
        }
        let spec: OpenApiSpec = serde_yaml_ng::from_str(FULL_SPEC_YAML).unwrap();
        let mut collector = IdCollector { ids: Vec::new() };
        walk_spec(&spec, &mut collector);
        assert!(collector.ids.contains(&"listPets".to_string()));
        assert!(collector.ids.contains(&"createPet".to_string()));
        assert!(collector.ids.contains(&"getPet".to_string()));
        assert!(collector.ids.contains(&"deletePet".to_string()));
    }

    #[test]
    fn visitor_collects_methods() {
        struct MethodCollector {
            methods: Vec<String>,
        }
        impl SpecVisitor for MethodCollector {
            fn visit_operation(&mut self, method: &str, _: &str, _: &Operation) {
                self.methods.push(method.to_string());
            }
        }
        let spec: OpenApiSpec = serde_yaml_ng::from_str(FULL_SPEC_YAML).unwrap();
        let mut collector = MethodCollector {
            methods: Vec::new(),
        };
        walk_spec(&spec, &mut collector);
        assert!(collector.methods.contains(&"get".to_string()));
        assert!(collector.methods.contains(&"post".to_string()));
        assert!(collector.methods.contains(&"delete".to_string()));
    }

    #[test]
    fn visitor_empty_spec() {
        struct Counter {
            total: usize,
        }
        impl SpecVisitor for Counter {
            fn visit_path(&mut self, _: &str, _: &PathItem) {
                self.total += 1;
            }
            fn visit_operation(&mut self, _: &str, _: &str, _: &Operation) {
                self.total += 1;
            }
            fn visit_schema(&mut self, _: &str, _: &Schema) {
                self.total += 1;
            }
            fn visit_parameter(&mut self, _: &Parameter) {
                self.total += 1;
            }
        }
        let spec: OpenApiSpec =
            serde_yaml_ng::from_str("info:\n  title: E\n  version: '1'\npaths: {}").unwrap();
        let mut counter = Counter { total: 0 };
        walk_spec(&spec, &mut counter);
        assert_eq!(counter.total, 0);
    }

    #[test]
    fn visitor_noop_default_implementations() {
        // Verifies that the default no-op implementations don't panic.
        struct NoopVisitor;
        impl SpecVisitor for NoopVisitor {}
        let spec: OpenApiSpec = serde_yaml_ng::from_str(FULL_SPEC_YAML).unwrap();
        let mut visitor = NoopVisitor;
        walk_spec(&spec, &mut visitor);
        // If we get here without panic, defaults are safe.
    }

    #[test]
    fn visitor_collects_schema_names() {
        struct SchemaCollector {
            names: Vec<String>,
        }
        impl SpecVisitor for SchemaCollector {
            fn visit_schema(&mut self, name: &str, _: &Schema) {
                self.names.push(name.to_string());
            }
        }
        let spec: OpenApiSpec = serde_yaml_ng::from_str(FULL_SPEC_YAML).unwrap();
        let mut collector = SchemaCollector { names: Vec::new() };
        walk_spec(&spec, &mut collector);
        assert!(collector.names.contains(&"Pet".to_string()));
        assert!(collector.names.contains(&"PetStatus".to_string()));
        assert!(collector.names.contains(&"CreatePetRequest".to_string()));
    }

    #[test]
    fn visitor_receives_correct_path_strings() {
        struct PathCollector {
            paths: Vec<String>,
        }
        impl SpecVisitor for PathCollector {
            fn visit_path(&mut self, path: &str, _: &PathItem) {
                self.paths.push(path.to_string());
            }
        }
        let spec: OpenApiSpec = serde_yaml_ng::from_str(FULL_SPEC_YAML).unwrap();
        let mut collector = PathCollector { paths: Vec::new() };
        walk_spec(&spec, &mut collector);
        assert!(collector.paths.contains(&"/pets".to_string()));
        assert!(collector.paths.contains(&"/pets/{petId}".to_string()));
    }

    #[test]
    fn visitor_receives_method_and_path_for_operations() {
        struct OpCollector {
            entries: Vec<(String, String)>,
        }
        impl SpecVisitor for OpCollector {
            fn visit_operation(&mut self, method: &str, path: &str, _: &Operation) {
                self.entries.push((method.to_string(), path.to_string()));
            }
        }
        let spec: OpenApiSpec = serde_yaml_ng::from_str(FULL_SPEC_YAML).unwrap();
        let mut collector = OpCollector {
            entries: Vec::new(),
        };
        walk_spec(&spec, &mut collector);
        assert!(collector.entries.contains(&("get".to_string(), "/pets".to_string())));
        assert!(collector.entries.contains(&("post".to_string(), "/pets".to_string())));
        assert!(
            collector
                .entries
                .contains(&("get".to_string(), "/pets/{petId}".to_string()))
        );
        assert!(
            collector
                .entries
                .contains(&("delete".to_string(), "/pets/{petId}".to_string()))
        );
    }

    #[test]
    fn visitor_receives_parameter_details() {
        struct ParamCollector {
            names: Vec<String>,
            locations: Vec<String>,
        }
        impl SpecVisitor for ParamCollector {
            fn visit_parameter(&mut self, param: &Parameter) {
                self.names.push(param.name.clone());
                self.locations.push(param.location.clone());
            }
        }
        let spec: OpenApiSpec = serde_yaml_ng::from_str(FULL_SPEC_YAML).unwrap();
        let mut collector = ParamCollector {
            names: Vec::new(),
            locations: Vec::new(),
        };
        walk_spec(&spec, &mut collector);
        assert!(collector.names.contains(&"limit".to_string()));
        assert!(collector.names.contains(&"petId".to_string()));
        assert!(collector.locations.contains(&"query".to_string()));
        assert!(collector.locations.contains(&"path".to_string()));
    }

    #[test]
    fn visitor_schema_receives_type_info() {
        struct SchemaTypeCollector {
            types: Vec<(String, Option<String>)>,
        }
        impl SpecVisitor for SchemaTypeCollector {
            fn visit_schema(&mut self, name: &str, schema: &Schema) {
                self.types
                    .push((name.to_string(), schema.schema_type.clone()));
            }
        }
        let spec: OpenApiSpec = serde_yaml_ng::from_str(FULL_SPEC_YAML).unwrap();
        let mut collector = SchemaTypeCollector { types: Vec::new() };
        walk_spec(&spec, &mut collector);
        assert!(collector.types.contains(&("Pet".to_string(), Some("object".to_string()))));
        assert!(
            collector
                .types
                .contains(&("PetStatus".to_string(), Some("string".to_string())))
        );
    }

    #[test]
    fn visitor_spec_without_components() {
        struct SchemaCounter {
            count: usize,
        }
        impl SpecVisitor for SchemaCounter {
            fn visit_schema(&mut self, _: &str, _: &Schema) {
                self.count += 1;
            }
        }
        let yaml = "info:\n  title: NoComp\n  version: '1'\npaths:\n  /test:\n    get:\n      responses:\n        '200':\n          description: OK";
        let spec: OpenApiSpec = serde_yaml_ng::from_str(yaml).unwrap();
        let mut counter = SchemaCounter { count: 0 };
        walk_spec(&spec, &mut counter);
        assert_eq!(counter.count, 0);
    }

    #[test]
    fn visitor_with_all_http_methods() {
        struct MethodCollector {
            methods: Vec<String>,
        }
        impl SpecVisitor for MethodCollector {
            fn visit_operation(&mut self, method: &str, _: &str, _: &Operation) {
                self.methods.push(method.to_string());
            }
        }
        let yaml = r#"
info:
  title: AllMethods
  version: "1.0.0"
paths:
  /resource:
    get:
      responses:
        "200":
          description: OK
    post:
      responses:
        "201":
          description: Created
    put:
      responses:
        "200":
          description: OK
    delete:
      responses:
        "204":
          description: Deleted
    patch:
      responses:
        "200":
          description: OK
"#;
        let spec: OpenApiSpec = serde_yaml_ng::from_str(yaml).unwrap();
        let mut collector = MethodCollector {
            methods: Vec::new(),
        };
        walk_spec(&spec, &mut collector);
        assert_eq!(collector.methods.len(), 5);
        assert!(collector.methods.contains(&"get".to_string()));
        assert!(collector.methods.contains(&"post".to_string()));
        assert!(collector.methods.contains(&"put".to_string()));
        assert!(collector.methods.contains(&"delete".to_string()));
        assert!(collector.methods.contains(&"patch".to_string()));
    }

    #[test]
    fn visitor_call_order_path_before_operations() {
        #[derive(Debug, PartialEq)]
        enum Event {
            Path(String),
            Op(String, String),
        }
        struct OrderTracker {
            events: Vec<Event>,
        }
        impl SpecVisitor for OrderTracker {
            fn visit_path(&mut self, path: &str, _: &PathItem) {
                self.events.push(Event::Path(path.to_string()));
            }
            fn visit_operation(&mut self, method: &str, path: &str, _: &Operation) {
                self.events
                    .push(Event::Op(method.to_string(), path.to_string()));
            }
        }
        let yaml = r#"
info:
  title: Order
  version: "1.0.0"
paths:
  /a:
    get:
      responses:
        "200":
          description: OK
"#;
        let spec: OpenApiSpec = serde_yaml_ng::from_str(yaml).unwrap();
        let mut tracker = OrderTracker {
            events: Vec::new(),
        };
        walk_spec(&spec, &mut tracker);
        assert_eq!(tracker.events[0], Event::Path("/a".to_string()));
        assert_eq!(
            tracker.events[1],
            Event::Op("get".to_string(), "/a".to_string())
        );
    }

    #[test]
    fn visitor_path_level_and_operation_level_params() {
        struct DetailedParamCollector {
            params: Vec<(String, String)>,
        }
        impl SpecVisitor for DetailedParamCollector {
            fn visit_parameter(&mut self, param: &Parameter) {
                self.params
                    .push((param.name.clone(), param.location.clone()));
            }
        }
        let yaml = r#"
info:
  title: MixedParams
  version: "1.0.0"
paths:
  /items/{id}:
    parameters:
      - name: id
        in: path
        required: true
    get:
      parameters:
        - name: filter
          in: query
      responses:
        "200":
          description: OK
"#;
        let spec: OpenApiSpec = serde_yaml_ng::from_str(yaml).unwrap();
        let mut collector = DetailedParamCollector {
            params: Vec::new(),
        };
        walk_spec(&spec, &mut collector);
        assert_eq!(collector.params.len(), 2);
        assert!(collector.params.contains(&("filter".to_string(), "query".to_string())));
        assert!(collector.params.contains(&("id".to_string(), "path".to_string())));
    }
}