Skip to content

.NET: Fix JsonWireSerializedValue hash code contract - #7961

Draft
Daichi Isami (normalian) wants to merge 2 commits into
microsoft:mainfrom
normalian:fix/7958-json-wire-hash-code
Draft

.NET: Fix JsonWireSerializedValue hash code contract#7961
Daichi Isami (normalian) wants to merge 2 commits into
microsoft:mainfrom
normalian:fix/7958-json-wire-hash-code

Conversation

@normalian

Copy link
Copy Markdown
Contributor

Motivation & Context

JsonWireSerializedValue.Equals() uses structural JSON equality, while its previous hash code came from JsonElement.GetHashCode(), which is not structural. Separately parsed equivalent JSON values could therefore compare equal but produce different hash codes.

Description & Review Guide

  • What are the major changes? Use JsonValueKind as the stable hash input and add a regression test for equivalent values parsed from separate JSON documents.
  • What is the impact of these changes? Equal JsonWireSerializedValue instances now produce equal hash codes. Hash collisions between values of the same JSON kind are intentional to remain consistent with JsonElement.DeepEquals() without duplicating its semantics.
  • What do you want reviewers to focus on? Whether the minimal ValueKind-based approach is appropriate for this internal delayed-deserialization wrapper.

Related Issue

Fixes #7958

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and the title prefix in sync automatically.

Use JsonValueKind for a stable hash when structurally equal JSON values are compared, and add a regression test for separately parsed equivalent values.
Copilot AI balanced review requested due to automatic review settings August 31, 2026 09:52

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 hash consistency for structurally equal, separately parsed JSON wrappers.

Changes:

  • Hashes JsonWireSerializedValue by JsonValueKind.
  • Adds a regression test for equivalent JSON documents.

Reviewed changes

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

File Description
JsonWireSerializedValue.cs Changes hash-code generation.
JsonSerializationTests.cs Tests equal wrapper hash codes.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

// JsonElement does not provide a structural hash code. ValueKind is necessarily equal
// whenever DeepEquals returns true, preserving the equality contract without recursively
// reimplementing JsonElement.DeepEquals semantics.
return this.Data.ValueKind.GetHashCode();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET Usage: [Issues, PRs], Target: .Net workflows Usage: [Issues, PRs], Target: Workflows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: [Bug]: JsonWireSerializedValue violates the equality and hash code contract

2 participants