def __init__(self, api_spec: dict, schema: dict, resolver_cls=OpenApiResolver): resolver = resolver_cls.from_schema( schema=openapi_schema_to_json_schema.to_json_schema(api_spec) ) # OpenApi 3 is closest to JSON Schema Draft 4 self.validator = jsonschema.Draft4Validator( schema=openapi_schema_to_json_schema.to_json_schema(schema), resolver=resolver )
def change_dict_values(d, prefix, version): new = {} try: for k, v in iteritems(d): new_v = v new_v = to_json_schema(new_v) if isinstance(v, dict): new_v = change_dict_values(v, prefix, version) elif isinstance(v, list): new_v = list() for x in v: new_v.append(change_dict_values(x, prefix, version)) elif isinstance(v, str): if k == "$ref": if version < "3": new_v = "%s%s" % (prefix, v) else: new_v = v.replace("#/components/schemas/", "") + ".json" else: new_v = v new[k] = new_v return new except AttributeError: return d
def get_specification(schema_name: str, filepath: str = SCHEMA_FILE) -> dict: specification = load_full_specification(filepath=filepath) open_api_dict = specification.get("components").get("schemas")[schema_name] options = {"supportPatternProperties": True} logging.debug(f"Converting OpenApi to jsonschema (schema: {schema_name})") converted_schema_dict = to_json_schema(schema=open_api_dict, options=options) return converted_schema_dict
def resolve(self, ref): url, schema = super().resolve(ref) return url, openapi_schema_to_json_schema.to_json_schema(schema)