[Java][JSON] No way to reject a repeated JSON property on read

未关闭
#4,165 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@pavel-ptashyts 已经在做这个了。

开始于 2026年10月11日。

  • #4166 来自 @pavel-ptashyts —— 未关闭

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
java, json
领域
backend

调研方向

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), default false (today's behaviour: the last value
    wins), next to failOnMissingRequiredProperties.
  • 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 @JsonMixin can supply it.

Semantics when enabled for a class:

  • A known property of that class (field, setter, @JsonCreator parameter, a name flattened in by
    @JsonUnwrapped) that appears twice in one object fails the read with a ForyJsonException that names the
    property.
  • An explicit null counts as an occurrence, also when onNullRead is SKIP.
  • Unknown and ignored names, and @JsonAnySetter entries, 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

环境准备

从这里开始

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

apache/fory 的其他 Issue

查看 apache/fory 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

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