Repository: microsoft/kiota (csharp)

Issue:
Null results for GitHub users GET request
### What are you generating using Kiota, clients or plugins?

API Client/SDK

### In what context or format are you using Kiota?

Nuget tool

### Client library/SDK language

Csharp

### Describe the bug

I am trying to get users from the GitHub API, like `var users = await gitHubClient.Users["kfcampbell"].GetAsync();`

I'm using the [dotnet-sdk](https://github.com/octokit/dotnet-sdk) generated from Kiota to do this, but it will reproduce with a fresh SDK generated from the Kiota CLI. The offending code is this generated code: 

```csharp
            public static WithUsernameGetResponse CreateFromDiscriminatorValue(IParseNode parseNode)
            {
                _ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
                var mappingValue = parseNode.GetChildNode("")?.GetStringValue();
                var result = new WithUsernameGetResponse();
                if("private-user".Equals(mappingValue, StringComparison.OrdinalIgnoreCase))
                {
                    result.PrivateUser = new GitHub.Models.PrivateUser();
                }
                else if("public-user".Equals(mappingValue, StringComparison.OrdinalIgnoreCase))
                {
                    result.PublicUser = new GitHub.Models.PublicUser();
                }
                return result;
            }
```

When generating, the above code lives in a file called `Users/Item/WithUsernameItemRequestBuilder.cs`. What happens is that `mappingValue` is always null, which means the `if` statements never trigger, which means the returned users are always null in the result.

### Expected behavior

I expect the returned users to be populated correctly.

### How to reproduce

You may use the [dotnet-sdk](https://github.com/octokit/dotnet-sdk) and modify the CLI to include the following C# code:

```csharp
using GitHub;
using GitHub.Octokit.Client;
using GitHub.Octokit.Client.Authentication;

var token = Environment.GetEnvironmentVariable("GITHUB_TOKEN") ?? "";
var request = RequestAdapter.Create(new TokenAuthProvider(new TokenProvider(token)));
var gitHubClient = new GitHubClient(request);

var users = await gitHubClient.Users["kfcampbell"].GetAsync();
```

Then run `dotnet build`, export your `GITHUB_TOKEN`, and run `dotnet run`. Alternately, you may generate a fresh C# SDK from the Kiota CLI, create your own CLI, input that code, and follow the same steps.

### Open API description file

https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.json

### Kiota Version

v1.14.0

### Latest Kiota version known to work for scenario above?(Not required)

_No response_

### Known Workarounds

Manually removing the `if` statements so the generated code reads like:

```csharp
            public static WithUsernameGetResponse CreateFromDiscriminatorValue(IParseNode parseNode)
            {
                _ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
                var mappingValue = parseNode.GetChildNode("")?.GetStringValue();
                var result = new WithUsernameGetResponse();
                result.PrivateUser = new GitHub.Models.PrivateUser();
                result.PublicUser = new GitHub.Models.PublicUser();

                return result;
            }
```

will fix the issue. 

### Configuration

This occurs both on my machine (an Ubuntu 22.04 derivative, 64-bit Linux box) and GitHub Actions (Ubuntu 22.04, 64-bit Linux). I do not believe it is specific to this configuration. 

### Debug output

N/A

### Other information

_No response_

Pull request:
Fixes: https://github.com/microsoft/kiota/issues/4844

### What
When generating Go based SDKs from GitHub's OpenAPI [definitions](https://github.com/github/rest-api-description/tree/main/descriptions/api.github.com) we noticed that the `GetFieldDeserializers` function was empty for any operations using discriminators and `OneOf`

### Why
It turns out that the issue is in WriteDeserializerBodyForUnionModel or at least why the method body is not getting generated.
So the LINQ statement for Go is:

```
var otherPropGetters = parentClass
 .GetPropertiesOfKind(CodePropertyKind.Custom)
 .Where(static x => !x.ExistsInBaseType && x.Getter != null)
 .Where(static x => x.Type is CodeType propertyType && !propertyType.IsCollection && propertyType.TypeDefinition is CodeClass)
 .OrderBy(static x => x, CodePropertyTypeForwardComparer)
 .ThenBy(static x => x.Name)
 .Select(static x => x.Getter!.Name.ToFirstCharacterUpperCase())
 .ToArray();
```
The problem is that for these discriminators the TypeDefinition is not of type CodeClass but of type CodeInterface
When the LINQ is rewritten like this, it works:
```
var otherPropGetters = parentClass
 .GetPropertiesOfKind(CodePropertyKind.Custom)
 .Where(static x => !x.ExistsInBaseType && x.Getter != null)
 .Where(static x => x.Type is CodeType propertyType && !propertyType.IsCollection && propertyType.TypeDefinition is CodeInterface)
 .OrderBy(static x => x, CodePropertyTypeForwardComparer)
 .ThenBy(static x => x.Name)
 .Select(static x => x.Getter!.Name.ToFirstCharacterUpperCase())
 .ToArray();
```

- [x] Tests have been updated
- [x] Change log has been updated
- [x] Verified to be working using GitHub's OpenAPI [definitions](https://github.com/github/rest-api-description/tree/main/descriptions/api.github.com)

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 23ea913175..d02c06c805 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ### Changed
 
+- Fixed an issue where when generating Go code the deserializer for unions was using `CodeClass` as a filter and not `CodeInterface`. [#4844](https://github.com/microsoft/kiota/issues/4844)
 - Fixes mapping of `int16` format to the `integer` type rather than `double` when the type is `integer` or `number` [#5611](https://github.com/microsoft/kiota/issues/5611)
 
 ## [1.19.1] - 2024-10-11
diff --git a/src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
index 8a50297c80..59a288062b 100644
--- a/src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
+++ b/src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
@@ -632,7 +632,7 @@ private static void WriteDeserializerBodyForUnionModel(CodeMethod method, CodeCl
         var otherPropGetters = parentClass
                                 .GetPropertiesOfKind(CodePropertyKind.Custom)
                                 .Where(static x => !x.ExistsInBaseType && x.Getter != null)
-                                .Where(static x => x.Type is CodeType propertyType && !propertyType.IsCollection && propertyType.TypeDefinition is CodeClass)
+                                .Where(static x => x.Type is CodeType propertyType && !propertyType.IsCollection && propertyType.TypeDefinition is CodeInterface)
                                 .OrderBy(static x => x, CodePropertyTypeForwardComparer)
                                 .ThenBy(static x => x.Name)
                                 .Select(static x => x.Getter!.Name.ToFirstCharacterUpperCase())


Test patch:
diff --git a/tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs
index 771c815782..885af77aae 100644
--- a/tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs
+++ b/tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs
@@ -291,16 +291,20 @@ private void AddSerializationBackingStoreMethods()
     }
     private CodeClass AddUnionTypeWrapper()
     {
-        var complexType1 = root.AddClass(new CodeClass
+        var complexType1 = root.AddInterface(new CodeInterface
         {
             Name = "ComplexType1",
-            Kind = CodeClassKind.Model,
+            Kind = CodeInterfaceKind.Model,
+            OriginalClass = new CodeClass { Name = "ComplexType1", Kind = CodeClassKind.Model }
         }).First();
-        var complexType2 = root.AddClass(new CodeClass
+
+        var complexType2 = root.AddInterface(new CodeInterface
         {
             Name = "ComplexType2",
-            Kind = CodeClassKind.Model,
+            Kind = CodeInterfaceKind.Model,
+            OriginalClass = new CodeClass { Name = "ComplexType2", Kind = CodeClassKind.Model }
         }).First();
+
         var unionTypeWrapper = root.AddClass(new CodeClass
         {
             Name = "UnionTypeWrapper",