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

Đang mở Phù hợp với người mới
#138,089 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp, json
Lĩnh vực
api

Hướng nghiên cứu

Đường dẫn được xây dựng bởi JsonNode.GetPath() và JsonException.Path, và cùng một lựa chọn giữa ký hiệu dấu chấm và ký hiệu ngoặc vuông cũng được thực hiện trong JsonObject.GetPath, ReadStack và WriteStack, các phương thức này dựa vào ContainsSpecialCharacters(). Hãy bắt đầu từ đó và coi tên thuộc tính rỗng là trường hợp cần dùng ký hiệu ngoặc vuông. Hoàn thành khi tạo ra $[''].x và $.a[''], đồng thời thêm một trường hợp mới vào GetPath_EscapedPropertyNames trong ParentPathRootTests.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C#
Star
18.5k
Fork
5.8k
Merge trung bình
2 ngày 18 giờ
Pull request đã merge (30 ngày)
588

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dotnet/runtime

Tất cả issue của dotnet/runtime

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.