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

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

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

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

評価

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

調査の方向性

パスは JsonNode.GetPath() と JsonException.Path によって構築されます。また、ドット記法と角括弧記法の選択は JsonObject.GetPath、ReadStack、WriteStack でも行われており、これらは ContainsSpecialCharacters() に依存しています。まずそこから始め、空のプロパティ名は角括弧記法が必要なものとして扱ってください。$[''].x と $.a[''] が生成され、ParentPathRootTests の GetPath_EscapedPropertyNames に新しいケースが追加されれば完了です。

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

説明

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.

主要言語
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 を短くまとめたダイジェスト。