fix: reject an unresolved node reference instead of keeping it as a string - #79
Merged
Merged
Conversation
…tring A bare <N> value that is not resolved used to be kept as a plain string, which a caller could not tell apart from a real string. The document is not valid json either way, so report it. Only a well formed <N> with an integer N is rejected, a quoted value and a token that is not a reference are unaffected. Marshal keeps writing the reference syntax for a cyclic object, its doc comment now says so.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
#77 让默认入口不再解析节点引用,但未解析的裸
<N>会回落成普通字符串。调用方拿到"<1>"时,无法与真的就是字符串"<1>"的数据区分 —— 有类型目标会报类型错误,但map[string]interface{}/JSONObject这类无类型目标会静默接受。典型触发路径:
Marshal对循环结构写出引用语法,再用默认入口Parse读回。变更
默认入口遇到格式正确的节点引用(裸
<N>,N 为整数)时返回ErrNodeReferenceDisabled,而不是降级为字符串。这类 token 本就不是合法 JSON,标准解析器同样拒绝。<整数>形式;带引号的"<1>"、以及<abc>这类非引用 token 行为不变ParseTrusted不受影响同时给
Marshal补上文档注释,说明循环结构会写出本库扩展语法、产出的不是合法 JSON、需用ParseTrusted读回。兼容性影响
含裸
<N>的文档(非标准 JSON)此前能解析成功,现在返回错误。忽略Parse错误并直接解引用返回值的调用方会遇到 nil 解引用崩溃 —— 但这类输入本就是非法 JSON,且同样的模式对畸形输入(如{"a":})早已存在(Parse返回nil, err)。测试
node_reference_test.go:TestNodeReferenceRejected:4 种含引用的文档必须报错;带引号的"<1>"与<abc>仍正常解析TestNodeIdKeyIsOrdinary:不含引用时___jnid_仍是普通键、nodeId保持 0TestMarshalUnmarshalKeepsCycle/TestParseTrustedResolvesReference/TestParseTrustedRoundTrip不变,确认受信任路径能力完整go test ./...全量通过。