|
| 1 | +pub fn resolve_json_path(json: serde_json::Value, path: &str) -> serde_json::Value { |
| 2 | + let parts = path.split('/').collect::<Vec<&str>>(); |
| 3 | + let mut current_json = json; |
| 4 | + for part in parts { |
| 5 | + current_json = current_json[part].clone(); |
| 6 | + } |
| 7 | + current_json |
| 8 | +} |
| 9 | + |
| 10 | +pub fn resolve_refs(json: serde_json::Value, root_json: serde_json::Value) -> serde_json::Value { |
| 11 | + match json { |
| 12 | + serde_json::Value::Object(map) => { |
| 13 | + let mut new_map = serde_json::Map::new(); |
| 14 | + for (key, value) in map { |
| 15 | + if key == "$ref" { |
| 16 | + if let serde_json::Value::String(string_val) = value { |
| 17 | + let correct_json = resolve_json_path( |
| 18 | + root_json.clone(), |
| 19 | + string_val.trim_start_matches("#/"), |
| 20 | + ); |
| 21 | + return resolve_refs(correct_json, root_json); |
| 22 | + } else { |
| 23 | + panic!("$ref value is not a string"); |
| 24 | + } |
| 25 | + } |
| 26 | + let new_value = resolve_refs(value, root_json.clone()); |
| 27 | + new_map.insert(key, new_value); |
| 28 | + } |
| 29 | + serde_json::Value::Object(new_map) |
| 30 | + } |
| 31 | + serde_json::Value::Array(array) => { |
| 32 | + let new_array = array |
| 33 | + .into_iter() |
| 34 | + .map(|value| resolve_refs(value, root_json.clone())) |
| 35 | + .collect(); |
| 36 | + serde_json::Value::Array(new_array) |
| 37 | + } |
| 38 | + _ => json, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[cfg(test)] |
| 43 | +mod tests { |
| 44 | + use std::{fs, path::Path}; |
| 45 | + |
| 46 | + use super::*; |
| 47 | + use crate::*; |
| 48 | + |
| 49 | + const SCHEMAS: [&str; 1] = ["./example/specs/basic_ref.yml"]; |
| 50 | + |
| 51 | + //parse file to json, allowed files are yaml and json |
| 52 | + fn parse_test(path: &Path) -> serde_json::Value { |
| 53 | + let string_content = fs::read_to_string(path).expect("file could not be read"); |
| 54 | + // check if file is yaml or json |
| 55 | + let parsed: serde_json::Value = match path.extension() { |
| 56 | + Some(ext) => match ext.to_str() { |
| 57 | + Some("yaml") => serde_yaml::from_str::<serde_json::Value>(&string_content).unwrap(), |
| 58 | + Some("yml") => serde_yaml::from_str::<serde_json::Value>(&string_content).unwrap(), |
| 59 | + Some("json") => serde_json::from_str::<serde_json::Value>(&string_content).unwrap(), |
| 60 | + _ => { |
| 61 | + panic!("file has no extension"); |
| 62 | + } |
| 63 | + }, |
| 64 | + None => { |
| 65 | + panic!("file has no extension"); |
| 66 | + } |
| 67 | + }; |
| 68 | + parsed |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn resolves_refs() { |
| 73 | + for schema_paths in SCHEMAS { |
| 74 | + let definition = parse_test(Path::new(schema_paths)); |
| 75 | + let resolved: serde_json::Value = resolve_refs(definition.clone(), definition.clone()); |
| 76 | + let filename_without_extension = Path::new(schema_paths) |
| 77 | + .file_stem() |
| 78 | + .unwrap() |
| 79 | + .to_str() |
| 80 | + .unwrap(); |
| 81 | + let out_dir = Path::new("./test_output/{}.rs").join(filename_without_extension); |
| 82 | + utils::write_to_path_create_dir(&resolved.to_string(), &out_dir).unwrap(); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments