fuzzer.rest_fuzzer.json_schema   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 42
dl 0
loc 170
rs 10
c 0
b 0
f 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A schema_fn() 0 18 1
A type_name() 0 11 1
A get_schema_for() 0 20 1
A get_schema_for_primitive() 0 14 1
A get_schema_for_list() 0 31 2
A make_schema_object() 0 35 1
A get_schema_for_dict() 0 27 1
1
def type_name(item):
2
    """
3
    ----
4
    examples:
5
6
    1) type_name('apple') -> 'str'
7
    ----
8
    :param item:
9
    :return:
10
    """
11
    return type(item).__name__
12
13
14
def schema_fn(type_of_item):
15
    """
16
    ----
17
    examples:
18
19
    1) schema_fn("str") -> get_schema_for_primitive
20
    ----
21
    :param type_of_item:
22
    :return:
23
    """
24
    type_fn_list = {
25
        "str": get_schema_for_primitive,
26
        "int": get_schema_for_primitive,
27
        "float": get_schema_for_primitive,
28
        "list": get_schema_for_list,
29
        "dict": get_schema_for_dict
30
    }
31
    return type_fn_list[type_of_item]
32
33
34
def get_schema_for_primitive(name, type_of_item, mutations):
35
    """
36
    ----
37
    examples:
38
39
    1) get_schema_for_primitive('', '', []) -> (0.1, [])
40
    ----
41
    :param name:
42
    :param type_of_item:
43
    :return:
44
    """
45
    mutation = 0.1
46
    mutations.append(mutation)
47
    return mutation, []
48
49
50
def get_schema_for_list(body, type_of_item, mutations):
51
    """
52
    ----
53
    examples:
54
55
    @let
56
    schema_object = (0.1, {
57
        'inner': (0.1, [{
58
            'inner':  (0.1, []),
59
            'name': 'key',
60
            'type': 'int'
61
        }]),
62
        'name': '__auto__',
63
        'type': 'dict'
64
    })
65
    @end
66
67
    1) get_schema_for_list([{"key": 1}], "list", []) -> schema_object
68
    ----
69
    :param body:
70
    :param type_of_item:
71
    :return:
72
    """
73
74
    mutation = 0.1
75
    mutations.append(mutation)
76
    element = body[0] if isinstance(body, list) and len(body) > 0 else None
77
    return mutation, {
78
        "name": "__auto__",
79
        "type": type_name(element),
80
        "inner": get_schema_for(element, type_name(element), mutations)
81
    }
82
83
84
def get_schema_for_dict(body, type_of_item, mutations):
85
    """
86
    ----
87
    examples:
88
89
    @let
90
    schema_obj = (0.1, [{
91
        'inner': (0.1, []),
92
        'type': 'int',
93
        'name': 'key'
94
    }])
95
    @end
96
97
98
    1) get_schema_for_dict({'key': 1}, 'dict', []) -> schema_obj
99
    ----
100
    :param body:
101
    :param type_of_item:
102
    :return:
103
    """
104
    mutation = 0.1
105
    mutations.append(mutation)
106
    return mutation, [{
107
        "name": key,
108
        "type": type_name(body[key]),
109
        "inner": get_schema_for(body[key], type_name(body[key]), mutations)
110
    } for key in body.keys()]
111
112
113
def get_schema_for(item, type_of_item, mutations):
114
    """
115
    ----
116
    examples:
117
118
    @let
119
    schema_obj = (0.1, [{
120
        'type': 'int',
121
        'inner': (0.1, []),
122
        'name': 'key'
123
    }])
124
    @end
125
126
    1) get_schema_for({"key": 1}, 'dict', []) -> schema_obj
127
    ----
128
    :param item:
129
    :param type_of_item:
130
    :return:
131
    """
132
    return schema_fn(type_of_item)(item, type_of_item, mutations)
133
134
135
def make_schema_object(req_body):
136
    """
137
    ----
138
    examples:
139
140
    @let
141
    schema_object = (
142
        [0.1, 0.1, 0.1],
143
        {
144
            'inner': (0.1, [{
145
                'inner': (0.1, []),
146
                'name': 'key',
147
                'type': 'int'
148
            }]),
149
            'name': '__root__',
150
            'type': 'dict'
151
        }
152
    )
153
154
    @end
155
156
    1) make_schema_object({"key": 1}) -> schema_object
157
    ----
158
    :param req_body:
159
    :return:
160
    """
161
    mutation = 0.1
162
    mutations = [mutation]
163
    schema_obj = {
164
        "name": "__root__",
165
        "type": type_name(req_body),
166
        "inner": get_schema_for(req_body, type_name(req_body), mutations),
167
    }
168
169
    return mutations, schema_obj
170