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

未关闭 适合新手
#138,089 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
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 小时
30 天内合并 PR
588

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dotnet/runtime 的其他 Issue

查看 dotnet/runtime 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。