Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix DataContractSerializer nullable struct initialization cache lookup - #128506

#128506
Merged
StephenMolloy merged 3 commits into
maindotnet/runtime:mainfrom
copilot/fix-datacontractserializer-issuedotnet/runtime:copilot/fix-datacontractserializer-issueCopy head branch name to clipboard
Jul 9, 2026
Merged

Fix DataContractSerializer nullable struct initialization cache lookup#128506
StephenMolloy merged 3 commits into
maindotnet/runtime:mainfrom
copilot/fix-datacontractserializer-issuedotnet/runtime:copilot/fix-datacontractserializer-issueCopy head branch name to clipboard

Conversation

Copilot AI commented May 22, 2026

Copy link
Copy Markdown
Contributor

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 DCS_NullableDataContractStructAsRoot_DeserializesWithoutPriorInitialization, a cold-cache deserialization test that deserializes a nullable root [DataContract] struct from a known XML payload using ReadObject only, with no prior WriteObject call that could pre-initialize the cache and mask the regression.

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

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
@StephenMolloy StephenMolloy added this to the 11.0.0 milestone Jun 16, 2026

@StephenMolloy StephenMolloy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@StephenMolloy
StephenMolloy marked this pull request as ready for review June 30, 2026 17:16
Copilot AI review requested due to automatic review settings June 30, 2026 17:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an incorrect cache-lookup loop in DataContract.GetIdForInitialization(ClassDataContract) that could fail to locate an existing DataContract entry and incorrectly throw SerializationException (DataContract cache overflow) during DataContractSerializer deserialization (notably involving Nullable<T> scenarios).

Changes:

  • Corrected the cache scan to index s_dataContractCache by the loop variable (i) rather than repeatedly re-checking the originally computed id.
  • Added a regression test covering nullable root deserialization for a [DataContract] struct.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/DataContract.cs Fixes the initialization-id lookup to scan the cache correctly by index.
src/libraries/System.Runtime.Serialization.Xml/tests/DataContractSerializer.cs Adds regression coverage for nullable root [DataContract] struct handling.

Co-authored-by: StephenMolloy <19562826+StephenMolloy@users.noreply.github.com>

@StephenMolloy StephenMolloy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@StephenMolloy
StephenMolloy requested a review from mangod9 July 8, 2026 00:33
@StephenMolloy
StephenMolloy merged commit a83cbd5 into main Jul 9, 2026
81 checks passed
@StephenMolloy
StephenMolloy deleted the copilot/fix-datacontractserializer-issue branch July 9, 2026 01:34
@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-preview7 Jul 10, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
#128506)

## 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.

  ```csharp
  for (int i = 0; i < currentDataContractId; i++)
  {
if (ContractMatches(classContract, s_dataContractCache.GetItem(i)))
      {
          return i;
      }
  }
  ```

- ### Regression coverage (`System.Runtime.Serialization.Xml` tests)
Added
`DCS_NullableDataContractStructAsRoot_DeserializesWithoutPriorInitialization`,
a cold-cache deserialization test that deserializes a nullable root
`[DataContract]` struct from a known XML payload using `ReadObject`
only, with no prior `WriteObject` call that could pre-initialize the
cache and mask the regression.
  
  Fixes #126873

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: StephenMolloy <19562826+StephenMolloy@users.noreply.github.com>
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

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.