Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions typify-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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!({
Expand Down
2 changes: 2 additions & 0 deletions typify-impl/tests/vega.out
Original file line number Diff line number Diff line change
Expand Up @@ -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" <details><summary>JSON schema</summary>"]
Expand Down
2 changes: 2 additions & 0 deletions typify/tests/schemas/type-with-modified-generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub mod error {
}
}
}
#[allow(unused_imports)]
pub use String as HandGeneratedType;
#[doc = "`TestType`"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
Expand Down
Loading