microsoft / kiota-dotnet

Abstractions library for the Kiota generated SDKs in dotnet
https://aka.ms/kiota/docs
MIT License
32 stars 29 forks source link

SendPrimitiveAsync throws InvalidOperationException for enums #274

Closed MartinM85 closed 1 month ago

MartinM85 commented 2 months ago

I've an endpoint which returns enum (C#):

return Ok(MyEnum.Value1);

The generated code for GetAsync looks like this:

return await RequestAdapter.SendPrimitiveAsync<MyEnum?>(requestInfo, errorMapping, cancellationToken).ConfigureAwait(false);

But it throws InvalidOperationException with message error handling the response, unexpected type

I can't find any workaround for it right now

MartinM85 commented 2 months ago

@baywet Out of the scope of this issue, but if the server returns collection of enums, the generated client code is

return await RequestAdapter.SendPrimitiveCollectionAsync<MyEnum?>(requestInfo, errorMapping, cancellationToken).ConfigureAwait(false);

SendPrimitiveCollectionAsync internally calls GetCollectionOfPrimitiveValues https://github.com/microsoft/kiota-serialization-json-dotnet/blob/main/src/JsonParseNode.cs#L310 and it doesn't handle enums.

IParseNode exposes GetCollectionOfEnumValues and GetEnumValue, but they have constraint for T limited to enum. ModelType doesn't have any constraint.

andrueastman commented 2 months ago

Transferring issue as part of https://github.com/microsoft/kiota-abstractions-dotnet/issues/238