Fix issues related to JsonSerializerOptions mutation and caching.#65863
Fix issues related to JsonSerializerOptions mutation and caching.#65863eiriktsarpalis merged 3 commits intodotnet:mainfrom
Conversation
|
Added When you commit this breaking change:
Tagging @dotnet/compat for awareness of the breaking change. |
|
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsSupersedes #65799. After conversation with @layomia in #65799 (comment) I realized that the caching implementation introduced in #64646 contains a bug. More specifically, a locked/immutable
Both of these changes alter the
|
25f9632 to
40cf07a
Compare
...ries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs
Show resolved
Hide resolved
...ries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs
Show resolved
Hide resolved
...Json/tests/System.Text.Json.Tests/Serialization/CustomConverterTests/CustomConverterTests.cs
Show resolved
Hide resolved
…tnet#65863) * Fix issues related to JsonSerializerOptions mutation and caching. * fix test style * fix linker warning
|
Apologies for that, I've reposted the changes in #66248 with relevant tests disabled in netfx. |
|
I'm removing the breaking-change label since in hindsight, the removed behavior (i.e. ability to add a |
Supersedes #65799. After conversation with @layomia in #65799 (comment) I realized that the caching implementation introduced in #64646 contains a bug. More specifically, a locked/immutable
JsonSerializerOptionsinstance can still be mutated by the serialization infrastructure in couple of important ways:JsonSerializerContextfield._typeInfoCreationFuncfield if the options instance has been passed to a reflection serialization API.Both of these changes alter the
JsonTypeInforesolution semantics in important ways, and as such they can invalidate the current contents of the metadata cache. This PR makes the following changes:JsonSerializerContextvalues can now only be attached to mutableJsonSerializerOptionsinstances. This is a potential breaking change, however the new throwing behavior is arguably better than the current one (nondeterministic use of serialization metadata).IsInitializedForReflectionSerializeran instance method instead of a static, and ensure it gets propagated correctly to shared caching contexts.I ran the benchmark suite and no performance regressions were recorded.