Fix deserialization bug

This commit is contained in:
rubenlagus 2020-11-08 11:28:57 +00:00 committed by Ruben Bermudez
parent 0bbe66986b
commit 88dd390566
4 changed files with 62 additions and 16 deletions

View File

@ -1,10 +1,10 @@
package org.telegram.telegrambots.meta.api.methods.commands; package org.telegram.telegrambots.meta.api.methods.commands;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod; import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
@ -27,7 +27,7 @@ import java.util.ArrayList;
@Getter @Getter
@Setter @Setter
@ToString @ToString
@NoArgsConstructor @AllArgsConstructor
@Builder @Builder
public class GetMyCommands extends BotApiMethod<ArrayList<BotCommand>> { public class GetMyCommands extends BotApiMethod<ArrayList<BotCommand>> {
public static final String PATH = "getMyCommands"; public static final String PATH = "getMyCommands";

View File

@ -1,15 +1,14 @@
package org.telegram.telegrambots.meta.api.methods.updates; package org.telegram.telegrambots.meta.api.methods.updates;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod; import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.ApiResponse; import org.telegram.telegrambots.meta.api.objects.ApiResponse;
import org.telegram.telegrambots.meta.api.objects.WebhookInfo;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException; import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
@ -28,9 +27,9 @@ import java.io.IOException;
@Getter @Getter
@Setter @Setter
@ToString @ToString
@NoArgsConstructor @AllArgsConstructor
@Builder @Builder
public class Close extends BotApiMethod<WebhookInfo> { public class Close extends BotApiMethod<Boolean> {
public static final String PATH = "close"; public static final String PATH = "close";
@Override @Override
@ -39,15 +38,15 @@ public class Close extends BotApiMethod<WebhookInfo> {
} }
@Override @Override
public WebhookInfo deserializeResponse(String answer) throws TelegramApiRequestException { public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
try { try {
ApiResponse<WebhookInfo> result = OBJECT_MAPPER.readValue(answer, ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
new TypeReference<ApiResponse<WebhookInfo>>() { new TypeReference<ApiResponse<Boolean>>() {
}); });
if (result.getOk()) { if (result.getOk()) {
return result.getResult(); return result.getResult();
} else { } else {
throw new TelegramApiRequestException("Error logging out info", result); throw new TelegramApiRequestException("Error closing", result);
} }
} catch (IOException e2) { } catch (IOException e2) {
throw new TelegramApiRequestException("Unable to deserialize response", e2); throw new TelegramApiRequestException("Unable to deserialize response", e2);

View File

@ -9,7 +9,6 @@ import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod; import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.ApiResponse; import org.telegram.telegrambots.meta.api.objects.ApiResponse;
import org.telegram.telegrambots.meta.api.objects.WebhookInfo;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException; import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
@ -30,7 +29,7 @@ import java.io.IOException;
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
public class LogOut extends BotApiMethod<WebhookInfo> { public class LogOut extends BotApiMethod<Boolean> {
public static final String PATH = "logOut"; public static final String PATH = "logOut";
@Override @Override
@ -39,15 +38,15 @@ public class LogOut extends BotApiMethod<WebhookInfo> {
} }
@Override @Override
public WebhookInfo deserializeResponse(String answer) throws TelegramApiRequestException { public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
try { try {
ApiResponse<WebhookInfo> result = OBJECT_MAPPER.readValue(answer, ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
new TypeReference<ApiResponse<WebhookInfo>>() { new TypeReference<ApiResponse<Boolean>>() {
}); });
if (result.getOk()) { if (result.getOk()) {
return result.getResult(); return result.getResult();
} else { } else {
throw new TelegramApiRequestException("Error logging out info", result); throw new TelegramApiRequestException("Error logging out", result);
} }
} catch (IOException e2) { } catch (IOException e2) {
throw new TelegramApiRequestException("Unable to deserialize response", e2); throw new TelegramApiRequestException("Unable to deserialize response", e2);

View File

@ -5,7 +5,10 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.telegram.telegrambots.meta.api.methods.commands.GetMyCommands;
import org.telegram.telegrambots.meta.api.methods.updates.Close;
import org.telegram.telegrambots.meta.api.methods.updates.GetUpdates; import org.telegram.telegrambots.meta.api.methods.updates.GetUpdates;
import org.telegram.telegrambots.meta.api.methods.updates.LogOut;
import org.telegram.telegrambots.meta.api.objects.ApiResponse; import org.telegram.telegrambots.meta.api.objects.ApiResponse;
import org.telegram.telegrambots.meta.api.objects.Audio; import org.telegram.telegrambots.meta.api.objects.Audio;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery; import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
@ -17,6 +20,7 @@ import org.telegram.telegrambots.meta.api.objects.MessageEntity;
import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.User; import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.api.objects.Voice; import org.telegram.telegrambots.meta.api.objects.Voice;
import org.telegram.telegrambots.meta.api.objects.commands.BotCommand;
import org.telegram.telegrambots.meta.api.objects.inlinequery.ChosenInlineQuery; import org.telegram.telegrambots.meta.api.objects.inlinequery.ChosenInlineQuery;
import org.telegram.telegrambots.meta.api.objects.inlinequery.InlineQuery; import org.telegram.telegrambots.meta.api.objects.inlinequery.InlineQuery;
@ -151,6 +155,50 @@ class TestDeserialization {
} }
} }
@Test
void TestDeserializationCloseMethod() throws Exception {
String updateText = "{\"ok\":true,\"result\": true}";
Boolean response = new Close().deserializeResponse(updateText);
assertTrue(response);
}
@Test
void TestDeserializationLogoutMethod() throws Exception {
String updateText = "{\"ok\":true,\"result\": true}";
Boolean response = new LogOut().deserializeResponse(updateText);
assertTrue(response);
}
@Test
void TestDeserializationGetMyCommandsMethod() throws Exception {
String updateText = "{\n" +
" \"ok\": true,\n" +
" \"result\": [\n" +
" {\n" +
" \"command\": \"enabled\",\n" +
" \"description\": \"Enabled Command\"\n" +
" },\n" +
" {\n" +
" \"command\": \"disabled\",\n" +
" \"description\": \"Disabled Command\"\n" +
" }\n" +
" ]\n" +
"}";
ArrayList<BotCommand> response = new GetMyCommands().deserializeResponse(updateText);
assertNotNull(response);
assertEquals(2, response.size());
assertEquals("enabled", response.get(0).getCommand());
assertEquals("Enabled Command", response.get(0).getDescription());
assertEquals("disabled", response.get(1).getCommand());
assertEquals("Disabled Command", response.get(1).getDescription());
}
@Test @Test
void TestUpdateDeserialization() throws Exception { void TestUpdateDeserialization() throws Exception {
Update update = mapper.readValue(TelegramBotsHelper.GetUpdate(), Update.class); Update update = mapper.readValue(TelegramBotsHelper.GetUpdate(), Update.class);