Skip to content

fix: add setter for FastjsonConfig.writer/readerContext, for issue #4000 - #4008

Open
jujn wants to merge 3 commits into
alibaba:mainfrom
jujn:fix_4000
Open

jujn wants to merge 3 commits into
alibaba:mainfrom
jujn:fix_4000

Conversation

@jujn

@jujn jujn commented Mar 5, 2026

Copy link
Copy Markdown
Collaborator

What this PR does / why we need it?

允许用户像 1.x 中那样限定自定义序列化器影响范围,如下所示:

        FastJsonConfig config = new FastJsonConfig();
        
        ObjectWriterProvider provider = new ObjectWriterProvider();
        provider.register(Long.class, ObjectWriters.ofToString(Object::toString));
        provider.register(Integer.class, ObjectWriters.ofToString(Object::toString));
        
        config.setWriterContext(JSONFactory.createWriteContext(provider));

Summary of your change

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed.

@jujn

jujn commented Apr 25, 2026

Copy link
Copy Markdown
Collaborator Author

done

public void setReaderContext(JSONReader.Context context) {
this.readerContext = context;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] 新增的 setReaderContext() / setWriterContext() 缺少测试覆盖和 Javadoc。

  1. 测试缺失: PR 新增了 4 个公共方法,但 diff 中没有测试文件变更。建议在 FastJsonConfigTest 中补充:(a) setter/getter 往返一致性,(b) PR 描述中的自定义 ObjectWriterProvider 用例,(c) 废弃方法委托正确性,(d) clearContext() 后自定义 context 被清除的行为。

  2. 缓存失效行为未文档化: 这两个 setter 注入的自定义 context 会被后续 setReaderFeatures()、setDateFormat() 等方法通过 clearContext() 静默清除。JavaBean 的 set/get 命名隐含"值会持续生效"的契约,建议添加 Javadoc 警告此行为:

/**
 * Sets a pre-built JSONReader.Context.
 * <p><b>Warning:</b> subsequent calls to {@link #setReaderFeatures},
 * {@link #setDateFormat}, etc. will invalidate this custom context.
 */
public void setReaderContext(JSONReader.Context context) {
    this.readerContext = context;
}

— qwen3.7-max via Qwen Code /review

@wenshao wenshao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — no blockers. Suggestions are inline.

中文说明

已审查——无阻断问题。 建议见行内评论。

— qwen3.8-max-preview via Qwen Code /review

Comment on lines +317 to +319
public void setReaderContext(JSONReader.Context context) {
this.readerContext = context;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The new setters store the injected context in the same readerContext/writerContext fields that every existing config setter (setDateFormat, set*Features, set*Filters, setCharset, setJSONB, setSymbolTable, clearContext) unconditionally nulls out as cache invalidation, so any such call silently discards the injected context and the next getter call lazily rebuilds a default from the config's own fields. The reverse order is lossy too: features/filters configured before injection are silently ignored because the injected context is used as-is. — Failure scenario: config.setWriterContext(customCtx) followed by config.setDateFormat("yyyy-MM-dd") → clearContext() nulls writerContext → the next getWriterContext() rebuilds a default context and the scoped ObjectWriterProvider is gone, with no log or exception.

Suggested fix: document the interaction on both setters (or hold the injected context in a separate field so config setters invalidate only the derived cache and the getter prefers the injected value):

Suggested change
public void setReaderContext(JSONReader.Context context) {
this.readerContext = context;
}
/**
* Sets the JSONReader context.
* <p>Note: any subsequent call to {@code setDateFormat}, {@code setReaderFeatures},
* {@code setReaderFilters}, {@code setCharset}, {@code setJSONB}, {@code setSymbolTable}
* or {@code clearContext} discards the injected context and reverts to lazy
* construction from the config fields.
* @param context the JSONReader context to use
*/
public void setReaderContext(JSONReader.Context context) {
this.readerContext = context;
}
中文说明

新增的 setter 将注入的 context 保存在 readerContext/writerContext 字段中,而所有现有配置 setter(setDateFormat、set*Features、set*Filters、setCharset、setJSONB、setSymbolTable、clearContext)都会无条件将这些字段置空以失效缓存,因此任何此类调用都会静默丢弃已注入的 context,下一次 getter 调用会从配置自身的字段懒加载重建默认 context。反序调用同样有问题:先配置 features/filters 再注入 context,之前配置的 features/filters 会被静默忽略。失败场景:先调用 config.setWriterContext(customCtx) 再调用 config.setDateFormat("yyyy-MM-dd") → clearContext() 将 writerContext 置空 → 下次 getWriterContext() 重建默认 context,作用域限定的 ObjectWriterProvider 丢失,且无日志、无异常。

建议修复:在两个 setter 上补充 Javadoc 说明该交互(或将注入的 context 保存在独立字段中,使配置 setter 仅失效派生缓存,且 getter 优先使用注入的值)。

— qwen3.8-max-preview via Qwen Code /review

Comment on lines +321 to +323
public void setWriterContext(JSONWriter.Context context) {
this.writerContext = context;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The cached context is consumed in exactly two places, both in extension-spring5's FastJsonHttpMessageConverter (the getReaderContext()/getWriterContext() call sites). Every other integration builds calls from the raw config arrays — e.g. extension-spring6's converters use config.getDateFormat() / config.getWriterFilters() / config.getWriterFeatures(), and the Redis serializers, solon and jaxrs modules likewise never read the context accessors — so setWriterContext/setReaderContext silently no-op for every consumer except the spring5 HTTP converter, with nothing documenting that scope. — Failure scenario: a Spring 6 user injects a scoped ObjectWriterProvider via setWriterContext per issue #4000; serialization goes through the feature-array path, the provider is never consulted, and output is identical to never having called the setter — no exception, no warning.

Suggested fix: at minimum, document the actual scope on the setters; better, route the other consumers (starting with the spring6 converter) through the context accessors so the setter means the same thing everywhere:

Suggested change
public void setWriterContext(JSONWriter.Context context) {
this.writerContext = context;
}
/**
* Sets the JSONWriter context.
* <p>Note: only honored by consumers that call {@link #getWriterContext()};
* currently {@code FastJsonHttpMessageConverter} in extension-spring5. Other
* integrations (spring6, Redis serializers, solon, jaxrs) read the feature/filter
* arrays directly and ignore an injected context.
* @param context the JSONWriter context to use
*/
public void setWriterContext(JSONWriter.Context context) {
this.writerContext = context;
}
中文说明

缓存的 context 仅在 extension-spring5 的 FastJsonHttpMessageConverter 的两处调用点(getReaderContext()/getWriterContext())中被消费。其他所有集成都直接使用配置数组构建调用——例如 extension-spring6 的转换器使用 config.getDateFormat() / config.getWriterFilters() / config.getWriterFeatures(),Redis 序列化器、solon 和 jaxrs 模块同样从不读取 context 访问器——因此 setWriterContext/setReaderContext 对除 spring5 HTTP 转换器外的所有消费者都静默无效,且没有任何文档说明该限制范围。失败场景:Spring 6 用户按 issue #4000 的用法通过 setWriterContext 注入作用域限定的 ObjectWriterProvider,序列化走 feature 数组路径,自定义 provider 完全不被使用,输出与未调用 setter 时完全相同——无异常、无警告。

建议修复:至少在 setter 上以 Javadoc 说明实际生效范围;更好的做法是让其他消费者(从 spring6 转换器开始)改为通过 context 访问器调用,使 setter 在所有模块中语义一致。

— qwen3.8-max-preview via Qwen Code /review

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants