diff --git a/libai/src/main/java/ai/api/GsonFactory.java b/libai/src/main/java/ai/api/GsonFactory.java index 31e0b72..800ada4 100644 --- a/libai/src/main/java/ai/api/GsonFactory.java +++ b/libai/src/main/java/ai/api/GsonFactory.java @@ -196,7 +196,16 @@ public ResponseChatBubble deserialize(JsonElement json, Type typeOfT, @Override public JsonElement serialize(ResponseChatBubble src, Type typeOfSrc, JsonSerializationContext context) { - return SIMPLIFIED_GSON.toJsonTree(src, ResponseMessage.class); + JsonObject result = (JsonObject)SIMPLIFIED_GSON.toJsonTree(src, ResponseMessage.class); + JsonArray items = result.getAsJsonArray("items"); + if ((items != null) && (items.size() == 1)) { + JsonObject item = (JsonObject)items.get(0); + result.add("textToSpeech", item.get("textToSpeech")); + result.add("ssml", item.get("ssml")); + result.add("displayText", item.get("displayText")); + result.remove("items"); + } + return result; } } } diff --git a/libai/src/test/java/ai/api/model/GoogleAssistantResponseMessagesTest.java b/libai/src/test/java/ai/api/model/GoogleAssistantResponseMessagesTest.java index 2a738ae..308c1b5 100644 --- a/libai/src/test/java/ai/api/model/GoogleAssistantResponseMessagesTest.java +++ b/libai/src/test/java/ai/api/model/GoogleAssistantResponseMessagesTest.java @@ -35,8 +35,8 @@ public class GoogleAssistantResponseMessagesTest { private final String TEST_CHAT_BUBBLE_SINGLE_ITEM = "{" + "\"customizeAudio\":true," - + "\"textToSpeech\":\"hello\",\"ssml\":\"ssmlText\",\"displayText\":\"Hello\"," - + "\"type\":\"simple_response\",\"platform\":\"google\"" + + "\"type\":\"simple_response\",\"platform\":\"google\"," + + "\"textToSpeech\":\"hello\",\"ssml\":\"ssmlText\",\"displayText\":\"Hello\"" + "}"; private final String TEST_BASIC_CARD = "{" @@ -75,6 +75,20 @@ public class GoogleAssistantResponseMessagesTest { + "\"type\":\"link_out_chip\",\"platform\":\"google\"" + "}"; + @Test + public void testResponseChatBubbleSerialization() { + ResponseChatBubble chatBubble = new ResponseChatBubble(); + chatBubble.setCustomizeAudio(true); + + ResponseChatBubble.Item item = new ResponseChatBubble.Item(); + item.setTextToSpeech("hello"); + item.setSsml("ssmlText"); + item.setDisplayText("Hello"); + + chatBubble.setItems(Arrays.asList(item)); + assertEquals(TEST_CHAT_BUBBLE_SINGLE_ITEM, gson.toJson(chatBubble)); + } + @Test public void testResponseChatBubbleDeserialization() { ResponseChatBubble chatBubble =