Skip to content

Commit

Permalink
fix: incorrect enum serializer generation for System.Text.Json (#1794)
Browse files Browse the repository at this point in the history
Co-authored-by: Rowland Banks <[email protected]>
  • Loading branch information
RowlandBanks and Rowland Banks authored Feb 13, 2024
1 parent 11e2053 commit d7ebc13
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/generators/csharp/presets/JsonSerializerPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function renderSerializeProperty(
model.property instanceof ConstrainedReferenceModel &&
model.property.ref instanceof ConstrainedEnumModel
) {
value = `value.${model.property.type}.GetValue()`;
value = `${modelInstanceVariable}?.GetValue()`;
}
return `JsonSerializer.Serialize(writer, ${value}, options);`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ internal class TestConverter : JsonConverter<Test>
if(value.EnumProp != null) {
// write property name and let the serializer serialize the value itself
writer.WritePropertyName(\\"enumProp\\");
JsonSerializer.Serialize(writer, value.EnumTest?.GetValue(), options);
JsonSerializer.Serialize(writer, value.EnumProp?.GetValue(), options);
}
if(value.ObjectProp != null) {
// write property name and let the serializer serialize the value itself
Expand Down
15 changes: 14 additions & 1 deletion test/runtime/generic-input.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
},
"enumTest": {
"enum": ["test", "test2"]
},
"houseType": {
"$ref": "#/$defs/housing-types"
},
"roofType": {
"$id": "TypeOfRoof",
"enum": ["tile", "straw", "wood", "metal"]
}
},
"patternProperties": {
Expand All @@ -57,5 +64,11 @@
"required": [
"house_number",
"array_type"
]
],
"$defs": {
"housing-types": {
"$id": "HousingType",
"enum": ["detached", "terraced", "bungalow", "flat"]
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Text.Json;
using com.mycompany.app.json_serializer;
using com.mycompany.app.json_serializer;
using System.Text.Json;

namespace runtime_csharp.json_serializer;

Expand All @@ -14,20 +14,22 @@ public void Setup()
[Test]
public void TestSerializingFullModel()
{
Address address = new Address();
NestedObject nestedObject = new NestedObject();
Address address = new();
NestedObject nestedObject = new();
nestedObject.Test = "test";
address.NestedObject = nestedObject;
address.Marriage = true;
address.Members = 2;
address.HouseNumber = 1;
address.ArrayType = new dynamic[] { 1, "test" };
address.EnumTest = EnumTest.TEST;
address.HouseType = HousingType.FLAT;
address.RoofType = TypeOfRoof.STRAW;
address.AdditionalProperties = new Dictionary<string, dynamic>();
address.AdditionalProperties.Add("test_not_used", 2);

string actualJsonString = JsonSerializer.Serialize(address);
string expectedJsonString = "{\"house_number\":1,\"marriage\":true,\"members\":2,\"array_type\":[1,\"test\"],\"nestedObject\":{\"test\":\"test\"},\"enumTest\":\"test\",\"test_not_used\":2}";
string expectedJsonString = "{\"house_number\":1,\"marriage\":true,\"members\":2,\"array_type\":[1,\"test\"],\"nestedObject\":{\"test\":\"test\"},\"enumTest\":\"test\",\"houseType\":\"flat\",\"roofType\":\"straw\",\"test_not_used\":2}";
Assert.That(actualJsonString, Is.EqualTo(expectedJsonString));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.Json;
using com.mycompany.app.newtonsoft;
using com.mycompany.app.newtonsoft;
using Newtonsoft.Json;

namespace runtime_csharp.newtonsoft;
Expand All @@ -15,20 +14,22 @@ public void Setup()
[Test]
public void TestSerializingFullModel()
{
Address address = new Address();
NestedObject nestedObject = new NestedObject();
Address address = new();
NestedObject nestedObject = new();
nestedObject.Test = "test";
address.NestedObject = nestedObject;
address.Marriage = true;
address.Members = 2;
address.HouseNumber = 1;
address.ArrayType = new dynamic[] { 1, "test" };
address.EnumTest = EnumTest.TEST;
address.HouseType = HousingType.FLAT;
address.RoofType = TypeOfRoof.STRAW;
address.AdditionalProperties = new Dictionary<string, dynamic>();
address.AdditionalProperties.Add("test_not_used", 2);

string actualJsonString = JsonConvert.SerializeObject(address);
string expectedJsonString = "{\"house_number\":1.0,\"marriage\":true,\"members\":2,\"array_type\":[1,\"test\"],\"nestedObject\":{\"test\":\"test\"},\"enumTest\":\"test\",\"test_not_used\":2}";
string expectedJsonString = "{\"house_number\":1.0,\"marriage\":true,\"members\":2,\"array_type\":[1,\"test\"],\"nestedObject\":{\"test\":\"test\"},\"enumTest\":\"test\",\"houseType\":\"flat\",\"roofType\":\"straw\",\"test_not_used\":2}";
Assert.That(actualJsonString, Is.EqualTo(expectedJsonString));
}
}
Expand Down

0 comments on commit d7ebc13

Please sign in to comment.