Skip to content

Commit f7dbf08

Browse files
authored
use inferred union variant names in more places (#894)
1 parent 257fc26 commit f7dbf08

File tree

9 files changed

+4987
-4880
lines changed

9 files changed

+4987
-4880
lines changed

typify-impl/src/enums.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,14 @@ impl TypeSpace {
609609
// account for types such as Uuid whose names come from outside of
610610
// the schema... but you can't win them all.
611611
.map(schema_is_named)
612+
// Fall back to `VariantN` naming if any variants do not have names
613+
// inferred.
612614
.collect::<Option<Vec<_>>>()
615+
// Fall back to `VariantN` naming if any variants have the same
616+
// name.
617+
.filter(|variant_names| {
618+
variant_names.len() == variant_names.iter().collect::<HashSet<_>>().len()
619+
})
613620
// Prune the common prefixes from all variant names. If this
614621
// results in any of them being empty, we don't use these names.
615622
.and_then(|variant_names| {

typify-impl/src/util.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use std::collections::{BTreeMap, BTreeSet, HashSet};
44

5+
use heck::ToPascalCase;
56
use log::debug;
67
use schemars::schema::{
78
ArrayValidation, InstanceType, Metadata, ObjectValidation, Schema, SchemaObject, SingleOrVec,
@@ -622,6 +623,22 @@ pub(crate) fn schema_is_named(schema: &Schema) -> Option<String> {
622623
extensions: _,
623624
}) => singleton_subschema(subschemas).and_then(schema_is_named),
624625

626+
// Best-effort fallback for things with raw types that can be easily inferred
627+
Schema::Object(SchemaObject {
628+
instance_type: Some(SingleOrVec::Single(single)),
629+
format,
630+
..
631+
}) => match (**single, format.as_deref()) {
632+
(_, Some(format)) => Some(format.to_pascal_case()),
633+
(InstanceType::Boolean, _) => Some("Boolean".to_string()),
634+
(InstanceType::Integer, _) => Some("Integer".to_string()),
635+
(InstanceType::Number, _) => Some("Number".to_string()),
636+
(InstanceType::String, _) => Some("String".to_string()),
637+
(InstanceType::Array, _) => Some("Array".to_string()),
638+
(InstanceType::Object, _) => Some("Object".to_string()),
639+
(InstanceType::Null, _) => Some("Null".to_string()),
640+
},
641+
625642
_ => None,
626643
}?;
627644

typify-impl/src/value.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ mod tests {
10891089
.map(|x| x.to_string()),
10901090
Some(
10911091
quote! {
1092-
super::Test::Variant0
1092+
super::Test::Null
10931093
}
10941094
.to_string()
10951095
)
@@ -1100,7 +1100,7 @@ mod tests {
11001100
.map(|x| x.to_string()),
11011101
Some(
11021102
quote! {
1103-
super::Test::Variant1("xx".to_string(), "yy".to_string())
1103+
super::Test::Array("xx".to_string(), "yy".to_string())
11041104
}
11051105
.to_string()
11061106
)
@@ -1117,7 +1117,7 @@ mod tests {
11171117
.map(|x| x.to_string()),
11181118
Some(
11191119
quote! {
1120-
super::Test::Variant2 {
1120+
super::Test::Object {
11211121
cc: "xx".to_string(),
11221122
dd: "yy".to_string()
11231123
}
@@ -1147,7 +1147,7 @@ mod tests {
11471147
.map(|x| x.to_string()),
11481148
Some(
11491149
quote! {
1150-
Test::Variant0
1150+
Test::Null
11511151
}
11521152
.to_string()
11531153
)
@@ -1158,7 +1158,7 @@ mod tests {
11581158
.map(|x| x.to_string()),
11591159
Some(
11601160
quote! {
1161-
Test::Variant1("xx".to_string(), "yy".to_string())
1161+
Test::Array("xx".to_string(), "yy".to_string())
11621162
}
11631163
.to_string()
11641164
)
@@ -1175,7 +1175,7 @@ mod tests {
11751175
.map(|x| x.to_string()),
11761176
Some(
11771177
quote! {
1178-
Test::Variant2 {
1178+
Test::Object {
11791179
cc: "xx".to_string(),
11801180
dd: "yy".to_string()
11811181
}

0 commit comments

Comments
 (0)