JsonObject's ICollection<KeyValuePair<string, JsonNode?>>.Remove ignores the value

オープン 初心者向け
#138,090 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp
領域
backend

調査の方向性

JsonObject.IDictionary.cs の、明示的な ICollection<KeyValuePair<string, JsonNode?>>.Remove メソッドから始めてください。現在これは値を確認せずに Remove(item.Key) を呼び出しています。そのすぐ上にある Contains メソッドと比較してください。Contains はキーとノード参照を確認します。キーは一致するが値が異なる場合のテストを JsonObjectTests に追加し、そのテストクラスを実行してください。このケースが false を返し、プロパティが残ったままになり、一致するペアは引き続き削除されてデタッチされる時点で完了です。

索引モデルが issue の本文から書いたものです。

説明

area-System.Collections untriaged
Description

JsonObject's explicit ICollection<KeyValuePair<string, JsonNode?>>.Remove(item) removes the property with the matching key even when the value doesn't match. Contains(item) on the same interface does check the value, and Dictionary<,> and OrderedDictionary<,> return false from Remove in this case.

Reproduction Steps
using System.Text.Json.Nodes;

var obj = new JsonObject { ["a"] = 1, ["b"] = 2 };
ICollection<KeyValuePair<string, JsonNode?>> collection = obj;
var other = new KeyValuePair<string, JsonNode?>("b", JsonValue.Create(99));

Console.WriteLine(collection.Contains(other));
Console.WriteLine(collection.Remove(other));
Console.WriteLine(obj.ToJsonString());
Expected behavior
False
False
{"a":1,"b":2}
Actual behavior
False
True
{"a":1}

Seen on .NET 10.0.15 and 11.0.0. For comparison, ((ICollection<KeyValuePair<string, int>>)new Dictionary<string, int> { ["a"] = 1, ["b"] = 2 }).Remove(new("b", 99)) returns false and keeps both entries, and so does OrderedDictionary<string, int>.

Other information

In JsonObject.IDictionary.cs the method is => Remove(item.Key);, while its XML docs say it removes "the property name and value" represented by the KeyValuePair. Contains right above it documents that it checks the property name and JsonNode reference, and it does: a different node with an equal value also returns false. The existing test in JsonObjectTests removes a pair that matches and a key that doesn't exist, but not a matching key with a different value.

A possible fix is to return false unless Contains(item) is true (so the same reference semantics as Contains), then remove by key as today so the node is still detached from its parent. I can send a PR with a test if you'd like.

主要言語
C#
スター
18.5k
フォーク
5.8k
平均マージ
2日 18時間
マージ済み PR(30日)
588

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dotnet/runtime のほかの issue

dotnet/runtime の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。