Skip to content

Commit 8b1498d

Browse files
committed
Fix enum property missing allow rename
1 parent aabb15a commit 8b1498d

File tree

4 files changed

+93
-6
lines changed

4 files changed

+93
-6
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#[typeshare]
2+
#[serde(rename_all = "camelCase")]
3+
pub struct ObjectNamedA {
4+
depends_on: String,
5+
age: i32,
6+
some_string_value: String,
7+
}
8+
9+
#[typeshare]
10+
#[derive(Serialize, Deserialize)]
11+
#[serde(rename_all = "kebab-case")]
12+
pub enum DimensionFitValue {
13+
WrapContent,
14+
FitHeight,
15+
}
16+
17+
#[typeshare]
18+
#[derive(Serialize, Deserialize)]
19+
#[serde(tag = "type", content = "value", rename_all = "kebab-case")]
20+
pub enum DimensionValue {
21+
FixedSize(f32),
22+
Percentage(f32),
23+
Fit(DimensionFitValue),
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#nullable enable
2+
3+
using System.Reflection;
4+
using JsonSubTypes;
5+
using Newtonsoft.Json;
6+
using System.Runtime.Serialization;
7+
8+
public class ObjectNamedA {
9+
[JsonProperty(Required = Required.Always)]
10+
public string dependsOn { get; set; }
11+
[JsonProperty(Required = Required.Always)]
12+
public int age { get; set; }
13+
[JsonProperty(Required = Required.Always)]
14+
public string someStringValue { get; set; }
15+
}
16+
17+
public enum DimensionFitValue
18+
{
19+
[EnumMember(Value = "wrap-content")]
20+
WrapContent,
21+
22+
[EnumMember(Value = "fit-height")]
23+
FitHeight,
24+
25+
}
26+
27+
[JsonConverter(typeof(JsonSubtypes), "type")]
28+
[JsonSubtypes.KnownSubType(typeof(fixed-size), "fixed-size")]
29+
[JsonSubtypes.KnownSubType(typeof(percentage), "percentage")]
30+
[JsonSubtypes.KnownSubType(typeof(fit), "fit")]
31+
public abstract record DimensionValue
32+
{
33+
public record FixedSize(float Value) : DimensionValue();
34+
public record Percentage(float Value) : DimensionValue();
35+
public record Fit(DimensionFitValue Value) : DimensionValue();
36+
}
37+
38+

core/src/language/csharp.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ impl Language for CSharp {
179179
writeln!(w, "\n}}\n")
180180
}
181181
RustEnum::Algebraic { shared, .. } => {
182-
write_discriminated_union_json_attributes(w, e)?;
182+
write_discriminated_union_json_attributes(
183+
w,
184+
e,
185+
self.without_csharp_naming_convention,
186+
)?;
183187
write!(
184188
w,
185189
"public abstract record {}{} \n{{",
@@ -203,7 +207,11 @@ impl Language for CSharp {
203207
}
204208
}
205209

206-
fn write_discriminated_union_json_attributes(w: &mut dyn Write, e: &RustEnum) -> io::Result<()> {
210+
fn write_discriminated_union_json_attributes(
211+
w: &mut dyn Write,
212+
e: &RustEnum,
213+
with_rename: bool,
214+
) -> io::Result<()> {
207215
match e {
208216
RustEnum::Algebraic {
209217
tag_key,
@@ -213,9 +221,11 @@ fn write_discriminated_union_json_attributes(w: &mut dyn Write, e: &RustEnum) ->
213221
writeln!(w, "[JsonConverter(typeof(JsonSubtypes), \"{}\")]", tag_key)?;
214222
let case_attributes = shared.variants.iter().map(|v| {
215223
let case_name = match v {
216-
RustEnumVariant::AnonymousStruct { shared, .. } => &shared.id.original,
217-
RustEnumVariant::Unit(shared) => &shared.id.original,
218-
RustEnumVariant::Tuple { shared, .. } => &shared.id.original,
224+
RustEnumVariant::AnonymousStruct { shared, .. } => {
225+
get_property_name(with_rename, shared)
226+
}
227+
RustEnumVariant::Unit(shared) => get_property_name(with_rename, shared),
228+
RustEnumVariant::Tuple { shared, .. } => get_property_name(with_rename, shared),
219229
};
220230
format!(
221231
"[JsonSubtypes.KnownSubType(typeof({0}), \"{0}\")]",
@@ -229,6 +239,17 @@ fn write_discriminated_union_json_attributes(w: &mut dyn Write, e: &RustEnum) ->
229239
}
230240
}
231241

242+
fn get_property_name(
243+
with_rename: bool,
244+
shared: &crate::rust_types::RustEnumVariantShared,
245+
) -> &String {
246+
if with_rename {
247+
&shared.id.renamed
248+
} else {
249+
&shared.id.original
250+
}
251+
}
252+
232253
impl CSharp {
233254
fn write_enum_variants(&mut self, w: &mut dyn Write, e: &RustEnum) -> io::Result<()> {
234255
match e {

core/tests/snapshot_tests.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,11 @@ tests! {
715715
];
716716
can_generate_anonymous_struct_with_skipped_fields: [swift, kotlin, scala, typescript, go, python];
717717
generic_struct_with_constraints_and_decorators: [swift { codablevoid_constraints: vec!["Equatable".into()] }];
718-
excluded_by_target_os: [ swift, kotlin, scala, typescript, go,python ] target_os: ["android", "macos"];
718+
csharp_without_naming_convention: [csharp {
719+
without_csharp_naming_convention: true,
720+
}
721+
];
722+
excluded_by_target_os: [ swift, kotlin, scala, typescript, go, python ] target_os: ["android", "macos"];
719723
// excluded_by_target_os_full_module: [swift] target_os: "ios";
720724
serde_rename_references: [ swift, kotlin, scala, typescript, go ];
721725
}

0 commit comments

Comments
 (0)