Skip to content

[Bug]: structToMap throws NPE on JSON null values in DataPart data/metadata #1159

Description

@lyingparachute

What happened?

A2ACommonFieldMapper.structToMap throws NullPointerException when a protobuf Struct contains a NULL_VALUE field. That is the mapping for a JSON null inside a DataPart's data or any metadata object.

valueToObject returns Java null for NULL_VALUE. Collectors.toMap then throws, because it does not accept null values. This is documented Collectors.toMap behaviour, not a missing null-check in the caller.

JSON-RPC message/send with a data part like {"attributeId": null, "source": "ID"} never produces a domain Message. The parse dies inside JSONRPCUtils.parseRequestBody.

Seen on org.a2aproject.sdk:a2a-java-sdk-spec-grpc:1.1.0.Final. The same Collectors.toMap call is still on main in spec-grpc/.../A2ACommonFieldMapper.java.

Related but different: #618 is an empty nested Struct (structToMap returns null for zero fields, then the parent toMap NPEs). This report is a JSON null value (NULL_VALUE). Fixing empty structs to emptyMap() does not fix this path.

Expected: JSON null in data / metadata becomes a Java null in the resulting Map, and the key is kept. containsKey("attributeId") is true and get("attributeId") is null.

Suggested fix: populate a HashMap with forEach instead of Collectors.toMap. Also stop returning null for an empty struct (return emptyMap()), which is the same null-hostile habit as #618.

default Map<String, Object> structToMap(Struct struct) {
    if (struct == null || struct.getFieldsCount() == 0) {
        return Collections.emptyMap();
    }
    Map<String, Object> map = new HashMap<>();
    struct.getFieldsMap().forEach((k, v) -> map.put(k, valueToObject(v)));
    return map;
}

Note that Message / Part constructors still use Map.copyOf on metadata, which forbids top-level null values. Even after this mapper fix, rebuilding a Message with null metadata values will NPE unless those constructors use a null-tolerant copy.

Relevant log output

java.lang.NullPointerException
  at java.util.stream.Collectors.lambda$uniqKeysMapAccumulator$0(Collectors.java:180)
  at org.a2aproject.sdk.grpc.mapper.A2ACommonFieldMapper.structToMap(A2ACommonFieldMapper.java:170)
  at org.a2aproject.sdk.grpc.mapper.PartMapper.fromProto(PartMapper.java:110)
  at org.a2aproject.sdk.grpc.mapper.MessageMapperImpl.fromProto(MessageMapperImpl.java:72)
  at org.a2aproject.sdk.grpc.utils.JSONRPCUtils.parseRequestBody(JSONRPCUtils.java:192)

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

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