Skip to content

.NET Core 3.0-preview9-014004 :: System.Text.Json.JsonException #30870

Closed

Description

@jonmikeli

Issue Title

System.Text.Json.JsonException: A possible object cycle was detected which is not supported.

Description

The exception is raised when trying to serialize an Exception in:

  • UseExceptionHandler()
  • MiddleWare loaded by UseMiddleware()

I have not tried the serialization in other contexts.
Replacing the serialization call by another serializer (ex: Newtonsoft.Json) works properly.

Versions

.Net Core 3.0.100-preview9-014004

Code

var result = JsonSerializer.Serialize<Exception>(exception, new JsonSerializerOptions { WriteIndented = true });

Exception detail

Exception thrown: 'System.Text.Json.JsonException' in System.Text.Json.dll
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware: Error: An exception was thrown attempting to execute the error handler.

System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 0.
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerCycleDetected(Int32 maxDepth)
at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteCore(Utf8JsonWriter writer, PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCore(PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCoreString(Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
at NETCore3APITemplate.API.Startup.<>c__DisplayClass12_0.<b__3>d.MoveNext() in D:\Sources\JMI\Git\ALMTools-Templates-DotNetCore-Simple\Sources\WorkingSolution\NETCore3APITemplate\NETCore3APITemplate\NETCore3APITemplate.API\Startup.cs:line 147
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi)
Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer: Error: Connection ID "18230571301796315147", Request ID "8000000f-0002-fd00-b63f-84710c7967bb": An unhandled exception was thrown by the application.

Packages references

<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.0.0-preview8.19405.7" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="4.0.0-preview8.19405.7" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc2" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.0.0-rc2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0-rc2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="5.0.0-rc2" />

Hope it helps.

Bests

Activity

  1. leecow commented on Sep 16, 2019

    @leecow
    Member
  2. transferred this issue fromdotnet/coreon Sep 16, 2019
  3. ahsonkhan commented on Sep 16, 2019

    @ahsonkhan
    Contributor

    @jonmikeli - can you share what is your .NET object model that you are trying to serialize? Are you actually trying to serialize the built-in System.Exception type?

    If your object model has cycles, then this is the expected behavior since cycles are not supported as part of this release.
    https://github.com/dotnet/corefx/issues/38579
    https://github.com/dotnet/corefx/issues/37786

  4. jonmikeli commented on Sep 16, 2019

    @jonmikeli
    Author

    Hi @ahsonkhan,

    Thanks for your answer.

    I was trying to serialize the built-in System.Exception type.
    Knowing that the property InnerException is of type System.Exception too, I guess that is the reason of the error.

  5. GGAlanSmithee commented on Sep 17, 2019

    @GGAlanSmithee

    sorry for hijacking this issue, but since it's the most recent one - @ahsonkhan you mentioned that cyclic reference handling is on the roadmap for vNext - https://github.com/dotnet/corefx/issues/38579#issuecomment-502955776 is there any available roadmap for this? thanks

  6. ahsonkhan commented on Sep 17, 2019

    @ahsonkhan
    Contributor

    @ahsonkhan you mentioned that cyclic reference handling is on the roadmap for vNext - https://github.com/dotnet/corefx/issues/38579#issuecomment-502955776 is there any available roadmap for this? thanks

    Yes, @GGAlanSmithee - see https://github.com/dotnet/corefx/issues/41002 where @jozkee is working on designing the feature. Your feedback, usage, and scenario would be helpful there.

    sorry for hijacking this issue,

    No problem, please feel free to participate on that issue.

  7. jozkee commented on Jan 21, 2020

    @jozkee
    Member

    The preview version for ReferenceHandling feature is here.

    var options = new JsonSerializerOptions
    {
        ReferenceHandling = ReferenceHandling.Preserve
    };
    
    string json = JsonSerializer.Serialize(objectWithLoops, options);

    Here's the spec doc including samples and notes about many scenarios.
    If you want to add feedback, feel free to leave a comment in https://github.com/dotnet/corefx/issues/41002.

  8. transferred this issue fromdotnet/corefxon Feb 1, 2020
  9. added this to the 3.0 milestone on Feb 1, 2020
  10. danich93 commented on May 13, 2020

    @danich93

    @jozkee this doesn't seem to work when applied "globally" like so:

    services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.ReferenceHandling = ReferenceHandling.Preserve);

  11. jozkee commented on May 13, 2020

    @jozkee
    Member

    @Ohmu93 That's probably because of a know ASP.NET issue where they were not honoring configuration of newly added properties to JsonSerializerOptions.

    Can you try the workaround described in #30820 (comment)? Once 5.0 is relased, this should no longer be an issue.

    You can also try upgrading your ASP.NET version to 5.0.0-preview4 since this was already fixed in dotnet/aspnetcore#21016.

    Let me know if that solves your problem.

  12. danich93 commented on May 14, 2020

    @danich93

    @jozkee Yep, that workaround does the trick, cheers.

  13. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions