microsoftgraph / msgraph-beta-sdk-dotnet

The Microsoft Graph Client Beta Library for .NET supports the Microsoft Graph /beta endpoint. (preview)
Other
96 stars 32 forks source link

Exception "The given key was not present in the dictionary." after upgrade to 5.4.0-preview #461

Closed MarkusBux closed 2 years ago

MarkusBux commented 2 years ago

After upgrading the NuGet package to version 5.4.0-preview I get a System.Collections.Generic.KeyNotFoundException. Message: The given key was not present in the dictionary.

Rolling back to 5.3.0-preview solves the issue.

code that throws the exception:

var devices = await graphServiceClient
    .DeviceManagement
    .ManagedDevices
    .GetAsync(cancellationToken: cancellationToken); 

Stacktrace

   at System.Text.Json.JsonElement.GetProperty(String propertyName)
   at Microsoft.Kiota.Serialization.Json.JsonParseNode.GetChildNode(String identifier)
   at Microsoft.Graph.Beta.Models.ManagedDevice.CreateFromDiscriminatorValue(IParseNode parseNode)
   at Microsoft.Kiota.Serialization.Json.JsonParseNode.GetObjectValue[T](ParsableFactory`1 factory)
   at Microsoft.Kiota.Serialization.Json.JsonParseNode.<GetCollectionOfObjectValues>d__17`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Microsoft.Graph.Beta.Models.ManagedDeviceCollectionResponse.<GetFieldDeserializers>b__15_1(IParseNode n)
   at Microsoft.Kiota.Serialization.Json.JsonParseNode.AssignFieldValues[T](T item, IDictionary`2 fieldDeserializers)
   at Microsoft.Kiota.Serialization.Json.JsonParseNode.GetObjectValue[T](ParsableFactory`1 factory)
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.<SendAsync>d__14`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.<SendAsync>d__14`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.Graph.Beta.DeviceManagement.ManagedDevices.ManagedDevicesRequestBuilder.<GetAsync>d__30.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at ConsoleTest.GraphSamples.ManagedDeviceService.<GetDevicesAsync>d__3.MoveNext() in /Users/username/Work/Temp/TestApp/Console/GraphSamples/ManagedDeviceService.cs:line 60
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
   at ConsoleTest.Program.<Main>d__0.MoveNext() in /Users/username/Work/Temp/TestApp/Console/Program.cs:line 42
andrueastman commented 2 years ago

Hey @MarkusBux,

Thanks for raising this.

It looks like the introduction of the discriminator values in the reference openApi document resulted in generation of extra cases at this code which results in an error getting thrown when the response payload does not have the key value in Json response.

https://github.com/microsoftgraph/msgraph-beta-sdk-dotnet/blob/7c1b8c6b0d22c8a5f80dad08f2e0d1e24528050b/src/Microsoft.Graph/Generated/Models/ManagedDevice.cs#L460

Ideally, we should handle this gracefully and will implement a fix in the serialization library to resolve this.