File size: 1,115 Bytes
65d80b0
 
 
 
 
 
 
 
 
 
 
 
4c8991b
 
 
65d80b0
4c8991b
65d80b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c04698b
65d80b0
 
 
 
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
import glob
import json
import os
import sys

DEFN_KEYWORDS = [
    "definitions",
    "$defs",
]

REMOVE_KEYS = [
    "$comment",
    "$id",
    "$ref",
    "Description",
    "description",
    "markdownDescription",
    "title",
]

dict_values = type({}.values())


def clean_object(obj):
    if isinstance(obj, dict):
        return {k: clean_object(v) for k, v in obj.items() if k not in REMOVE_KEYS}
    elif isinstance(obj, list):
        return [clean_object(v) for v in obj]
    else:
        return obj

def get_definitions(obj):
    for key in DEFN_KEYWORDS:
        for name, defn in obj.get(key, {}).items():
            if '"$ref"' not in json.dumps(defn):
                yield name, defn


for line in sys.stdin:
    obj = json.loads(line)
    schema = obj["name"]
    obj = json.loads(obj["content"])
    for key, defn in get_definitions(obj):
        json.dump(
            {
                "schema": os.path.basename(schema),
                "key": key,
                "definition": json.dumps(clean_object(defn))
            },
            sys.stdout,
        )
        sys.stdout.write("\n")