jmap icon indicating copy to clipboard operation
jmap copied to clipboard

Roundtrip deserialization is broken for JMAP objects with Enum Maps

Open jaudriga opened this issue 4 years ago • 2 comments

GSON seems to have an issue with roundtrip deserialization and enum maps (see https://github.com/google/gson/issues/1920 ). This is also an issue for this library, because some objects use Enum Maps. The result is that GSON serializes the keys of this map as uppercase letters in the JSON, but for deserialization the library expects them to be lowercase.

This currently happens with the https://github.com/jaudriga/jmap/tree/64-support-calendars branch.

For example, take the CalendarEvent.replyTo enum map attribute:

public class CalendarEvent extends AbstractIdentifiableEntity {
...
	private Map<CalendarReplyToMethod, String> replyTo;
...
}

For the following serialized CalendarEvent JSON:

{
  "@type" : "jsevent",
  "calendarId" : "123",
  "id" : "c2ec0813-1227-42fa-b6bd-7b68ad4ca281",
...
  "replyTo": {
    "imip": "mailto:[email protected]"
  },
...
}

The following test will try to "roundtrip deserialize" the CalendarEvent object and fail:

public class RoundtripDeserializationTest extends AbstractGsonTest {

    @Test
    public void roundtripDeserializeCalendarEvent() throws Exception {
        final CalendarEvent event = parseFromResource("calendar-event/calendar-event.json", CalendarEvent.class);
        final Map<CalendarReplyToMethod, String> replyTo = event.getReplyTo();
        Gson gson = getGson();

        System.out.println(gson.toJson(replyTo));   // {"IMIP":"mailto:[email protected]"}
        assertNotNull(replyTo.get(CalendarReplyToMethod.IMIP));

        String strSerialized = gson.toJson(replyTo);

        System.out.println(strSerialized);                // {"IMIP":"mailto:[email protected]"}

        Type type = new TypeToken<Map<CalendarReplyToMethod, String>>() {}.getType();
        Map <CalendarReplyToMethod, String> replyToDeserialized = gson.fromJson(strSerialized, type);

        System.out.println(replyToDeserialized);            // {null=mailto:[email protected]}
        assertNotNull(replyToDeserialized.get(CalendarReplyToMethod.IMIP));
    }
}

jaudriga avatar Oct 28 '21 13:10 jaudriga