Skip to content

Commit b0edb54

Browse files
scroogemcfawkrubenlagusclaude
committed
fix: Message.getCommand() might return null if text of MessageEntity objects is not computed yet (#1602)
* fix(richtext): serialize RichTextPlain as a bare JSON string The Bot API defines RichText as "either a String for plain text, an Array of RichText, or any of the following types" - there is no "plain" type on the wire. RichTextPlain exists only to give plain text a place in the Java RichText hierarchy. RichTextDeserializer already mapped a bare JSON string to RichTextPlain on the way in, but nothing handled the way out: RichTextPlain had no custom serializer, so Jackson wrote it as {"type":"plain","text":...}. Telegram rejected such payloads with: Bad Request: Can't parse PageBlockTableCell: Unsupported rich text type Add RichTextPlainSerializer, mirroring the existing RichTextConcatSerializer for the other library-only pseudo-type. It writes the bare string and overrides serializeWithType so the @JsonTypeInfo machinery on the RichText interface cannot re-wrap it in a type envelope. Reading {"type":"plain",...} keeps working, so JSON persisted by the old serializer still deserializes. Fixes #1599 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Bug fixing v10.2.1 * fix: compute text of MessageEntity objects on Message.getCommand() --------- Co-authored-by: ruben <rberlopez@gmail.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Ruben Bermudez <rubenlagus@users.noreply.github.com>
1 parent 26e8b35 commit b0edb54

4 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/MessageEntity.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ public class MessageEntity implements BotApiObject {
127127

128128
public void computeText(String message) {
129129
if (message != null) {
130+
if (text != null) {
131+
return;
132+
}
130133
text = message.substring(offset, offset + length);
131134
}
132135
}

‎telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/message/Message.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ public boolean isCommand() {
10981098
@JsonIgnore
10991099
public String getCommand() {
11001100
if (hasText() && hasEntities()) {
1101-
for (var entity : entities) {
1101+
for (var entity : getEntities()) {
11021102
if (entity != null && NumberUtils.INTEGER_ZERO.equals(entity.getOffset())) {
11031103
if (EntityType.BOTCOMMAND.equals(entity.getType())) {
11041104
return entity.getText();

‎telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TelegramBotsHelper.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,8 @@ public static String GetSetGameScoreBooleanResponse() {
196196
public static String GetSetGameScoreMessageResponse() {
197197
return "{\"ok\": true,\"result\": {\"date\": 1441645532,\"chat\": {\"last_name\": \"Test Lastname\",\"id\": 1111111,\"type\": \"private\",\"first_name\": \"Test Firstname\",\"username\": \"Testusername\"},\"message_id\": 1365,\"from\": {\"last_name\": \"Test Lastname\",\"id\": 1111111,\"first_name\": \"Test Firstname\",\"username\": \"Testusername\",\"is_bot\": false},\"text\": \"Original\"}}";
198198
}
199+
200+
public static String GetCommandMessage() {
201+
return "{\"message_id\": 1, \"date\": 1, \"text\": \"/debug\", \"entities\": [{\"type\": \"bot_command\", \"offset\": 0, \"length\": 6}]}";
202+
}
199203
}

‎telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestDeserialization.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,12 @@ void TestUpdateDeserialization() throws Exception {
458458
assertUpdate(update);
459459
}
460460

461+
@Test
462+
void TestMessageEntityTextComputedOnDeserialization() throws Exception {
463+
Message message = mapper.readValue(TelegramBotsHelper.GetCommandMessage(), Message.class);
464+
assertNotNull(message.getCommand());
465+
}
466+
461467
@Test
462468
void TestUpdateDeserializationWithInlineKeyboard() throws Exception {
463469
Update update = mapper.readValue(TelegramBotsHelper.GetUpdateWithMessageInCallbackQuery(), Update.class);
@@ -631,4 +637,4 @@ private void assertChannelChat(Chat chat) {
631637
assertTrue(chat.isChannelChat());
632638
assertEquals("Test channel", chat.getTitle());
633639
}
634-
}
640+
}

0 commit comments

Comments
 (0)