Fix DataContractJsonSerializer not passing ISerializationSurrogateProvider to internal serializer#118257
Conversation
|
Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it. |
|
@copilot I left some comments in review. Please update the PR with suggested changes. |
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug in DataContractJsonSerializer where the ISerializationSurrogateProvider was not being properly passed to the internal XML serializer contexts, preventing surrogate providers from functioning correctly during JSON serialization/deserialization.
Key changes:
- Added new constructors to JSON serializer context classes to accept and pass through the surrogate provider
- Fixed RootContract property to use surrogate type resolution when a provider is available
- Added comprehensive test coverage with tracking properties to verify surrogate provider method calls
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| SerializationTypes.RuntimeOnly.cs | Added tracking properties to MyPersonSurrogateProvider for test validation |
| DataContractJsonSerializer.cs (tests) | Added comprehensive test to verify surrogate provider integration |
| XmlObjectSerializerWriteContextComplex.cs | Added constructor overload accepting ISerializationSurrogateProvider |
| XmlObjectSerializerReadContextComplex.cs | Added constructor overload accepting ISerializationSurrogateProvider |
| XmlObjectSerializerWriteContextComplexJson.cs | Updated constructor call to pass surrogate provider to base class |
| XmlObjectSerializerReadContextComplexJson.cs | Updated constructor call to pass surrogate provider to base class |
| DataContractJsonSerializer.cs | Fixed RootContract property and added GetSurrogatedType helper method |
|
@StephenMolloy are you still working on this? |
Yup. I was cleaning up the serialization area towards the end of 10 and ultimately decided that this didn't make the cut. But I've got a couple of copilot PR's to review when I get back into serialization for 11 - soon. |
…r to internal serializer contexts Co-authored-by: StephenMolloy <19562826+StephenMolloy@users.noreply.github.com>
Co-authored-by: StephenMolloy <19562826+StephenMolloy@users.noreply.github.com>
…ontract pattern, remove cache invalidation Co-authored-by: StephenMolloy <19562826+StephenMolloy@users.noreply.github.com>
cbc35cf to
16cf4fc
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…izer and DataContractSerializer
🤖 Copilot Code Review — PR #118257Note This review was generated by GitHub Copilot. Holistic AssessmentMotivation: The PR fixes a real, verified bug: Approach: The fix correctly mirrors how Summary: ✅ LGTM. The fix is correct, minimal, and consistent with the existing Detailed Findings✅ Core Fix — Surrogate provider propagation to contextsThe root cause is properly addressed. Before this PR, // Before (broken):
: base(serializer, serializer.MaxItemsInObjectGraph, ..., serializer.IgnoreExtensionDataObject)The fix adds a 5th parameter to pass the surrogate provider: // After (fixed):
: base(serializer, serializer.MaxItemsInObjectGraph, ..., serializer.IgnoreExtensionDataObject, serializer.SerializationSurrogateProvider)This is identical to how ✅ RootContract surrogate type resolutionThe _rootContract = DataContract.GetDataContract(
(_serializationSurrogateProvider == null)
? _rootType
: GetSurrogatedType(_serializationSurrogateProvider, _rootType));This is an exact match of the pattern used in ✅ Cache invalidation deliberately omitted — consistent with DCSThe commit history shows cache invalidation ( ✅ Test coverage — mirrors DCS testsBoth ✅ No public API surface changesThe 💡 Minor code duplication —
|
|
Added the surrogate test coverage that was identified as missing. However, I chose not to add Consistency with Historical contract from .NET 4.8. In .NET Framework, both DCS and DCJS accepted surrogates exclusively at construction time (via constructor parameters or settings objects). This made Immutability of Mitigation. Rather than invalidating the cache, the appropriate response is to document that surrogate providers must be set before the serializer is first used — consistent with how .NET 4.8 enforced this structurally. A debug-build |
mangod9
left a comment
There was a problem hiding this comment.
Overall looks good and is low risk.
Summary
Successfully implemented and refined DataContractJsonSerializer surrogate provider support:
The fix ensures DataContractJsonSerializer now properly respects SetSerializationSurrogateProvider() calls, matching the behavior of DataContractSerializer.
Fixes: #100553