Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ protected Object readType(Type type, HttpInputMessage inputMessage) {
final long contentLength = inputMessage.getHeaders().getContentLength(); // -1 表示未知
try {
final byte[] body = fastRead(inputMessage.getBody(), contentLength);
return JSON.parseObject(body, type, config.readerContext());
return JSON.parseObject(body, type, config.getReaderContext());
} catch (JSONException ex) {
throw new HttpMessageNotReadableException("JSON parse error: " + ex.getMessage(), ex, inputMessage);
} catch (IOException ex) {
Expand Down Expand Up @@ -211,7 +211,7 @@ protected void writeInternal(Object object, HttpOutputMessage outputMessage) thr
}

contentLength = JSON.writeTo(
baos, object, config.writerContext()
baos, object, config.getWriterContext()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void clearContext() {
writerContext = null;
}

public JSONReader.Context readerContext() {
public JSONReader.Context getReaderContext() {
Comment thread
jujn marked this conversation as resolved.
Resolved
JSONReader.Context context = readerContext;
if (context == null) { // Concurrency may occur, but it will not cause any problems
context = new JSONReader.Context(JSONFactory.getDefaultObjectReaderProvider(), jsonb ? symbolTable : null, readerFilters, readerFeatures);
Expand All @@ -278,7 +278,7 @@ public JSONReader.Context readerContext() {
return context;
}

public JSONWriter.Context writerContext() {
public JSONWriter.Context getWriterContext() {
Comment thread
jujn marked this conversation as resolved.
Resolved
JSONWriter.Context context = writerContext;
if (context == null) {
context = new JSONWriter.Context(dateFormat, writerFeatures);
Expand All @@ -293,4 +293,32 @@ public JSONWriter.Context writerContext() {
}
return context;
}

/**
* Gets the JSONReader context.
* @deprecated Use {@link #getReaderContext()} instead to comply with JavaBean naming conventions.
* @return the JSONReader context
*/
@Deprecated
public JSONReader.Context readerContext() {
return getReaderContext();
}

/**
* Gets the JSONWriter context.
* @deprecated Use {@link #getWriterContext()} instead to comply with JavaBean naming conventions.
* @return the JSONWriter context
*/
@Deprecated
public JSONWriter.Context writerContext() {
return getWriterContext();
}

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

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


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

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

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

}
Loading