Skip to content

fix: honor AutoTypeBeforeHandler for JSON.parse(byte[]) array elements (issue #7952) - #7962

Open
divine-punishment wants to merge 1 commit into
alibaba:mainfrom
divine-punishment:fix/autotype-beforehandler-bytes
Open

divine-punishment wants to merge 1 commit into
alibaba:mainfrom
divine-punishment:fix/autotype-beforehandler-bytes

Conversation

@divine-punishment

Copy link
Copy Markdown

What this PR does / why we need it?

Fixes #7952.

With Feature.ErrorOnNotSupportAutoType enabled and an AutoTypeBeforeHandler that authorizes the requested type, JSON.parse(String) and JSON.parse(byte[]) behaved inconsistently for a top-level array containing @type objects: the String path succeeded while the byte[] path threw autoType not support, even though the same handler would allow the type.

Root cause: JSONReader.readArray delegated nested objects to the handler-aware ObjectReaderImplObject only when a context-level handler was set, ignoring a provider-level handler (ObjectReaderProvider#setAutoTypeBeforeHandler). The byte[] entry path therefore fell into the plain readObject, which rejects @type outright under ErrorOnNotSupportAutoType.

Summary of your change

  • JSONReader.readArray: also check the provider-level handler in the nested-object guard, so authorized types resolve exactly as on the String path
  • Reorder the guard so isReference() is checked first: reference objects as array elements must keep taking the reference path regardless of any handler, otherwise $ref values would be parsed as literal maps (covered by the existing Issue2140 regression test)

Unauthorized types are still rejected under ErrorOnNotSupportAutoType on both paths.

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. (Bug fix only, no API change)
中文说明

修复内容

Fixes #7952:开启 ErrorOnNotSupportAutoType 且 AutoTypeBeforeHandler 已授权目标类型时,顶层带 @type 的数组:JSON.parse(String) 成功,JSON.parse(byte[]) 抛 autoType not support。

根因:JSONReader.readArray 只有在 context 级 handler 存在时才把嵌套对象委托给支持 handler 的 ObjectReaderImplObject,provider 级 handler(ObjectReaderProvider#setAutoTypeBeforeHandler)被忽略,byte[] 路径落入无条件拒绝 @type 的通用 readObject。

修改方式

  • readArray 的守卫补充 provider 级 handler 检查,授权类型与 String 路径一致地解析
  • 将 isReference() 检查前置:$ref 引用元素必须始终走引用路径,否则会被解析成字面量 map(现有 Issue2140 回归测试覆盖此场景)

未授权类型在两条路径上仍然被 ErrorOnNotSupportAutoType 拒绝。

With Feature.ErrorOnNotSupportAutoType enabled and an AutoTypeBeforeHandler
that authorizes the requested type, JSON.parse(String) and JSON.parse(byte[])
behaved inconsistently for a top-level array containing @type objects: the
String path succeeded while the byte[] path threw 'autoType not support',
even though the same handler would allow the type (issue alibaba#7952).

JSONReader.readArray delegated nested objects to the handler-aware
ObjectReaderImplObject only when a context-level handler was set, ignoring a
provider-level handler (ObjectReaderProvider#setAutoTypeBeforeHandler). The
byte[] entry path therefore fell into the plain readObject, which rejects
@type outright under ErrorOnNotSupportAutoType.

- Check the provider-level handler in readArray's nested-object guard, so
  authorized types resolve exactly as on the String path
- Reorder the guard so isReference() is checked first: reference objects as
  array elements must keep taking the reference path regardless of any
  handler, otherwise {$ref} values are parsed as literal maps (guarded by
  the existing Issue2140 regression test)

Unauthorized types are still rejected under ErrorOnNotSupportAutoType on
both paths.

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.

[BUG] JSON.parse(byte[]) does not consult AutoTypeBeforeHandler for array @type while JSON.parse(String) does

1 participant