25
25
26
26
def canonicalize_schema (obj ):
27
27
# First, make sure the given json is a valid json schema.
28
- utils .validate_schema (obj ) # should throw jsonschema.SchemaError on unknown types
28
+ # should throw jsonschema.SchemaError on unknown types
29
+ utils .validate_schema (obj )
29
30
30
31
# Second, canonicalize the schema.
31
32
if utils .is_dict (obj ):
@@ -45,15 +46,15 @@ def canonicalize_dict(d, outer_key=None):
45
46
46
47
# Ignore (drop) any other validatoin keyword when there is a $ref
47
48
# Currently, jsonref handles this case properly,
48
- # We might need to handle it again on out own when
49
+ # We might need to handle it again on out own when
49
50
# we handle recursive $ref independently from jsonref.
50
51
# if d.get("$ref"):
51
52
# for k in list(d.keys()):
52
53
# if k != "$ref" and k not in definitions.JNonValidation:
53
54
# del d[k]
54
55
55
56
# Skip normal dict canonicalization
56
- # for object.properties;
57
+ # for object.properties;
57
58
# patternProperties;
58
59
# dependencies
59
60
# because these should be usual dict containers.
@@ -115,8 +116,8 @@ def canonicalize_single_type(d):
115
116
return rewrite_enum (d )
116
117
else :
117
118
return d
118
-
119
- # jsonschema validation in the begining prevents
119
+
120
+ # jsonschema validation in the begining prevents
120
121
# reaching this case. So we don't need this.
121
122
# else:
122
123
# print("Unknown schema type {} at:".format(t))
@@ -134,8 +135,8 @@ def canonicalize_list_of_types(d):
134
135
s_i ["type" ] = t_i
135
136
s_i = canonicalize_single_type (s_i )
136
137
anyofs .append (s_i )
137
-
138
- # jsonschema validation in the begining prevents
138
+
139
+ # jsonschema validation in the begining prevents
139
140
# reaching this case. So we don't need this.
140
141
# else:
141
142
# print("Unknown schema type {} at: {}".format(t_i, t))
@@ -190,9 +191,14 @@ def canonicalize_connectors(d):
190
191
allofs = one + nots
191
192
anyofs .append ({"allOf" : allofs })
192
193
return canonicalize_connectors ({"anyOf" : anyofs })
194
+
195
+ # Here, the connector is either allOf or oneOf
196
+ # So we better simplify them before proceeding more.
193
197
else :
194
198
d [c ] = [canonicalize_dict (i ) for i in d [c ]]
195
- return d
199
+ # return d
200
+ simplified = simplify_schema_and_embed_checkers (d )
201
+ return simplified
196
202
197
203
# Connector + other keywords. Combine them first.
198
204
else :
@@ -201,8 +207,10 @@ def canonicalize_connectors(d):
201
207
allofs .append (canonicalize_dict ({c : d [c ]}))
202
208
del d [c ]
203
209
if lhs_kw_without_connectors :
204
- allofs .append (canonicalize_dict ({k : d [k ] for k in lhs_kw_without_connectors }))
210
+ allofs .append (canonicalize_dict (
211
+ {k : d [k ] for k in lhs_kw_without_connectors }))
205
212
return {"allOf" : allofs }
213
+ # return simplify_schema_and_embed_checkers({"allOf": allofs})
206
214
207
215
208
216
def canonicalize_not (d ):
@@ -225,7 +233,7 @@ def canonicalize_not(d):
225
233
if c == "not" :
226
234
return negated_schema ["not" ]
227
235
228
- if c == "anyOf" :
236
+ elif c == "anyOf" :
229
237
allofs = []
230
238
for i in negated_schema ["anyOf" ]:
231
239
allofs .append (canonicalize_not ({"not" : i }))
@@ -272,7 +280,7 @@ def rewrite_enum(d):
272
280
{"type" : "number" , "minimum" : i , "maximum" : i })
273
281
274
282
if t == "boolean" :
275
- # booleans are allowed to keep enums,
283
+ # booleans are allowed to keep enums,
276
284
# since there are only two values.
277
285
return d
278
286
0 commit comments