@@ -657,7 +657,7 @@ pub fn gen_unions(out: &mut String, unions: &BTreeMap<String, InferredUnion>, me
657
657
. unwrap_or_else ( || schema. schema_data . title . clone ( ) . unwrap ( ) ) ;
658
658
let variant_name = meta. schema_to_rust_type ( & object_name) ;
659
659
let type_name = meta. schema_to_rust_type ( variant_schema) ;
660
- if variant_name . to_snake_case ( ) != object_name {
660
+ if variant_to_serde_snake_case ( & variant_name ) != object_name {
661
661
write_serde_rename ( out, & object_name) ;
662
662
}
663
663
out. push_str ( " " ) ;
@@ -694,6 +694,22 @@ pub fn gen_unions(out: &mut String, unions: &BTreeMap<String, InferredUnion>, me
694
694
}
695
695
}
696
696
697
+ /// This code is taken from serde RenameRule::apply_to_variant
698
+ /// It differs in some cases from heck, so we need to make sure we
699
+ /// do exactly the same when figuring out whether we need a serde(rename)
700
+ /// e.g. heck_snake(Self_) = self
701
+ /// serde_snake(Self_) = self_
702
+ pub fn variant_to_serde_snake_case ( variant : & str ) -> String {
703
+ let mut snake = String :: new ( ) ;
704
+ for ( i, ch) in variant. char_indices ( ) {
705
+ if i > 0 && ch. is_uppercase ( ) {
706
+ snake. push ( '_' ) ;
707
+ }
708
+ snake. push ( ch. to_ascii_lowercase ( ) ) ;
709
+ }
710
+ snake
711
+ }
712
+
697
713
#[ tracing:: instrument( skip_all) ]
698
714
pub fn gen_variant_name ( wire_name : & str , meta : & Metadata ) -> String {
699
715
match wire_name {
@@ -732,7 +748,7 @@ pub fn gen_enums(out: &mut String, enums: &BTreeMap<String, InferredEnum>, meta:
732
748
if variant_name. trim ( ) . is_empty ( ) {
733
749
panic ! ( "unhandled enum variant: {:?}" , wire_name)
734
750
}
735
- if & variant_name . to_snake_case ( ) != wire_name {
751
+ if & variant_to_serde_snake_case ( & variant_name ) != wire_name {
736
752
write_serde_rename ( out, wire_name) ;
737
753
}
738
754
out. push_str ( " " ) ;
0 commit comments