Skip to content

Commit

Permalink
Fix enum property missing allow rename
Browse files Browse the repository at this point in the history
  • Loading branch information
dandro committed Jul 23, 2024
1 parent fede5fc commit 44e2191
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
24 changes: 24 additions & 0 deletions core/data/tests/csharp_without_naming_convention/input.rs
Original file line number Diff line number Diff line change
@@ -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),
}
38 changes: 38 additions & 0 deletions core/data/tests/csharp_without_naming_convention/output.cs
Original file line number Diff line number Diff line change
@@ -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();
}


31 changes: 26 additions & 5 deletions core/src/language/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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{{",
Expand All @@ -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,
Expand All @@ -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}\")]",
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions core/tests/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

0 comments on commit 44e2191

Please sign in to comment.