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

Aperta Adatta ai principianti
#138,090 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp
Ambito
backend

Direzione di ricerca

Inizia da JsonObject.IDictionary.cs, nel metodo esplicito ICollection<KeyValuePair<string, JsonNode?>>.Remove, che attualmente chiama Remove(item.Key) senza verificare il valore. Confrontalo con il metodo Contains appena sopra, che verifica la chiave e il riferimento al nodo. Aggiungi un test in JsonObjectTests per una chiave corrispondente con un valore diverso, ed esegui quella classe di test. È completato quando questo caso restituisce false e lascia la proprietà al suo posto, mentre una coppia corrispondente viene ancora rimossa e staccata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
C#
Stelle
18.5k
Fork
5.8k
Merge medio
2g 18h
PR unite (30g)
588

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dotnet/runtime

Tutte le issue di dotnet/runtime

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.