Repository: microsoft/kiota (csharp)

Issue:
[TS] Enum default value deserialization
I have an OpenAPI type definition `geography` with an enum field `type` that has a default value `States`:
```
"geography": {
  "type": "object",
  "properties": {
    "type": {
      "default": "States",
      "enum": ["States","Counties", ...],
````
I see a build error in the generated TypeScript client model code line in /client/models/index.ts in the method `deserializeIntoGeography` ('Geography_type' only refers to a type, but is being used as a value):
```ts
"type": n => { geography.type = n.getEnumValue<Geography_type>(Geography_typeObject) ?? Geography_type.States; },
```
and I am able to fix it by changing it to:
```ts
"type": n => { geography.type = n.getEnumValue<Geography_type>(Geography_typeObject) ?? Geography_typeObject.States; },
```
using Kiota dotnet tool version 1.11.1+86b3a623dc3fb6b55fed6a49519ec837f0fbc02c

Pull request description:
fixes #4216 enum default value literal

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5b6ee54c0f..57601a4e2c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - 📢📢📢 Java generation is now stable! 🚀🚀🚀 special thanks to @andreaTP (Red Hat) for all the help.
 - Fixed bug where stream responses would generate incorrect partial paging code. [#4207](https://github.com/microsoft/kiota/issues/4207)
 - Sanitize Accept and Content-Type headers during generation. [#4211](https://github.com/microsoft/kiota/issues/4211)
+- Fixed a bug in enum default value generator for TypeScript. [#4216](https://github.com/microsoft/kiota/issues/4216)
 
 ## [1.11.1] - 2024-02-05
 
diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
index 1fb6d24e66..c5b809c950 100644
--- a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
+++ b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
@@ -255,8 +255,8 @@ private void WriteDeserializerFunction(CodeFunction codeFunction, LanguageWriter
     private static string GetDefaultValueLiteralForProperty(CodeProperty codeProperty)
     {
         if (string.IsNullOrEmpty(codeProperty.DefaultValue)) return string.Empty;
-        if (codeProperty.Type is CodeType propertyType && propertyType.TypeDefinition is CodeEnum enumDefinition)
-            return $"{enumDefinition.Name.ToFirstCharacterUpperCase()}.{codeProperty.DefaultValue.Trim('"').CleanupSymbolName().ToFirstCharacterUpperCase()}";
+        if (codeProperty.Type is CodeType propertyType && propertyType.TypeDefinition is CodeEnum enumDefinition && enumDefinition.CodeEnumObject is not null)
+            return $"{enumDefinition.CodeEnumObject.Name.ToFirstCharacterUpperCase()}.{codeProperty.DefaultValue.Trim('"').CleanupSymbolName().ToFirstCharacterUpperCase()}";
         return codeProperty.DefaultValue;
     }
     private static void WriteDefensiveStatements(CodeMethod codeElement, LanguageWriter writer)


Test patch:
diff --git a/tests/Kiota.Builder.Tests/TestHelper.cs b/tests/Kiota.Builder.Tests/TestHelper.cs
index bd9725336f..aa81170632 100644
--- a/tests/Kiota.Builder.Tests/TestHelper.cs
+++ b/tests/Kiota.Builder.Tests/TestHelper.cs
@@ -115,6 +115,8 @@ public static void AddSerializationPropertiesToModelClass(CodeClass modelClass)
         {
             Name = "EnumType"
         };
+        var enumOption = new CodeEnumOption() { Name = "SomeOption" };
+        propertyEnum.AddOption(enumOption);
         parentNamespace.AddEnum(propertyEnum);
         modelClass.AddProperty(new CodeProperty
         {
diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs
index 18358913bb..8855644e50 100644
--- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs
+++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs
@@ -333,6 +333,26 @@ public async Task WritesDeSerializerBodyWithDefaultValue()
                 Name = "string",
             },
         });
+        var propertyEnum = new CodeEnum
+        {
+            Name = "EnumTypeWithOption",
+            Parent = parentClass,
+        };
+        var enumOption = new CodeEnumOption() { Name = "SomeOption" };
+        propertyEnum.AddOption(enumOption);
+        var codeNamespace = parentClass.Parent as CodeNamespace;
+        codeNamespace.AddEnum(propertyEnum);
+        parentClass.AddProperty(new CodeProperty
+        {
+            Name = "propWithDefaultEnum",
+            DefaultValue = enumOption.Name,
+            Type = new CodeType
+            {
+                Name = "EnumTypeWithOption",
+                TypeDefinition = propertyEnum,
+            }
+        });
+
         await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.TypeScript }, root);
         var deserializerFunction = root.FindChildByName<CodeFunction>($"deserializeInto{parentClass.Name.ToFirstCharacterUpperCase()}");
         Assert.NotNull(deserializerFunction);
@@ -342,6 +362,7 @@ public async Task WritesDeSerializerBodyWithDefaultValue()
         writer.Write(deserializerFunction);
         var result = tw.ToString();
         Assert.Contains("?? \"Test Value\"", result);
+        Assert.Contains("?? EnumTypeWithOptionObject.SomeOption", result);
     }
     [Fact]
     public async Task WritesInheritedSerializerBody()
@@ -1032,6 +1053,6 @@ public async Task WritesConstructorWithEnumValue()
         var serializeFunction = root.FindChildByName<CodeFunction>($"Serialize{parentClass.Name.ToFirstCharacterUpperCase()}");
         writer.Write(serializeFunction);
         var result = tw.ToString();
-        Assert.Contains($" ?? {codeEnum.Name.ToFirstCharacterUpperCase()}.{defaultValue.CleanupSymbolName()}", result);//ensure symbol is cleaned up
+        Assert.Contains($" ?? {codeEnum.CodeEnumObject.Name.ToFirstCharacterUpperCase()}.{defaultValue.CleanupSymbolName()}", result);//ensure symbol is cleaned up
     }
 }