Havunen / SystemTextJsonPatch

SystemTextJsonPatch is a JSON Patch (JsonPatchDocument) RFC 6902 implementation for .NET using System.Text.Json
MIT License
102 stars 12 forks source link

.Net 8 and Minimal APIs #27

Closed furyd closed 7 months ago

furyd commented 8 months ago

When using this in a minimal API setup, under .NET 8, I'm getting a circular reference exception when serialising the JsonPatchDocument, and looking at the Options value it's JsonSerializerOptions with a couple of TypeInfoResolvers (ProblemDetailsJsonContext and DefaultJsonTypeInfoResolver) and the ProblemDetails one (ironically enough) is causing the problem.

Workaround is currently to change that value before serialising, wondering if there is a better solution?

Havunen commented 8 months ago

Are you able to provide an example program where the failure occurs, as a zip file or github repository. That should speed up fixing that issue

furyd commented 7 months ago

Hi @Havunen I believe I've found the issue, and whilst it's not with this library directly, it is a case others might hit.

Issue is when using AddProblemDetails() and MassTransit's UseNewtonsoftJsonSerializer() together, the latter being left over from when I was using MS' JsonPatch library.

AddProblemDetails looks to insert a TypeInfoResolver for ProblemDetails, which the JsonPatchDocument now has in its Options property, and UseNewtonsoftJsonSerializer presumably picks the first one, which is now the one injected by AddProblemDetails, which then explodes with the circular reference error.

I imagine there are other combinations like this which might cause the same issue.

I've created an example repo at if you want to take a look. I should have an easy fix by removing UseNewtonsoftJsonSerializer(), but if/when this happens with other libraries, I'm not sure there is a solution to this other than forcing the Options value to the desired TypeInfoResolver.

I'll close this though.