File size: 3,261 Bytes
5eaaba5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def extract_descriptions(schema, path=""):
    """
    Recursively extract all descriptions from a JSON schema.
    If a property doesn't have a description, use its property name.

    Args:
        schema (dict): The JSON schema to extract descriptions from
        path (str): Current path in the schema (for nested properties)

    Returns:
        list: All descriptions found in the schema
    """
    descriptions = []

    # Handle schema description or use path as fallback
    # Skip if type is object
    if schema.get("type") != "object":
        if "description" in schema:
            descriptions.append(schema["description"])
        elif path:  # Only add path if it's not empty (root element)
            descriptions.append(path.split(".")[-1])  # Use the last part of the path

    # Handle properties
    if "properties" in schema:
        for prop_name, prop_schema in schema["properties"].items():
            prop_path = f"{path}.{prop_name}" if path else prop_name
            descriptions.extend(extract_descriptions(prop_schema, prop_path))

    # Handle array items
    if "items" in schema and isinstance(schema["items"], dict):
        item_path = f"{path}[]" if path else "items"
        descriptions.extend(extract_descriptions(schema["items"], item_path))

    # Handle oneOf, anyOf, allOf
    for key in ["oneOf", "anyOf", "allOf"]:
        if key in schema and isinstance(schema[key], list):
            for i, sub_schema in enumerate(schema[key]):
                sub_path = f"{path}.{key}[{i}]" if path else f"{key}[{i}]"
                descriptions.extend(extract_descriptions(sub_schema, sub_path))

    # Handle additional properties
    if "additionalProperties" in schema and isinstance(
        schema["additionalProperties"], dict
    ):
        add_path = f"{path}.additionalProperties" if path else "additionalProperties"
        descriptions.extend(
            extract_descriptions(schema["additionalProperties"], add_path)
        )

    return descriptions


# Example usage:
if __name__ == "__main__":
    sample_schema = {
        "type": "object",
        "description": "A person object",
        "properties": {
            "name": {"type": "string", "description": "The person's full name"},
            "age": {
                "type": "integer",
                # No description for age, will use property name
            },
            "address": {
                "type": "object",
                "description": "The person's address",
                "properties": {
                    "street": {
                        "type": "string",
                        "description": "Street name and number",
                    },
                    "city": {
                        "type": "string"
                        # No description for city, will use property name
                    },
                },
            },
            "hobbies": {
                "type": "array",
                "description": "List of hobbies",
                "items": {"type": "string", "description": "A hobby name"},
            },
        },
    }

    descriptions = extract_descriptions(sample_schema)
    print("Extracted descriptions:")
    for desc in descriptions:
        print(f"- {desc}")