jentic-openapi-traverse 1.0.0a22__tar.gz → 1.0.0a23__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (16) hide show
  1. jentic_openapi_traverse-1.0.0a23/PKG-INFO +491 -0
  2. jentic_openapi_traverse-1.0.0a23/README.md +476 -0
  3. {jentic_openapi_traverse-1.0.0a22 → jentic_openapi_traverse-1.0.0a23}/pyproject.toml +14 -2
  4. jentic_openapi_traverse-1.0.0a23/src/jentic/apitools/openapi/traverse/datamodels/low/__init__.py +14 -0
  5. jentic_openapi_traverse-1.0.0a23/src/jentic/apitools/openapi/traverse/datamodels/low/introspection.py +114 -0
  6. jentic_openapi_traverse-1.0.0a23/src/jentic/apitools/openapi/traverse/datamodels/low/merge.py +111 -0
  7. jentic_openapi_traverse-1.0.0a23/src/jentic/apitools/openapi/traverse/datamodels/low/path.py +145 -0
  8. jentic_openapi_traverse-1.0.0a23/src/jentic/apitools/openapi/traverse/datamodels/low/traversal.py +235 -0
  9. jentic_openapi_traverse-1.0.0a23/src/jentic/apitools/openapi/traverse/json/py.typed +0 -0
  10. jentic_openapi_traverse-1.0.0a22/PKG-INFO +0 -209
  11. jentic_openapi_traverse-1.0.0a22/README.md +0 -196
  12. {jentic_openapi_traverse-1.0.0a22 → jentic_openapi_traverse-1.0.0a23}/LICENSE +0 -0
  13. {jentic_openapi_traverse-1.0.0a22 → jentic_openapi_traverse-1.0.0a23}/NOTICE +0 -0
  14. {jentic_openapi_traverse-1.0.0a22/src/jentic/apitools/openapi/traverse/json → jentic_openapi_traverse-1.0.0a23/src/jentic/apitools/openapi/traverse/datamodels/low}/py.typed +0 -0
  15. {jentic_openapi_traverse-1.0.0a22 → jentic_openapi_traverse-1.0.0a23}/src/jentic/apitools/openapi/traverse/json/__init__.py +0 -0
  16. {jentic_openapi_traverse-1.0.0a22 → jentic_openapi_traverse-1.0.0a23}/src/jentic/apitools/openapi/traverse/json/traversal.py +0 -0
