Mutates the given JSON schema to ensure it conforms to the `strict` standard that the API expects.
(
json_schema: object,
*,
path: tuple[str, ...],
root: dict[str, object],
)
| 25 | |
| 26 | |
| 27 | def _ensure_strict_json_schema( |
| 28 | json_schema: object, |
| 29 | *, |
| 30 | path: tuple[str, ...], |
| 31 | root: dict[str, object], |
| 32 | ) -> dict[str, Any]: |
| 33 | """Mutates the given JSON schema to ensure it conforms to the `strict` standard |
| 34 | that the API expects. |
| 35 | """ |
| 36 | if not is_dict(json_schema): |
| 37 | raise TypeError(f"Expected {json_schema} to be a dictionary; path={path}") |
| 38 | |
| 39 | defs = json_schema.get("$defs") |
| 40 | if is_dict(defs): |
| 41 | for def_name, def_schema in defs.items(): |
| 42 | _ensure_strict_json_schema(def_schema, path=(*path, "$defs", def_name), root=root) |
| 43 | |
| 44 | definitions = json_schema.get("definitions") |
| 45 | if is_dict(definitions): |
| 46 | for definition_name, definition_schema in definitions.items(): |
| 47 | _ensure_strict_json_schema(definition_schema, path=(*path, "definitions", definition_name), root=root) |
| 48 | |
| 49 | typ = json_schema.get("type") |
| 50 | if typ == "object" and "additionalProperties" not in json_schema: |
| 51 | json_schema["additionalProperties"] = False |
| 52 | |
| 53 | # object types |
| 54 | # { 'type': 'object', 'properties': { 'a': {...} } } |
| 55 | properties = json_schema.get("properties") |
| 56 | if is_dict(properties): |
| 57 | json_schema["required"] = [prop for prop in properties.keys()] |
| 58 | json_schema["properties"] = { |
| 59 | key: _ensure_strict_json_schema(prop_schema, path=(*path, "properties", key), root=root) |
| 60 | for key, prop_schema in properties.items() |
| 61 | } |
| 62 | |
| 63 | # arrays |
| 64 | # { 'type': 'array', 'items': {...} } |
| 65 | items = json_schema.get("items") |
| 66 | if is_dict(items): |
| 67 | json_schema["items"] = _ensure_strict_json_schema(items, path=(*path, "items"), root=root) |
| 68 | |
| 69 | # unions |
| 70 | any_of = json_schema.get("anyOf") |
| 71 | if is_list(any_of): |
| 72 | json_schema["anyOf"] = [ |
| 73 | _ensure_strict_json_schema(variant, path=(*path, "anyOf", str(i)), root=root) |
| 74 | for i, variant in enumerate(any_of) |
| 75 | ] |
| 76 | |
| 77 | # intersections |
| 78 | all_of = json_schema.get("allOf") |
| 79 | if is_list(all_of): |
| 80 | if len(all_of) == 1: |
| 81 | json_schema.update(_ensure_strict_json_schema(all_of[0], path=(*path, "allOf", "0"), root=root)) |
| 82 | json_schema.pop("allOf") |
| 83 | else: |
| 84 | json_schema["allOf"] = [ |
no test coverage detected