Skip to content

[BUG] Typed Map with bean values throws on inner @type when auto type is disabled #7970

Not planned

Description

@liango2

What happens

A typed map with bean values throws when the JSON contains an inner @type and auto type is off.

Repro on current main (fastjson2 2.0.64, JDK 21):

class Val {
    public String name;
}

class ParamVal {
    public Map<String, Val> varsMap;
}

String json = "{\"varsMap\":{\"@type\":\"java.util.LinkedHashMap\",\"1\":{\"name\":\"x\"}}}";

// auto type not enabled: no SupportAutoType feature, no autoTypeFilter
JSON.parseObject(json, ParamVal.class);

Result:

com.alibaba.fastjson2.JSONException: expect ':', but 44, offset 21, character ", line 1, column 21
	at com.alibaba.fastjson2.JSONReaderASCII.readFieldNameHashCodeE(JSONReaderASCII.java:308)
	at com.alibaba.fastjson2.reader.ORG_3_1_Val.readObject(Unknown Source)
	at com.alibaba.fastjson2.reader.ObjectReaderImplMapTyped.readObject(ObjectReaderImplMapTyped.java:429)

Why it surprises me

The same JSON parses fine in three neighbouring cases:

  • untyped map: JSON.parseObject(json) -> {"varsMap":{"@type":"java.util.LinkedHashMap","1":{"name":"x"}}}, the @type entry is kept as a normal key.
  • Map<String, String>: keeps @type as a key, no error.
  • Map<String, Object>: keeps @type as a key, no error.

Only when the value type is a bean does it blow up. My guess: auto type is off, so the reader skips the @type key, but the string value "java.util.LinkedHashMap" is still handed to the bean reader, which expects { and a field name, and dies on the comma.

What I would expect

With auto type off, an inner @type entry should be skipped like any other unknown metadata, not turn into an exception. Either drop it, or keep it as a plain key the way the untyped map does.

Related

#7964 fixes the opposite side: auto type ON plus keyType == String used to ignore the inner @type. This case is auto type OFF, which that PR does not touch.

Activity

  1. liango2 commented on Nov 25, 2026

    @liango2
    Author

    Closing this. My analysis was wrong.

    I ran one more case: Map<String, Val> where Val can be built from a string through @JSONCreator. It still fails. But so does the same map with a plain string value and no @type in the JSON at all:

    JSON.parseObject("{\"varsMap\":{\"1\":\"hello\"}}", ParamValFromString.class);
    // JSONException: expect ':', but 125, offset 17

    So the error has nothing to do with @type. A map whose value type is a bean cannot read a bare string, and the @type string is just one of those strings.

    The policy is consistent: with auto type off, @type is treated as a normal entry. String and Object value types can hold it; a bean value type cannot, so it throws. That is a type mismatch, not an @type bug.

    Sorry for the noise.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions