[Java][JSON] No way to reject a repeated JSON property on read
维护者通常 1 天内回复
评估
调研方向
Read the existing failOnMissingRequiredProperties builder option and compare how JSON readers are generated and interpreted; the issue names both paths but gives no file locations or tests. Then assess how class annotations, mixins, flattened properties, null handling, and nested classes fit the proposed duplicate detection semantics. Done means a per-class option rejects repeated known properties while preserving the stated behavior for unknown, ignored, and any-setter entries.
由索引模型根据 Issue 内容生成。
描述
What happens
When a JSON object repeats one of the bean's known properties, fory-json keeps the last value without an error.
There is no builder option and no annotation that rejects such a document.
public static class Request {
public String id;
public Long tmax;
public List<String> bcat = List.of();
}
ForyJson fory = ForyJson.builder().build();
Request r = fory.fromJson(
"{\"id\":\"r1\",\"tmax\":100,\"bcat\":[\"IAB1\"],\"tmax\":200,\"bcat\":[]}", Request.class);
// tmax=200 bcat=[]
Reproduced on main (b6023929) and on 1.7.6, JDK 25.
Why it matters
RFC 8259 leaves duplicate names to the implementation, and parsers disagree on them: some take the first value,
some the last, some fail. A service that receives a document from a third party and passes it on (or checks it
in one component and acts on it in another) can therefore read a different value than the next hop does. For
input validation it is useful to refuse such a document outright.
Our case: we receive OpenRTB bid requests from external exchanges and answer a request that repeats a top-level
field ("tmax", "imp", ...) with HTTP 400. Nested objects of the same request stay lenient, and so do our
other models. Today we do this on Jackson with a custom module that wraps the root properties of one class. With
Fory there is no hook for it, so this is one of the things that keep us from moving the inbound request read to
Fory.
Jackson only offers StreamReadFeature.STRICT_DUPLICATE_DETECTION, which is parser-wide: it applies to every
object and map in every document and keeps a name set per object, so it costs every read. A per-class switch is
both cheaper and closer to what applications need.
Proposal
ForyJsonBuilder.failOnDuplicateProperties(boolean), defaultfalse(today's behaviour: the last value
wins), next tofailOnMissingRequiredProperties.- A class annotation
@JsonDuplicateProperties(fail = true | false)that overrides the builder setting for one
class. Like@JsonInclude, it is not inherited, and an exact-target@JsonMixincan supply it.
Semantics when enabled for a class:
- A known property of that class (field, setter,
@JsonCreatorparameter, a name flattened in by
@JsonUnwrapped) that appears twice in one object fails the read with aForyJsonExceptionthat names the
property. - An explicit
nullcounts as an occurrence, also whenonNullReadisSKIP. - Unknown and ignored names, and
@JsonAnySetterentries, are not tracked. - Nested objects follow their own class setting; enabling it on the root does not make children strict.
Cost: when disabled, the generated readers stay exactly as they are today (the decision is made at code
generation time, like onNullRead). When enabled, each object of that class marks one bit per known property
it reads, so there is no per-read allocation and no name set.
I have an implementation in progress for the generated readers and the interpreter and would like to open a PR
for it if this direction is acceptable. Happy to adjust the API shape (for example an enum instead of the
boolean, or only the class annotation) to whatever fits the project better.
- 主要语言
- Java
- 星标
- 4.6k
- 派生
- 470
- 平均合并
- 9 小时 28 分钟
- 30 天内合并 PR
- 33
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
apache/fory 的其他 Issue
-
[Python] Compatible xlang read silently drops fields when the peer writes camelCase/PascalCase names未关闭
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
[Build] Add ASF license headers to config files that allow comments可能重新可做 @Swatantra-66 于 27 天前认领,目前没有进行中的 PR。 未关闭enhancement
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
xlang: the snake_case conversion of field names is not specified, and the runtimes do not agree可能重新可做 @dhruv-dagar 于 14 天前认领,目前没有进行中的 PR。 未关闭bug
难度 5/5 一周以上 新手友好度 25/100
维护者通常 1 天内回复
-
[Rust] Add an option to use Rust native enum可能已有人在做 @BaldDemian 于 19 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 35/100
apache/fory#4177 · 1 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Untyped read collapses a number literal that does not fit a double可能已有人在做 @mohitjoshi-hey 于 20 天前认领。 未关闭bug
难度 3/5 1-2 天 新手友好度 68/100
apache/fory#4170 · 3 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1 小时以内 新手友好度 70/100
Vakore/ZappierGames#248 ·
-
难度 2/5 1-3 小时 新手友好度 66/100
thelastpickle/cassandra-reaper#1748 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 3 天内回复
-
A proxy that fails to start logs a RejectedExecutionException stack trace from its shutdown hook未关闭follow-up
难度 2/5 1-3 小时 新手友好度 62/100
Sanne/incus-spawn#1953 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 72/100
OneBusAway/maglev#1917 · 1 条评论 ·
维护者通常 3 天内回复