Repository: microsoft/kiota (csharp)

Issue:
Add support for Enums or collection of enum values as root of the payload
A good example of that is beta `POST /deviceManagement/chromeOSOnboardingSettings/disconnect` which returns `chromeOSOnboardingStatus` (enum)

Here are the changes we need to make:
1. Request adapter interface need to accept objects as well for the responseType argument of the "sendPrimitiveAsync" and "sendCollectionOfPrimitiveAsync" methods
2. reflect the changes to the implementation
3. add a case in the implementation to call getEnumValue/getCollectionOfEnumValues when an object is provided.
4. update kiota generation to use that method for enums, add a using to the Object of the enum, and pass that object (in CodeConstantWriter for the requests metadata constants)

+ required tests

Pull request:
Fixes: https://github.com/microsoft/kiota-typescript/issues/1042
fixes #4199

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 47407350b6..be1ce6bb4c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Added the init command as part of the experience revamp of [#3356](https://github.com/microsoft/kiota/issues/3356)
 - Added uri-form encoded serialization for Python. [#2075](https://github.com/microsoft/kiota/issues/2075)
 - Added support for multipart form data request body in Python. [#3030](https://github.com/microsoft/kiota/issues/3030)
+- Added support for Enums or collection of enum values as root of the payload. [#1042](https://github.com/microsoft/kiota-typescript/issues/1042)
 
 ### Changed
 
diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs
index bdcf9460ba..d049665cfd 100644
--- a/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs
+++ b/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs
@@ -99,7 +99,9 @@ private void WriteRequestsMetadataConstant(CodeConstant codeElement, LanguageWri
             var returnType = conventions.GetTypeString(executorMethod.ReturnType, codeElement);
             var isVoid = "void".EqualsIgnoreCase(returnType);
             var isStream = conventions.StreamTypeName.Equals(returnType, StringComparison.OrdinalIgnoreCase);
+            var isEnum = executorMethod.ReturnType is CodeType codeType && codeType.TypeDefinition is CodeEnum;
             var returnTypeWithoutCollectionSymbol = GetReturnTypeWithoutCollectionSymbol(executorMethod, returnType);
+            var isPrimitive = conventions.IsPrimitiveType(returnTypeWithoutCollectionSymbol);
             writer.StartBlock($"{executorMethod.Name.ToFirstCharacterLowerCase()}: {{");
             var urlTemplateValue = executorMethod.HasUrlTemplateOverride ? $"\"{executorMethod.UrlTemplateOverride}\"" : uriTemplateConstant.Name.ToFirstCharacterUpperCase();
             writer.WriteLine($"uriTemplate: {urlTemplateValue},");
@@ -115,10 +117,13 @@ private void WriteRequestsMetadataConstant(CodeConstant codeElement, LanguageWri
                 }
                 writer.CloseBlock("},");
             }
-            writer.WriteLine($"adapterMethodName: \"{GetSendRequestMethodName(isVoid, isStream, executorMethod.ReturnType.IsCollection, returnTypeWithoutCollectionSymbol)}\",");
-            if (!isVoid)
+            writer.WriteLine($"adapterMethodName: \"{GetSendRequestMethodName(isVoid, isStream, executorMethod.ReturnType.IsCollection, isPrimitive, isEnum)}\",");
+            if (isEnum)
+                writer.WriteLine($"enumObject: \"{executorMethod.ReturnType.Name.ToFirstCharacterUpperCase()}\",");
+            else if (!isVoid)
                 writer.WriteLine($"responseBodyFactory: {GetTypeFactory(isVoid, isStream, executorMethod, writer)},");
             var sanitizedRequestBodyContentType = executorMethod.RequestBodyContentType.SanitizeDoubleQuote();
+
             if (!string.IsNullOrEmpty(sanitizedRequestBodyContentType))
                 writer.WriteLine($"requestBodyContentType: \"{sanitizedRequestBodyContentType}\",");
             if (executorMethod.Parameters.FirstOrDefault(static x => x.Kind is CodeParameterKind.RequestBody) is CodeParameter requestBody)
@@ -186,17 +191,25 @@ currentType.TypeDefinition is CodeClass definitionClass &&
         }
         throw new InvalidOperationException($"Unable to find factory method for {targetClassName}");
     }
-    private string GetSendRequestMethodName(bool isVoid, bool isStream, bool isCollection, string returnType)
+
+    private string GetSendRequestMethodName(bool isVoid, bool isStream, bool isCollection, bool isPrimitive, bool isEnum)
     {
-        if (isVoid) return "sendNoResponseContent";
-        if (isCollection)
+        if (isVoid)
         {
-            if (conventions.IsPrimitiveType(returnType)) return $"sendCollectionOfPrimitive";
-            return $"sendCollection";
+            return "sendNoResponseContent";
+        }
+        else if (isEnum)
+        {
+            return isCollection ? "sendCollectionOfEnum" : "sendEnum";
+        }
+        else if (isPrimitive || isStream)
+        {
+            return isCollection ? "sendCollectionOfPrimitive" : "sendPrimitive";
+        }
+        else
+        {
+            return isCollection ? "sendCollection" : "send";
         }
-
-        if (isStream || conventions.IsPrimitiveType(returnType)) return $"sendPrimitive";
-        return $"send";
     }
 
     private void WriteUriTemplateConstant(CodeConstant codeElement, LanguageWriter writer)


Test patch:
diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs
index d3210509d7..2d228419e9 100644
--- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs
+++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs
@@ -187,6 +187,108 @@ public void WritesRequestExecutorBodyForCollections()
         AssertExtensions.CurlyBracesAreClosed(result);
     }
     [Fact]
+    public void WritesRequestExecutorForEnum()
+    {
+        AddCodeEnum();
+        method.Kind = CodeMethodKind.RequestExecutor;
+        method.HttpMethod = HttpMethod.Get;
+        method.ReturnType = new CodeType
+        {
+            Name = "SomeComplexTypeForRequestBody",
+            TypeDefinition = currentEnum,
+        };
+        AddRequestBodyParameters();
+        var constant = CodeConstant.FromRequestBuilderToRequestsMetadata(parentClass);
+        var codeFile = parentClass.GetImmediateParentOfType<CodeNamespace>().TryAddCodeFile("foo", constant);
+        codeFile.AddElements(new CodeConstant
+        {
+            Name = "UriTemplate",
+            Kind = CodeConstantKind.UriTemplate,
+            UriTemplate = "{baseurl+}/foo/bar"
+        });
+        writer.Write(constant);
+        var result = tw.ToString();
+        Assert.Contains("sendEnum", result);
+        Assert.Contains("enumObject:", result);
+        Assert.Contains(EnumName.ToFirstCharacterUpperCase(), result);
+        AssertExtensions.CurlyBracesAreClosed(result);
+    }
+    [Fact]
+    public void WritesRequestExecutorForEnumCollection()
+    {
+        AddCodeEnum();
+        method.Kind = CodeMethodKind.RequestExecutor;
+        method.HttpMethod = HttpMethod.Get;
+        method.ReturnType = new CodeType
+        {
+            Name = "SomeComplexTypeForRequestBody",
+            TypeDefinition = currentEnum,
+            CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array,
+        };
+        AddRequestBodyParameters();
+        var constant = CodeConstant.FromRequestBuilderToRequestsMetadata(parentClass);
+        var codeFile = parentClass.GetImmediateParentOfType<CodeNamespace>().TryAddCodeFile("foo", constant);
+        codeFile.AddElements(new CodeConstant
+        {
+            Name = "UriTemplate",
+            Kind = CodeConstantKind.UriTemplate,
+            UriTemplate = "{baseurl+}/foo/bar"
+        });
+        writer.Write(constant);
+        var result = tw.ToString();
+        Assert.Contains("sendCollectionOfEnum", result);
+        Assert.Contains("enumObject:", result);
+        Assert.Contains(EnumName.ToFirstCharacterUpperCase(), result);
+        AssertExtensions.CurlyBracesAreClosed(result);
+    }
+    [Fact]
+    public void WritesRequestExecutorForPrimitive()
+    {
+        method.Kind = CodeMethodKind.RequestExecutor;
+        method.HttpMethod = HttpMethod.Get;
+        method.ReturnType = new CodeType
+        {
+            Name = "string",
+        };
+        AddRequestBodyParameters();
+        var constant = CodeConstant.FromRequestBuilderToRequestsMetadata(parentClass);
+        var codeFile = parentClass.GetImmediateParentOfType<CodeNamespace>().TryAddCodeFile("foo", constant);
+        codeFile.AddElements(new CodeConstant
+        {
+            Name = "UriTemplate",
+            Kind = CodeConstantKind.UriTemplate,
+            UriTemplate = "{baseurl+}/foo/bar"
+        });
+        writer.Write(constant);
+        var result = tw.ToString();
+        Assert.Contains("sendPrimitive", result);
+        AssertExtensions.CurlyBracesAreClosed(result);
+    }
+    [Fact]
+    public void WritesRequestExecutorForPrimitiveCollection()
+    {
+        method.Kind = CodeMethodKind.RequestExecutor;
+        method.HttpMethod = HttpMethod.Get;
+        method.ReturnType = new CodeType
+        {
+            Name = "string",
+            CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array,
+        };
+        AddRequestBodyParameters();
+        var constant = CodeConstant.FromRequestBuilderToRequestsMetadata(parentClass);
+        var codeFile = parentClass.GetImmediateParentOfType<CodeNamespace>().TryAddCodeFile("foo", constant);
+        codeFile.AddElements(new CodeConstant
+        {
+            Name = "UriTemplate",
+            Kind = CodeConstantKind.UriTemplate,
+            UriTemplate = "{baseurl+}/foo/bar"
+        });
+        writer.Write(constant);
+        var result = tw.ToString();
+        Assert.Contains("sendCollectionOfPrimitive", result);
+        AssertExtensions.CurlyBracesAreClosed(result);
+    }
+    [Fact]
     public void WritesRequestGeneratorBodyForScalar()
     {
         parentClass.Kind = CodeClassKind.RequestBuilder;