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

Abierto Apto para principiantes
#138,090 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
82/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
csharp
Área
backend

Línea de trabajo

Empieza en JsonObject.IDictionary.cs, en el método explícito ICollection<KeyValuePair<string, JsonNode?>>.Remove, que actualmente llama a Remove(item.Key) sin comprobar el valor. Compáralo con el método Contains justo encima, que comprueba la clave y la referencia del nodo. Añade una prueba en JsonObjectTests para una clave coincidente con un valor distinto y ejecuta esa clase de pruebas. Está terminado cuando ese caso devuelve false y deja la propiedad en su sitio, mientras que un par coincidente sigue eliminándose y desvinculándose.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C#
Estrellas
18.5k
Forks
5.8k
Merge medio
2 d 18 h
PR fusionados (30 d)
588

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de dotnet/runtime

Todos los issues de dotnet/runtime

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.