diff --git a/src/generators/csharp/presets/JsonSerializerPreset.ts b/src/generators/csharp/presets/JsonSerializerPreset.ts index eefd5fd1df..2299cfbec0 100644 --- a/src/generators/csharp/presets/JsonSerializerPreset.ts +++ b/src/generators/csharp/presets/JsonSerializerPreset.ts @@ -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);`; } diff --git a/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap b/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap index 6d60375705..21ac576590 100644 --- a/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap +++ b/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap @@ -127,7 +127,7 @@ internal class TestConverter : JsonConverter 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 diff --git a/test/runtime/generic-input.json b/test/runtime/generic-input.json index afe3656468..9ecb63c338 100644 --- a/test/runtime/generic-input.json +++ b/test/runtime/generic-input.json @@ -47,6 +47,13 @@ }, "enumTest": { "enum": ["test", "test2"] + }, + "houseType": { + "$ref": "#/$defs/housing-types" + }, + "roofType": { + "$id": "TypeOfRoof", + "enum": ["tile", "straw", "wood", "metal"] } }, "patternProperties": { @@ -57,5 +64,11 @@ "required": [ "house_number", "array_type" - ] + ], + "$defs": { + "housing-types": { + "$id": "HousingType", + "enum": ["detached", "terraced", "bungalow", "flat"] + } + } } \ No newline at end of file diff --git a/test/runtime/runtime-csharp/runtime-csharp/test/models/json_serializer/Address.cs b/test/runtime/runtime-csharp/runtime-csharp/test/models/json_serializer/Address.cs index bb50cce423..de0f6ab5e0 100644 --- a/test/runtime/runtime-csharp/runtime-csharp/test/models/json_serializer/Address.cs +++ b/test/runtime/runtime-csharp/runtime-csharp/test/models/json_serializer/Address.cs @@ -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; @@ -14,8 +14,8 @@ 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; @@ -23,11 +23,13 @@ public void TestSerializingFullModel() 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(); 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)); } } diff --git a/test/runtime/runtime-csharp/runtime-csharp/test/models/newtonsoft/Address.cs b/test/runtime/runtime-csharp/runtime-csharp/test/models/newtonsoft/Address.cs index 896823abbb..9fb5b3c686 100644 --- a/test/runtime/runtime-csharp/runtime-csharp/test/models/newtonsoft/Address.cs +++ b/test/runtime/runtime-csharp/runtime-csharp/test/models/newtonsoft/Address.cs @@ -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; @@ -15,8 +14,8 @@ 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; @@ -24,11 +23,13 @@ public void TestSerializingFullModel() 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(); 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)); } }