microsoftgraph / microsoftgraph/msgraph-sdk-dotnet

'GetAsync' throw 'System.Text.Json.JsonReaderException' while trying to read mails from mailbox folders

Open
#2,877 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Status: Needs investigation type:bug
Dominant language
C#
Stars
789
Forks
264
Avg merge
15h 17m
Merged PRs (30d)
3

Description

Describe the bug

Hi,

Graph SDK: NuGet "Microsoft.Graph" Version="5.73.0"
C# (dotnet 8)

I`m getting 'System.Text.Json.JsonReaderException' exception.

This exception is not being thrown consistently.

This is a snipped code of the Graph SDK useage:

GraphClient
                              .Users[mUser.Id]
                              .MailFolders[iFolder.Id]
                              .Messages
                              .GetAsync(iConfig => {
                                            iConfig.QueryParameters.Select = [
                                                "from",
                                                "receivedDateTime",
                                                "createdDateTime",
                                                "subject",
                                                "id",
                                                "hasAttachments",
                                                "body",
                                                "attachments"
                                            ];
                                            iConfig.QueryParameters.Expand = ["attachments"];
                                            iConfig.QueryParameters.Filter = iFilter;
                                            iConfig.QueryParameters.Orderby = ["receivedDateTime DESC"];
                                            iConfig.QueryParameters.Top = rOffice365Settings.MailsCountToRead;
                                            iConfig.Headers.Add("Prefer", $"outlook.body-content-type=\"{rOffice365Settings.PreferredContentType}\"");
                                            iConfig.Headers.Add("Accept-Language", rOffice365Settings.AcceptLanguage);
                                        },
                                        cancellationToken: iCancellationToken).ConfigureAwait(false);

The mail box language is in Espanol (Costa Rica) so I tried to play with the settings but it did not helped.

  • Reduce mails fetch count.
  • Changed AcceptLanguage for both English and Spanish.

When:
AcceptLanguage: en-US,
PreferredContentType: text,
mailsCountToReadPerIteration: 10

I got:
Error from System.Text.Json: System.Text.Json.JsonReaderException: 'e' is invalid after a value. Expected either ',', '}', or ']'. LineNumber: 0 | BytePositionInLine: 7464595. at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan1 bytes)
at System.Text.Json.Utf8JsonReader.ConsumeNextToken(Byte marker)
at System.Text.Json.Utf8JsonReader.ReadSingleSegment()
at System.Text.Json.JsonDocument.Parse(ReadOnlySpan1 utf8JsonSpan, JsonReaderOptions readerOptions, MetadataDb& database, StackRowStack& stack) at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory1 utf8Json, JsonReaderOptions readerOptions, Byte[] extraRentedArrayPoolBytes, PooledByteBufferWriter extraPooledByteBufferWriter)
at System.Text.Json.JsonDocument.ParseAsyncCore(Stream utf8Json, JsonDocumentOptions options, CancellationToken cancellationToken)
at Microsoft.Kiota.Serialization.Json.JsonParseNodeFactory.GetRootParseNodeAsync(String contentType, Stream content, CancellationToken cancellationToken)
at Microsoft.Kiota.Abstractions.Serialization.ParseNodeProxyFactory.GetRootParseNodeAsync(String contentType, Stream content, CancellationToken cancellationToken)
at Microsoft.Kiota.Abstractions.Serialization.ParseNodeFactoryRegistry.GetRootParseNodeAsync(String contentType, Stream content, CancellationToken cancellationToken)
at Microsoft.Kiota.Abstractions.Serialization.ParseNodeProxyFactory.GetRootParseNodeAsync(String contentType, Stream content, CancellationToken cancellationToken)
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.GetRootParseNodeAsync(HttpResponseMessage response, CancellationToken cancellationToken)
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory1 factory, Dictionary2 errorMapping, CancellationToken cancellationToken)
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory1 factory, Dictionary2 errorMapping, CancellationToken cancellationToken)
at Microsoft.Graph.Users.Item.MailFolders.Item.Messages.MessagesRequestBuilder.GetAsync(Action1 requestConfiguration, CancellationToken cancellationToken)

When:
AcceptLanguage: es-ES
PreferredContentType: text
MailsCountToRead: 10

I got:

Error from System.Text.Json: System.Text.Json.JsonReaderException: Expected depth to be zero at the end of the JSON payload. There is an open JSON object or array that should be closed. LineNumber: 0 | BytePositionInLine: 361.
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.ReadSingleSegment()
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.JsonDocument.Parse(ReadOnlySpan`1 utf8JsonSpan, JsonReaderOptions readerOptions, MetadataDb& database, StackRowStack& stack)
   at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 utf8Json, JsonReaderOptions readerOptions, Byte[] extraRentedArrayPoolBytes, PooledByteBufferWriter extraPooledByteBufferWriter)
   at System.Text.Json.JsonDocument.ParseAsyncCore(Stream utf8Json, JsonDocumentOptions options, CancellationToken cancellationToken)
   at Microsoft.Kiota.Serialization.Json.JsonParseNodeFactory.GetRootParseNodeAsync(String contentType, Stream content, CancellationToken cancellationToken)
   at Microsoft.Kiota.Abstractions.Serialization.ParseNodeProxyFactory.GetRootParseNodeAsync(String contentType, Stream content, CancellationToken cancellationToken)
   at Microsoft.Kiota.Abstractions.Serialization.ParseNodeFactoryRegistry.GetRootParseNodeAsync(String contentType, Stream content, CancellationToken cancellationToken)
   at Microsoft.Kiota.Abstractions.Serialization.ParseNodeProxyFactory.GetRootParseNodeAsync(String contentType, Stream content, CancellationToken cancellationToken)
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.GetRootParseNodeAsync(HttpResponseMessage response, CancellationToken cancellationToken)
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory`1 factory, Dictionary`2 errorMapping, CancellationToken cancellationToken)
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory`1 factory, Dictionary`2 errorMapping, CancellationToken cancellationToken)
   at Microsoft.Graph.Users.Item.MailFolders.Item.Messages.MessagesRequestBuilder.GetAsync(Action`1 requestConfiguration, CancellationToken cancellationToken)

Please advice how to continue.

Expected behavior

Enable to read emails

How to reproduce

I`m not sure but maybe set the mailbox language to Spanish

SDK Version

5.73.0

Latest version known to work for scenario above?

No response

Known Workarounds

No response

Debug output
Click to expand log ```
</details>


### Configuration

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the MessagesRequestBuilder.GetAsync call and the HttpClientRequestAdapter stack shown in the report. Reproduce the mailbox-folder request with the listed Graph SDK 5.73.0 query and locale variations, then inspect the returned payload near the reported JSON byte positions. Done means the cause is isolated and the request can read the affected emails without a JsonReaderException.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.