From 44e2191cffacdddc587b590e79f2f6edb081a3d6 Mon Sep 17 00:00:00 2001 From: Daniel Martinez Date: Thu, 18 Jul 2024 16:50:44 +1000 Subject: [PATCH] Fix enum property missing allow rename --- .../csharp_without_naming_convention/input.rs | 24 ++++++++++++ .../output.cs | 38 +++++++++++++++++++ core/src/language/csharp.rs | 31 ++++++++++++--- core/tests/snapshot_tests.rs | 4 ++ 4 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 core/data/tests/csharp_without_naming_convention/input.rs create mode 100644 core/data/tests/csharp_without_naming_convention/output.cs diff --git a/core/data/tests/csharp_without_naming_convention/input.rs b/core/data/tests/csharp_without_naming_convention/input.rs new file mode 100644 index 00000000..79cd6c16 --- /dev/null +++ b/core/data/tests/csharp_without_naming_convention/input.rs @@ -0,0 +1,24 @@ +#[typeshare] +#[serde(rename_all = "camelCase")] +pub struct ObjectNamedA { + depends_on: String, + age: i32, + some_string_value: String, +} + +#[typeshare] +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum DimensionFitValue { + WrapContent, + FitHeight, +} + +#[typeshare] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "value", rename_all = "kebab-case")] +pub enum DimensionValue { + FixedSize(f32), + Percentage(f32), + Fit(DimensionFitValue), +} diff --git a/core/data/tests/csharp_without_naming_convention/output.cs b/core/data/tests/csharp_without_naming_convention/output.cs new file mode 100644 index 00000000..426f3f05 --- /dev/null +++ b/core/data/tests/csharp_without_naming_convention/output.cs @@ -0,0 +1,38 @@ +#nullable enable + +using System.Reflection; +using JsonSubTypes; +using Newtonsoft.Json; +using System.Runtime.Serialization; + +public class ObjectNamedA { + [JsonProperty(Required = Required.Always)] + public string dependsOn { get; set; } + [JsonProperty(Required = Required.Always)] + public int age { get; set; } + [JsonProperty(Required = Required.Always)] + public string someStringValue { get; set; } +} + +public enum DimensionFitValue +{ + [EnumMember(Value = "wrap-content")] + WrapContent, + + [EnumMember(Value = "fit-height")] + FitHeight, + +} + +[JsonConverter(typeof(JsonSubtypes), "type")] +[JsonSubtypes.KnownSubType(typeof(fixed-size), "fixed-size")] +[JsonSubtypes.KnownSubType(typeof(percentage), "percentage")] +[JsonSubtypes.KnownSubType(typeof(fit), "fit")] +public abstract record DimensionValue +{ + public record FixedSize(float Value) : DimensionValue(); + public record Percentage(float Value) : DimensionValue(); + public record Fit(DimensionFitValue Value) : DimensionValue(); +} + + diff --git a/core/src/language/csharp.rs b/core/src/language/csharp.rs index 171d31c1..ae97b453 100644 --- a/core/src/language/csharp.rs +++ b/core/src/language/csharp.rs @@ -179,7 +179,11 @@ impl Language for CSharp { writeln!(w, "\n}}\n") } RustEnum::Algebraic { shared, .. } => { - write_discriminated_union_json_attributes(w, e)?; + write_discriminated_union_json_attributes( + w, + e, + self.without_csharp_naming_convention, + )?; write!( w, "public abstract record {}{} \n{{", @@ -203,7 +207,11 @@ impl Language for CSharp { } } -fn write_discriminated_union_json_attributes(w: &mut dyn Write, e: &RustEnum) -> io::Result<()> { +fn write_discriminated_union_json_attributes( + w: &mut dyn Write, + e: &RustEnum, + with_rename: bool, +) -> io::Result<()> { match e { RustEnum::Algebraic { tag_key, @@ -213,9 +221,11 @@ fn write_discriminated_union_json_attributes(w: &mut dyn Write, e: &RustEnum) -> writeln!(w, "[JsonConverter(typeof(JsonSubtypes), \"{}\")]", tag_key)?; let case_attributes = shared.variants.iter().map(|v| { let case_name = match v { - RustEnumVariant::AnonymousStruct { shared, .. } => &shared.id.original, - RustEnumVariant::Unit(shared) => &shared.id.original, - RustEnumVariant::Tuple { shared, .. } => &shared.id.original, + RustEnumVariant::AnonymousStruct { shared, .. } => { + get_property_name(with_rename, shared) + } + RustEnumVariant::Unit(shared) => get_property_name(with_rename, shared), + RustEnumVariant::Tuple { shared, .. } => get_property_name(with_rename, shared), }; format!( "[JsonSubtypes.KnownSubType(typeof({0}), \"{0}\")]", @@ -229,6 +239,17 @@ fn write_discriminated_union_json_attributes(w: &mut dyn Write, e: &RustEnum) -> } } +fn get_property_name( + with_rename: bool, + shared: &crate::rust_types::RustEnumVariantShared, +) -> &String { + if with_rename { + &shared.id.renamed + } else { + &shared.id.original + } +} + impl CSharp { fn write_enum_variants(&mut self, w: &mut dyn Write, e: &RustEnum) -> io::Result<()> { match e { diff --git a/core/tests/snapshot_tests.rs b/core/tests/snapshot_tests.rs index 78558703..b3c22577 100644 --- a/core/tests/snapshot_tests.rs +++ b/core/tests/snapshot_tests.rs @@ -687,6 +687,10 @@ tests! { ]; can_generate_anonymous_struct_with_skipped_fields: [swift, kotlin, scala, typescript, go]; generic_struct_with_constraints_and_decorators: [swift { codablevoid_constraints: vec!["Equatable".into()] }]; + csharp_without_naming_convention: [csharp { + without_csharp_naming_convention: true, + } + ]; excluded_by_target_os: [ swift, kotlin, scala, typescript, go ] target_os: "android"; // excluded_by_target_os_full_module: [swift] target_os: "ios"; }