def resolve_network(self, schema): """ Resolves all references of a given schema :param schema: a json containing the schema_url attribute :type schema: dict :return: the resolved network """ processed_schemas = {} schema_url = schema['schema_url'] if schema_url not in self.cached_requests["resolved_network"].keys(): processed_schemas[get_name(schema_url)] = '#' resolved_network = resolve_schema_references( resolve_reference(schema_url), processed_schemas, schema_url) self.cached_requests["resolved_network"][schema_url] = {} self.cached_requests["resolved_network"][schema_url][ 'schema'] = resolved_network self.cached_requests["resolved_network"][schema_url][ 'timestamp'] = datetime.datetime.now() return json.dumps(resolved_network, indent=4) else: return json.dumps( self.cached_requests["resolved_network"][schema_url]['schema'], indent=4)
def resolve_network_file(schema_file): network_schemas = {} try: with open(schema_file.replace("file:/", '')) as f: schema_content = json.load(f) network_schemas[get_name(schema_content['id'])] = schema_content resolver = RefResolver(schema_file, schema_content, store={}) return resolve_schema_ref(schema_content, resolver, network_schemas) except Exception as e: raise Exception("There is a problem with your url or schema: ", schema_file, ", ", e)
def test_resolve_schema_ref(self): expected_output = self.load_expected_input()[0]['schemas'] network_schemas = {} schema_content = json.loads( requests.get(self.network_1_schema_url).text) network_schemas[get_name(schema_content['id'])] = schema_content resolver = RefResolver(self.network_1_schema_url, schema_content, store={}) data = prepare_fulldiff_input.resolve_schema_ref( schema_content, resolver, network_schemas) self.assertTrue(DeepDiff(data, expected_output) == {})
def test__resolve_schema_references(self): schema_url = 'https://w3id.org/dats/schema/person_schema.json#' processed_schemas = {compile_schema.get_name(schema_url): '#'} schema = compile_schema.resolve_reference(schema_url) resolver = RefResolver(schema_url, schema, store={}) data = compile_schema._resolve_schema_references( schema, resolver, processed_schemas, '#') output_value = json.loads(json.dumps(data)) expected_output = json.loads(json.dumps(self.expected_output)) self.assertTrue(DeepDiff(output_value, expected_output) == {})
def test_resolve_schema_references(self): expected_output = self.expected_output processed_schemas = {} schema_url = 'https://w3id.org/dats/schema/person_schema.json#' processed_schemas[compile_schema.get_name(schema_url)] = '#' data = compile_schema.resolve_schema_references( compile_schema.resolve_reference(schema_url), processed_schemas, schema_url) output_value = json.loads(json.dumps(data)) output_expected = json.loads(json.dumps(expected_output)) self.assertTrue(DeepDiff(output_value, output_expected) == {})
def resolve_network_url(schema_url): """ Function that triggers the resolved_schema_ref function :param schema_url: a schema URL :return: a fully resolved network """ network_schemas = {} try: schema_content = json.loads(requests.get(schema_url).text) network_schemas[get_name(schema_content['id'])] = schema_content resolver = RefResolver(schema_url, schema_content, store={}) return resolve_schema_ref(schema_content, resolver, network_schemas) except Exception as e: raise Exception("There is a problem with your url or schema", schema_url, "exception ", e)
def resolve_schema_ref(schema, resolver, network): """ Recursively resolves the references in the schemas and add them to the network .. warning:: use resolve network instead :param schema: the schema to resolve :param resolver: the refResolver object :param network: the network to add the schemas to :return: a fully processed network with resolved ref """ if SchemaKey.ref in schema \ and schema['$ref'][0] != '#' \ and schema['$ref'].replace('#', '') not in network: reference_path = schema[SchemaKey.ref] resolved = resolver.resolve(reference_path)[1] if type(resolved) != Exception: network[get_name(resolved['id'])] = resolved resolve_schema_ref(resolved, resolver, network) if SchemaKey.properties in schema: for k, val in schema[SchemaKey.properties].items(): resolve_schema_ref(val, resolver, network) if SchemaKey.definitions in schema: for k, val in schema[SchemaKey.definitions].items(): resolve_schema_ref(val, resolver, network) for pattern in SchemaKey.sub_patterns: if pattern in schema: for val in schema[pattern]: resolve_schema_ref(val, resolver, network) if SchemaKey.items in schema: resolve_schema_ref(schema[SchemaKey.items], resolver, network) return network
def test_get_name(self): expected_output = "study_schema.json" schema_name = compile_schema.get_name(self.schema_URL) self.assertTrue(schema_name == expected_output)