Skip to content

JSON conversion fails for maps with nested object values and repeated fields as first field #738

Description

@tristantarrant

Problem

ProtobufUtil.toCanonicalJSON throws IllegalStateException: Maps only have nested objects for values when a map's value is a message that contains nested structures (sub-maps, repeated fields). This was introduced in PR #411 which rewrote the JSON writer hierarchy.

Additionally, ArrayJsonWriter drops the property name when a repeated field is the first content in a non-complex object (no _type written), producing malformed JSON.

Root Cause

MapJsonWriter:

  • onTag treated field number 1 as a map key even when the delegate (value message) was active
  • onStartNested threw instead of forwarding to the delegate when the value message was open
  • onEndNested cleared the delegate too early

ArrayJsonWriter:

  • onStartNested only handled the followedByComma(lastToken()) case but not isOpen(lastToken()), so the property name was missing when a repeated field is the first content in an object

Fix

  • MapJsonWriter: added valueField tracking so nested structures inside a map's message value are correctly delegated
  • ArrayJsonWriter: added the isOpen(lastToken()) branch to write the property name when the repeated field is first in an object

Test plan

  • testMapWithNestedObjectValues — mirrors the Keycloak authenticationSessions scenario
  • testMapWithMultipleNestedObjectValues — multiple map entries with nested message values
  • testRepeatedFieldFirstInObject — repeated field as first content in a non-complex nested object

Created with the assistance of an AI tool

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

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions