Skip to content

Fix DataContractSerializer nullable struct initialization cache lookup#128506

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-datacontractserializer-issue
Draft

Fix DataContractSerializer nullable struct initialization cache lookup#128506
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-datacontractserializer-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 22, 2026

Description

DataContractSerializer regressed in .NET 8+ when deserializing Nullable<T> for [DataContract] structs that had not been initialized in the cache path yet, causing SerializationException (DataContract cache overflow).
The initialization lookup loop compared against the wrong cache slot, so valid contracts were not found.

  • Runtime fix (System.Private.DataContractSerialization)

    Corrected DataContract.GetIdForInitialization(ClassDataContract) to scan cache entries by loop index instead of repeatedly reading the originally computed id.

    for (int i = 0; i < currentDataContractId; i++)
    {
        if (ContractMatches(classContract, s_dataContractCache.GetItem(i)))
        {
            return i;
        }
    }
  • Regression coverage (System.Runtime.Serialization.Xml tests)

    Added a focused test that round-trips a nullable root [DataContract] struct and verifies successful deserialization without requiring prior serialization of the non-nullable struct type.

Copilot AI requested review from Copilot and removed request for Copilot May 22, 2026 23:29
Copilot AI changed the title [WIP] Fix DataContractSerializer bug with nullable structs in .NET 8 Fix DataContractSerializer nullable struct initialization cache lookup May 22, 2026
Copilot AI requested a review from StephenMolloy May 22, 2026 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataContractSerializer bug when serializing nullable structs in .net >= 8.0

2 participants