Skip to main content

Jentic OpenAPI Traversal Utilities

Project description

jentic-openapi-traverse

A Python library for traversing OpenAPI documents. This package is part of the Jentic OpenAPI Tools ecosystem and provides two types of traversal:

  1. JSON Traversal (current) - Generic depth-first traversal of JSON-like structures
  2. Datamodel Traversal (planned) - OpenAPI-aware semantic traversal with visitor pattern

Installation

pip install jentic-openapi-traverse

Prerequisites:

  • Python 3.11+

JSON Traversal (Current Implementation)

Generic depth-first traversal of any JSON-like structure (dicts, lists, scalars). Works with raw parsed OpenAPI documents or any other JSON data.

Quick Start

from jentic.apitools.openapi.traverse.json import traverse

# Traverse a nested structure
data = {
    "openapi": "3.1.0",
    "info": {"title": "My API", "version": "1.0.0"},
    "paths": {
        "/users": {
            "get": {"summary": "List users"}
        }
    }
}

# Walk all nodes
for node in traverse(data):
    print(f"{node.format_path()}: {node.value}")

Output:

openapi: 3.1.0
info: {'title': 'My API', 'version': '1.0.0'}
info.title: My API
info.version: 1.0.0
paths: {'/users': {'get': {'summary': 'List users'}}}
paths./users: {'get': {'summary': 'List users'}}
paths./users.get: {'summary': 'List users'}
paths./users.get.summary: List users

Working with Paths

from jentic.apitools.openapi.traverse.json import traverse

data = {
    "users": [
        {"name": "Alice", "email": "alice@example.com"},
        {"name": "Bob", "email": "bob@example.com"}
    ]
}

for node in traverse(data):
    # Access path information
    print(f"Path: {node.path}")
    print(f"Segment: {node.segment}")
    print(f"Full path: {node.full_path}")
    print(f"Formatted: {node.format_path()}")
    print(f"Depth: {len(node.ancestors)}")
    print()

Custom Path Formatting

for node in traverse(data):
    # Default dot separator
    print(node.format_path())  # e.g., "paths./users.get.summary"

    # Custom separator
    print(node.format_path(separator="/"))  # e.g., "paths//users/get/summary"

Finding Specific Nodes

# Find all $ref references in a document
refs = [
    node.value["$ref"]
    for node in traverse(openapi_doc)
    if isinstance(node.value, dict) and "$ref" in node.value
]

# Find all nodes at a specific path segment
schemas = [
    node.value
    for node in traverse(openapi_doc)
    if node.segment == "schema"
]

# Find deeply nested values
response_descriptions = [
    node.value
    for node in traverse(openapi_doc)
    if node.segment == "description" and "responses" in node.path
]

API Reference

traverse(root: JSONValue) -> Iterator[TraversalNode]

Performs depth-first traversal of a JSON-like structure.

Parameters:

  • root: The data structure to traverse (dict, list, or scalar)

Returns:

  • Iterator of TraversalNode objects

Yields:

  • For dicts: one node per key-value pair
  • For lists: one node per index-item pair
  • Scalars at root don't yield nodes (but are accessible via parent nodes)

TraversalNode

Immutable dataclass representing a node encountered during traversal.

Attributes:

  • path: JSONPath - Path from root to the parent container (tuple of segments)
  • parent: JSONContainer - The parent container (dict or list)
  • segment: PathSeg - The key (for dicts) or index (for lists) within parent
  • value: JSONValue - The actual value at parent[segment]
  • ancestors: tuple[JSONValue, ...] - Ordered tuple of values from root down to (but not including) parent

Properties:

  • full_path: JSONPath - Complete path from root to this value (path + (segment,))

Methods:

  • format_path(separator: str = ".") -> str - Format the full path as a human-readable string

Usage Examples

Collecting All Schemas

from jentic.apitools.openapi.traverse.json import traverse

def collect_schemas(openapi_doc):
    """Collect all schema objects from an OpenAPI document."""
    schemas = []

    for node in traverse(openapi_doc):
        if node.segment == "schema" and isinstance(node.value, dict):
            schemas.append({
                "path": node.format_path(),
                "schema": node.value
            })

    return schemas

Analyzing Document Structure

def analyze_depth(data):
    """Analyze the depth distribution of a document."""
    max_depth = 0
    depth_counts = {}

    for node in traverse(data):
        depth = len(node.ancestors)
        max_depth = max(max_depth, depth)
        depth_counts[depth] = depth_counts.get(depth, 0) + 1

    return {
        "max_depth": max_depth,
        "depth_distribution": depth_counts
    }

Testing

The package includes comprehensive test coverage for JSON traversal:

uv run --package jentic-openapi-traverse pytest packages/jentic-openapi-traverse/tests -v

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jentic_openapi_traverse-1.0.0a11.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jentic_openapi_traverse-1.0.0a11-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file jentic_openapi_traverse-1.0.0a11.tar.gz.

File metadata

File hashes

Hashes for jentic_openapi_traverse-1.0.0a11.tar.gz
Algorithm Hash digest
SHA256 99ccf91cdb35bd743613b46038e7b7f596246af73c72e4c968bfc603b1a605e4
MD5 d38b67a293ad0c8946a384038ebe2ac2
BLAKE2b-256 c67f04b8d5f9061cc31e0135ba06ddd21748fffe44ddd7e88b8bae5088ffe05b

See more details on using hashes here.

Provenance

The following attestation bundles were made for jentic_openapi_traverse-1.0.0a11.tar.gz:

Publisher: release.yml on jentic/jentic-openapi-tools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file jentic_openapi_traverse-1.0.0a11-py3-none-any.whl.

File metadata

File hashes

Hashes for jentic_openapi_traverse-1.0.0a11-py3-none-any.whl
Algorithm Hash digest
SHA256 bebce7520dfe1a1c73586a52108d1b2ea0378de76d76248b11dab60573a8c23d
MD5 9daa3661cf7edd879cca7c82a86b5a6a
BLAKE2b-256 91db2f0dccc7157074fe43124615e9b41cfa52d0cb1a775ad4df4a03f0ed3660

See more details on using hashes here.

Provenance

The following attestation bundles were made for jentic_openapi_traverse-1.0.0a11-py3-none-any.whl:

Publisher: release.yml on jentic/jentic-openapi-tools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page