diff --git a/typify-impl/src/lib.rs b/typify-impl/src/lib.rs index a9cf8c24..60079d30 100644 --- a/typify-impl/src/lib.rs +++ b/typify-impl/src/lib.rs @@ -924,6 +924,36 @@ impl TypeSpace { .iter() .for_each(|x| output.add_item(output::OutputSpaceMod::Defaults, "", x.into())); + // Re-export replaced types so callers can access them without knowing + // the replacement path. Only emit the re-export when the replacement + // was actually used in this schema. + self.settings + .replace + .iter() + .filter(|(_, replace)| { + self.id_to_entry.values().any(|entry| { + matches!( + &entry.details, + TypeEntryDetails::Native(n) if n.type_name == replace.replace_type + ) + }) + }) + .for_each(|(type_name, replace)| { + let type_ident = quote::format_ident!("{}", type_name); + let replace_tokens: TokenStream = replace + .replace_type + .parse() + .expect("invalid replacement type path"); + output.add_item( + output::OutputSpaceMod::Crate, + type_name, + quote! { + #[allow(unused_imports)] + pub use #replace_tokens as #type_ident; + }, + ); + }); + output.into_stream() } @@ -1300,6 +1330,28 @@ mod tests { } } + #[test] + fn test_replacement_reexport() { + let schema = json!({ + "$schema": "http://json-schema.org/draft-04/schema#", + "definitions": { + "somename": { + "type": "object", + "properties": { + "x": { "type": "string" } + } + } + } + }); + let schema = serde_json::from_value(schema).unwrap(); + let mut settings = TypeSpaceSettings::default(); + settings.with_replacement("Somename", "other_crate::Somename", [].into_iter()); + let mut type_space = TypeSpace::new(&settings); + type_space.add_root_schema(schema).unwrap(); + let tokens = type_space.to_stream().to_string(); + assert!(tokens.contains("pub use other_crate :: Somename as Somename")); + } + #[test] fn test_external_references() { let schema = json!({ diff --git a/typify-impl/tests/vega.out b/typify-impl/tests/vega.out index ca8894dc..b725284b 100644 --- a/typify-impl/tests/vega.out +++ b/typify-impl/tests/vega.out @@ -17658,6 +17658,8 @@ pub struct ColorRgb { pub g: NumberValue, pub r: NumberValue, } +#[allow(unused_imports)] +pub use ColorValue; #[doc = "`Compare`"] #[doc = r""] #[doc = r"
JSON schema"] diff --git a/typify/tests/schemas/type-with-modified-generation.rs b/typify/tests/schemas/type-with-modified-generation.rs index 1ff62b6d..b994d9da 100644 --- a/typify/tests/schemas/type-with-modified-generation.rs +++ b/typify/tests/schemas/type-with-modified-generation.rs @@ -25,6 +25,8 @@ pub mod error { } } } +#[allow(unused_imports)] +pub use String as HandGeneratedType; #[doc = "`TestType`"] #[doc = r""] #[doc = r"
JSON schema"]