diff --git a/typify-impl/src/util.rs b/typify-impl/src/util.rs index 848ea5b6..64cb813b 100644 --- a/typify-impl/src/util.rs +++ b/typify-impl/src/util.rs @@ -544,11 +544,17 @@ pub(crate) fn constant_string_value(schema: &Schema) -> Option<&str> { } } +fn decode_segment(segment: &str) -> String { + segment.replace("~1", "/").replace("~0", "~") +} + pub(crate) fn ref_key(ref_name: &str) -> RefKey { if ref_name == "#" { RefKey::Root } else if let Some(idx) = ref_name.rfind('/') { - RefKey::Def(ref_name[idx + 1..].to_string()) + let decoded_segment = decode_segment(&ref_name[idx + 1..]); + + RefKey::Def(decoded_segment) } else { panic!("expected a '/' in $ref: {}", ref_name) } @@ -901,7 +907,7 @@ mod tests { }; use crate::{ - util::{sanitize, schemas_mutually_exclusive, Case}, + util::{decode_segment, sanitize, schemas_mutually_exclusive, Case}, Name, }; @@ -1007,6 +1013,12 @@ mod tests { assert!(schemas_mutually_exclusive(&b, &a, &BTreeMap::new())); } + #[test] + fn test_decode_segment() { + assert_eq!(decode_segment("foo~1bar"), "foo/bar"); + assert_eq!(decode_segment("foo~0bar"), "foo~bar"); + } + #[test] fn test_sanitize() { assert_eq!(sanitize("type", Case::Snake), "type_");