Repository: asyncapi/modelina (ts)

Issue:
Incorrect C# code generated for Enum in System.Text.Json JsonConverter.
#### Describe the bug
When generating `System.Text.Json` compatible models, the generated code does not compile.

#### How to Reproduce

```yml
asyncapi: 2.0.0
info:
  title: Person Service
  version: 1.0.0
channels:
  person:
    subscribe:
      message:
        payload:
          type: object
          $id: Person
          properties:
            Name:
              type: string
            FavoriteColor:
              type: string
              $id: Colors
              enum:
                - Red
                - Green
                - Blue
```

Run the following command to generate the models
```ps
docker run --rm -it --user=root `
   -v .:/app/input `
   -v c:\temp\modelina\bug1\models:/app/output  `
   asyncapi/cli:1.5.2 `
   generate models csharp `
   -o /app/output `
   /app/input/PersonService.yaml `
   --namespace=MyTest `
   --csharpSystemJson
```

#### Expected behavior
Compilable code that correctly serializes the `FavouriteColor` property should be generated.

#### Actual Behaviour

The generated code is incorrect - specifically in the following method:

```c#
public override void Write(Utf8JsonWriter writer, Person value, JsonSerializerOptions options)
{
  // snip
  var properties = value.GetType().GetProperties().Where(prop => prop.Name != "AdditionalProperties");

  writer.WriteStartObject();

  // snip
  if(value.FavoriteColor != null) {
    // write property name and let the serializer serialize the value itself
    writer.WritePropertyName("FavoriteColor");

    // ERROR: value.Colors does not exist. It should be value.FavoriteColor.
    JsonSerializer.Serialize(writer, value.Colors?.GetValue(), options);
  }
  // snip
}
```

Pull request description:
fix: incorrect enum serializer generation for System.Text.Json
## Description
<!-- Provide a brief description of the changes introduced by this pull request. -->
Ensures enums with a different name to the property are correctly serialized by `System.Text.Json`. 

## Related Issue
<!-- If this pull request is related to any existing issue, mention it here. -->
Fixes #1783 

## Checklist
- [x] The code follows the project's coding standards and is properly linted (`npm run lint`).
- [x] Tests have been added or updated to cover the changes.
- [ ] Documentation has been updated to reflect the changes.
   No documentation change required
- [x] All tests pass successfully locally.(`npm run test`).

## Additional Notes
<!-- Add any additional information or context that might be relevant to reviewers. -->


Patch:
diff --git a/src/generators/csharp/presets/JsonSerializerPreset.ts b/src/generators/csharp/presets/JsonSerializerPreset.ts
index eefd5fd1..2299cfbe 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);`;
 }


Test patch:
diff --git a/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap b/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap
index 6d603757..21ac5765 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<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
diff --git a/test/runtime/generic-input.json b/test/runtime/generic-input.json
index afe36564..9ecb63c3 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 bb50cce4..de0f6ab5 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 class AddressTests
     [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 class AddressTests
         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));
     }
 }
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 896823ab..9fb5b3c6 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 class AddressTests
     [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 class AddressTests
         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));
     }
 }