@@ -0,0 +1,491 @@
1
+ Metadata-Version: 2.4
2
+ Name: jentic-openapi-traverse
3
+ Version: 1.0.0a23
4
+ Summary: Jentic OpenAPI Traversal Utilities
5
+ Author: Jentic
6
+ Author-email: Jentic <hello@jentic.com>
7
+ License-Expression: Apache-2.0
8
+ License-File: LICENSE
9
+ License-File: NOTICE
10
+ Requires-Dist: jentic-openapi-datamodels~=1.0.0a23
11
+ Requires-Dist: jsonpointer~=3.0.0
12
+ Requires-Python: >=3.11
13
+ Project-URL: Homepage, https://github.com/jentic/jentic-openapi-tools
14
+ Description-Content-Type: text/markdown
15
+
16
+ # jentic-openapi-traverse
17
+
18
+ A Python library for traversing OpenAPI documents. This package is part of the Jentic OpenAPI Tools ecosystem and provides two types of traversal:
19
+
20
+ 1. **Datamodel Traversal** - OpenAPI-aware semantic traversal with visitor pattern
21
+ 2. **JSON Traversal** - Generic depth-first traversal of JSON-like structures
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ pip install jentic-openapi-traverse
27
+ ```
28
+
29
+ **Prerequisites:**
30
+ - Python 3.11+
31
+
32
+ ---
33
+
34
+ ## Datamodel Traversal
35
+
36
+ OpenAPI-aware semantic traversal using the visitor pattern. Works with low-level datamodels from `jentic-openapi-datamodels` package, preserving source location information and providing structured access to OpenAPI nodes.
37
+
38
+ ### Quick Start
39
+
40
+ ```python
41
+ from jentic.apitools.openapi.parser.core import OpenAPIParser
42
+ from jentic.apitools.openapi.traverse.datamodels.low import traverse, DataModelLowVisitor
43
+
44
+ # Parse OpenAPI document
45
+ parser = OpenAPIParser("datamodel-low")
46
+ doc = parser.parse("file:///path/to/openapi.yaml")
47
+
48
+ # Create visitor
49
+ class OperationCollector(DataModelLowVisitor):
50
+ def __init__(self):
51
+ self.operations = []
52
+
53
+ def visit_Operation(self, path):
54
+ self.operations.append({
55
+ "path": path.format_path(path_format="jsonpointer"),
56
+ "operation_id": path.node.operation_id.value if path.node.operation_id else None
57
+ })
58
+
59
+ # Traverse
60
+ visitor = OperationCollector()
61
+ traverse(doc, visitor)
62
+
63
+ print(f"Found {len(visitor.operations)} operations")
64
+ ```
65
+
66
+ ### Visitor Pattern
67
+
68
+ The datamodel traversal uses a flexible visitor pattern with multiple hook types:
69
+
70
+ #### Hook Methods
71
+
72
+ **Generic hooks** (fire for ALL nodes):
73
+ - `visit_enter(path)` - Called before processing any node
74
+ - `visit_leave(path)` - Called after processing any node and its children
75
+
76
+ **Class-specific hooks** (fire for matching node types):
77
+ - `visit_ClassName(path)` - Main visitor for specific node type
78
+ - `visit_enter_ClassName(path)` - Called before visit_ClassName
79
+ - `visit_leave_ClassName(path)` - Called after children are visited
80
+
81
+ #### Dispatch Order
82
+
83
+ For each node, hooks are called in this order:
84
+ 1. `visit_enter(path)` - generic enter
85
+ 2. `visit_enter_ClassName(path)` - specific enter
86
+ 3. `visit_ClassName(path)` - main visitor
87
+ 4. [child traversal - automatic unless skipped]
88
+ 5. `visit_leave_ClassName(path)` - specific leave
89
+ 6. `visit_leave(path)` - generic leave
90
+
91
+ #### Control Flow
92
+
93
+ Visitor methods control traversal by their return value:
94
+
95
+ - `None` (or no return) - **Continue normally** (children visited automatically)
96
+ - `False` - **Skip children** of this node (but continue to siblings)
97
+ - `BREAK` - **Stop entire traversal** immediately
98
+
99
+ ```python
100
+ from jentic.apitools.openapi.traverse.datamodels.low import traverse, BREAK
101
+
102
+ class ControlFlowExample:
103
+ def visit_PathItem(self, path):
104
+ if path.parent_key == "/internal":
105
+ return False # Skip internal paths and their children
106
+
107
+ def visit_Operation(self, path):
108
+ if some_error_condition:
109
+ return BREAK # Stop entire traversal
110
+ ```
111
+
112
+ ### NodePath Context
113
+
114
+ Every visitor method receives a `NodePath` object with context about the current node:
115
+
116
+ ```python
117
+ class PathInspector:
118
+ def visit_Operation(self, path):
119
+ # Current node
120
+ print(f"Node: {path.node.__class__.__name__}")
121
+
122
+ # Parent information
123
+ print(f"Parent field: {path.parent_field}") # e.g., "get", "post"
124
+ print(f"Parent key: {path.parent_key}") # e.g., "/users" (for path items)
125
+
126
+ # Ancestry
127
+ print(f"Ancestors: {len(path.ancestors)}")
128
+ root = path.get_root()
129
+
130
+ # Path formatting
131
+ print(f"JSONPointer: {path.format_path()}") # /paths/~1users/get
132
+ print(f"JSONPath: {path.format_path(path_format='jsonpath')}") # $['paths']['/users']['get']
133
+ ```
134
+
135
+ ### Enter/Leave Hooks
136
+
137
+ Use enter/leave hooks for pre/post processing logic:
138
+
139
+ ```python
140
+ class DepthTracker(DataModelLowVisitor):
141
+ def __init__(self):
142
+ self.current_depth = 0
143
+ self.max_depth = 0
144
+
145
+ def visit_enter(self, path):
146
+ self.current_depth += 1
147
+ self.max_depth = max(self.max_depth, self.current_depth)
148
+ print(" " * self.current_depth + f"Entering {path.node.__class__.__name__}")
149
+
150
+ def visit_leave(self, path):
151
+ print(" " * self.current_depth + f"Leaving {path.node.__class__.__name__}")
152
+ self.current_depth -= 1
153
+ ```
154
+
155
+ ### Examples
156
+
157
+ #### Collecting All Schemas
158
+
159
+ ```python
160
+ class SchemaCollector(DataModelLowVisitor):
161
+ def __init__(self):
162
+ self.schemas = {}
163
+
164
+ def visit_Schema(self, path):
165
+ schema_name = path.parent_key if path.parent_field == "schemas" else None
166
+ if schema_name:
167
+ self.schemas[schema_name] = path.node
168
+
169
+ visitor = SchemaCollector()
170
+ traverse(doc, visitor)
171
+ print(f"Found {len(visitor.schemas)} schemas")
172
+ ```
173
+
174
+ #### Validating Security Requirements
175
+
176
+ ```python
177
+ class SecurityValidator(DataModelLowVisitor):
178
+ def __init__(self):
179
+ self.errors = []
180
+
181
+ def visit_Operation(self, path):
182
+ if not path.node.security:
183
+ self.errors.append(f"Missing security at {path.format_path()}")
184
+
185
+ def visit_SecurityRequirement(self, path):
186
+ # Validate security requirement
187
+ if not path.node.schemes:
188
+ self.errors.append(f"Empty security requirement at {path.format_path()}")
189
+
190
+ visitor = SecurityValidator()
191
+ traverse(doc, visitor)
192
+ if visitor.errors:
193
+ for error in visitor.errors:
194
+ print(f"Security error: {error}")
195
+ ```
196
+
197
+ #### Finding Deprecated Operations
198
+
199
+ ```python
200
+ class DeprecatedFinder:
201
+ def __init__(self):
202
+ self.deprecated_ops = []
203
+
204
+ def visit_Operation(self, path):
205
+ if path.node.deprecated and path.node.deprecated.value:
206
+ self.deprecated_ops.append({
207
+ "path": path.format_path(),
208
+ "operation_id": path.node.operation_id.value if path.node.operation_id else None,
209
+ "method": path.parent_field
210
+ })
211
+ return False # Skip children (we don't need to go deeper)
212
+
213
+ visitor = DeprecatedFinder()
214
+ traverse(doc, visitor)
215
+ ```
216
+
217
+ #### Early Exit on Error
218
+
219
+ ```python
220
+ class ErrorDetector(DataModelLowVisitor):
221
+ def __init__(self):
222
+ self.error_found = False
223
+ self.error_location = None
224
+
225
+ def visit_Operation(self, path):
226
+ if not path.node.responses:
227
+ self.error_found = True
228
+ self.error_location = path.format_path()
229
+ return BREAK # Stop traversal immediately
230
+ ```
231
+
232
+ ### Merging Multiple Visitors
233
+
234
+ Run multiple visitors in a single traversal pass (parallel visitation) using `merge_visitors`:
235
+
236
+ ```python
237
+ from jentic.apitools.openapi.traverse.datamodels.low import merge_visitors
238
+
239
+ # Create separate visitors
240
+ schema_collector = SchemaCollector()
241
+ security_validator = SecurityValidator()
242
+ deprecated_finder = DeprecatedFinder()
243
+
244
+ # Merge and traverse once
245
+ merged = merge_visitors(schema_collector, security_validator, deprecated_finder)
246
+ traverse(doc, merged)
247
+
248
+ # Each visitor maintains independent state
249
+ print(f"Schemas: {len(schema_collector.schemas)}")
250
+ print(f"Security errors: {len(security_validator.errors)}")
251
+ print(f"Deprecated: {len(deprecated_finder.deprecated_ops)}")
252
+ ```
253
+
254
+ **Per-Visitor Control Flow:**
255
+ - Each visitor can independently skip subtrees or break
256
+ - If `visitor1` returns `False`, only `visitor1` skips children
257
+ - Other visitors continue normally
258
+ - This follows ApiDOM's per-visitor semantics
259
+
260
+ ### Duck Typing
261
+
262
+ You don't need to inherit from `DataModelLowVisitor` - duck typing works:
263
+
264
+ ```python
265
+ class SimpleCounter: # No inheritance
266
+ def __init__(self):
267
+ self.count = 0
268
+
269
+ def visit_Operation(self, path):
270
+ self.count += 1
271
+
272
+ visitor = SimpleCounter()
273
+ traverse(doc, visitor)
274
+ ```
275
+
276
+ The `DataModelLowVisitor` base class is optional and provides no functionality - it's purely for organizational purposes.
277
+
278
+ ### API Reference
279
+
280
+ #### `traverse(root, visitor) -> None`
281
+
282
+ Traverse OpenAPI datamodel tree using visitor pattern.
283
+
284
+ **Parameters:**
285
+ - `root` - Root datamodel object (OpenAPI30, OpenAPI31, or any datamodel node)
286
+ - `visitor` - Object with `visit_*` methods (duck typing)
287
+
288
+ **Returns:**
289
+ - None (traversal is side-effect based)
290
+
291
+ #### `BREAK`
292
+
293
+ Sentinel value to stop traversal immediately. Return this from any visitor method.
294
+
295
+ ```python
296
+ from jentic.apitools.openapi.traverse.datamodels.low import BREAK
297
+
298
+ def visit_Operation(self, path):
299
+ if should_stop:
300
+ return BREAK
301
+ ```
302
+
303
+ #### `merge_visitors(*visitors) -> object`
304
+
305
+ Merge multiple visitors into one composite visitor.
306
+
307
+ **Parameters:**
308
+ - `*visitors` - Variable number of visitor objects
309
+
310
+ **Returns:**
311
+ - Composite visitor object with per-visitor state tracking
312
+
313
+
314
+ ## JSON Traversal
315
+
316
+ Generic depth-first traversal of any JSON-like structure (dicts, lists, scalars).
317
+ Works with raw parsed OpenAPI documents or any other JSON data.
318
+
319
+ ### Quick Start
320
+
321
+ ```python
322
+ from jentic.apitools.openapi.traverse.json import traverse
323
+
324
+ # Traverse a nested structure
325
+ data = {
326
+ "openapi": "3.1.0",
327
+ "info": {"title": "My API", "version": "1.0.0"},
328
+ "paths": {
329
+ "/users": {
330
+ "get": {"summary": "List users"}
331
+ }
332
+ }
333
+ }
334
+
335
+ # Walk all nodes
336
+ for node in traverse(data):
337
+ print(f"{node.format_path()}: {node.value}")
338
+ ```
339
+
340
+ Output:
341
+ ```
342
+ openapi: 3.1.0
343
+ info: {'title': 'My API', 'version': '1.0.0'}
344
+ info.title: My API
345
+ info.version: 1.0.0
346
+ paths: {'/users': {'get': {'summary': 'List users'}}}
347
+ paths./users: {'get': {'summary': 'List users'}}
348
+ paths./users.get: {'summary': 'List users'}
349
+ paths./users.get.summary: List users
350
+ ```
351
+
352
+ ### Working with Paths
353
+
354
+ ```python
355
+ from jentic.apitools.openapi.traverse.json import traverse
356
+
357
+ data = {
358
+ "users": [
359
+ {"name": "Alice", "email": "alice@example.com"},
360
+ {"name": "Bob", "email": "bob@example.com"}
361
+ ]
362
+ }
363
+
364
+ for node in traverse(data):
365
+ # Access path information
366
+ print(f"Path: {node.path}")
367
+ print(f"Segment: {node.segment}")
368
+ print(f"Full path: {node.full_path}")
369
+ print(f"Formatted: {node.format_path()}")
370
+ print(f"Depth: {len(node.ancestors)}")
371
+ print()
372
+ ```
373
+
374
+ ### Custom Path Formatting
375
+
376
+ ```python
377
+ for node in traverse(data):
378
+ # Default dot separator
379
+ print(node.format_path()) # e.g., "paths./users.get.summary"
380
+
381
+ # Custom separator
382
+ print(node.format_path(separator="/")) # e.g., "paths//users/get/summary"
383
+ ```
384
+
385
+ ### Finding Specific Nodes
386
+
387
+ ```python
388
+ # Find all $ref references in a document
389
+ refs = [
390
+ node.value["$ref"]
391
+ for node in traverse(openapi_doc)
392
+ if isinstance(node.value, dict) and "$ref" in node.value
393
+ ]
394
+
395
+ # Find all nodes at a specific path segment
396
+ schemas = [
397
+ node.value
398
+ for node in traverse(openapi_doc)
399
+ if node.segment == "schema"
400
+ ]
401
+
402
+ # Find deeply nested values
403
+ response_descriptions = [
404
+ node.value
405
+ for node in traverse(openapi_doc)
406
+ if node.segment == "description" and "responses" in node.path
407
+ ]
408
+ ```
409
+
410
+ ### API Reference
411
+
412
+ #### `traverse(root: JSONValue) -> Iterator[TraversalNode]`
413
+
414
+ Performs depth-first traversal of a JSON-like structure.
415
+
416
+ **Parameters:**
417
+ - `root`: The data structure to traverse (dict, list, or scalar)
418
+
419
+ **Returns:**
420
+ - Iterator of `TraversalNode` objects
421
+
422
+ **Yields:**
423
+ - For dicts: one node per key-value pair
424
+ - For lists: one node per index-item pair
425
+ - Scalars at root don't yield nodes (but are accessible via parent nodes)
426
+
427
+ #### `TraversalNode`
428
+
429
+ Immutable dataclass representing a node encountered during traversal.
430
+
431
+ **Attributes:**
432
+ - `path: JSONPath` - Path from root to the parent container (tuple of segments)
433
+ - `parent: JSONContainer` - The parent container (dict or list)
434
+ - `segment: PathSeg` - The key (for dicts) or index (for lists) within parent
435
+ - `value: JSONValue` - The actual value at `parent[segment]`
436
+ - `ancestors: tuple[JSONValue, ...]` - Ordered tuple of values from root down to (but not including) parent
437
+
438
+ **Properties:**
439
+ - `full_path: JSONPath` - Complete path from root to this value (`path + (segment,)`)
440
+
441
+ **Methods:**
442
+ - `format_path(separator: str = ".") -> str` - Format the full path as a human-readable string
443
+
444
+ ### Usage Examples
445
+
446
+ #### Collecting All Schemas
447
+
448
+ ```python
449
+ from jentic.apitools.openapi.traverse.json import traverse
450
+
451
+ def collect_schemas(openapi_doc):
452
+ """Collect all schema objects from an OpenAPI document."""
453
+ schemas = []
454
+
455
+ for node in traverse(openapi_doc):
456
+ if node.segment == "schema" and isinstance(node.value, dict):
457
+ schemas.append({
458
+ "path": node.format_path(),
459
+ "schema": node.value
460
+ })
461
+
462
+ return schemas
463
+ ```
464
+
465
+
466
+ #### Analyzing Document Structure
467
+
468
+ ```python
469
+ def analyze_depth(data):
470
+ """Analyze the depth distribution of a document."""
471
+ max_depth = 0
472
+ depth_counts = {}
473
+
474
+ for node in traverse(data):
475
+ depth = len(node.ancestors)
476
+ max_depth = max(max_depth, depth)
477
+ depth_counts[depth] = depth_counts.get(depth, 0) + 1
478
+
479
+ return {
480
+ "max_depth": max_depth,
481
+ "depth_distribution": depth_counts
482
+ }
483
+ ```
484
+
485
+ ### Testing
486
+
487
+ The package includes comprehensive test coverage for JSON traversal:
488
+
489
+ ```bash
490
+ uv run --package jentic-openapi-traverse pytest packages/jentic-openapi-traverse/tests -v
491
+ ```