JsonNode.GetPath() and JsonException.Path produce `$..x` / `$.a.` for empty property names

Aperta Adatta ai principianti
#138,089 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
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp, json
Ambito
api

Direzione di ricerca

Il percorso viene costruito da JsonNode.GetPath() e JsonException.Path, e la stessa scelta tra notazione a punto e notazione a parentesi quadre viene fatta in JsonObject.GetPath, ReadStack e WriteStack, che si basano su ContainsSpecialCharacters(). Inizia da lì e considera un nome di proprietà vuoto come un caso che richiede la notazione a parentesi quadre. Il lavoro è completato quando vengono prodotti $[''].x e $.a[''] e viene aggiunto un nuovo caso a GetPath_EscapedPropertyNames in ParentPathRootTests.

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

Descrizione

area-System.Text.Json untriaged
Description

An empty JSON property name is written with dot notation in paths, which gives $..x (JSONPath's descendant operator) and $.a.. Names with special characters already get bracket notation (e.g. $['a b'].x), and #122841 recently fixed their escaping, but an empty name still uses dot notation.

Reproduction Steps
using System.Text.Json;
using System.Text.Json.Nodes;

var root = JsonNode.Parse("""{"":{"x":1},"a":{"":2}}""")!;
Console.WriteLine(root[""]!["x"]!.GetPath());
Console.WriteLine(root["a"]![""]!.GetPath());

try
{
    JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, int>>>("""{"":{"x":"nan"}}""");
}
catch (JsonException e)
{
    Console.WriteLine(e.Path);
}
Expected behavior
$[''].x
$.a['']
$[''].x
Actual behavior
$..x
$.a.
$..x

Seen on .NET 10.0.15 and 11.0.0.

Other information

JsonObject.GetPath, ReadStack and WriteStack use bracket notation only when ContainsSpecialCharacters() is true, and that's ContainsAny(...), which is false for an empty span. RFC 9535's dot shorthand needs at least one character, so an empty member name can only be written as ['']. GetPath_EscapedPropertyNames in ParentPathRootTests doesn't include an empty name.

A possible fix is to treat an empty name as needing bracket notation in those three places, with an extra test case. I'm happy to send a PR.

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.