Skip to content

Commit

Permalink
Fix Gson type adapters fallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
pengrad committed Aug 20, 2024
1 parent dcb52ff commit ba791f7
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Objects;

public abstract class BackgroundFill {
public class BackgroundFill {

private final String type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Objects;

public abstract class BackgroundType {
public class BackgroundType {

private final String type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ public class ChatBoostSource implements Serializable {
protected String source;
protected User user;

ChatBoostSource() {
}

public ChatBoostSource(String source) {
this.source = source;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.Serializable;
import java.util.Objects;

public abstract class PaidMedia implements Serializable {
public class PaidMedia implements Serializable {

private final static long serialVersionUID = 0L;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.pengrad.telegrambot.model.reaction;

import java.io.Serializable;
import java.util.Objects;

public abstract class ReactionType {
public class ReactionType implements Serializable {
private final static long serialVersionUID = 0L;

private final String type;

Expand Down Expand Up @@ -30,7 +32,7 @@ public int hashCode() {
@Override
public String toString() {
return "ReactionType{" +
"type='" + type + '\'' +
'}';
"type='" + type + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.Serializable;
import java.util.Objects;

public abstract class TransactionPartner implements Serializable {
public class TransactionPartner implements Serializable {

private final static long serialVersionUID = 0L;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.pengrad.telegrambot.model.stars.withdrawal;

import com.pengrad.telegrambot.model.chatbackground.BackgroundFill;

import java.io.Serializable;
import java.util.Objects;

public abstract class RevenueWithdrawalState implements Serializable {
public class RevenueWithdrawalState implements Serializable {

private final static long serialVersionUID = 0L;

private String type = "unknown";

protected RevenueWithdrawalState(String type) {
public RevenueWithdrawalState(String type) {
this.type = type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class BackgroundFillAdapter implements JsonDeserializer<BackgroundFill> {
@Override
public BackgroundFill deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = element.getAsJsonObject();
String discriminator = object.getAsJsonPrimitive("type").getAsString();
JsonPrimitive primitive = object.getAsJsonPrimitive("type");
String discriminator = primitive != null ? primitive.getAsString() : "unknown";

if (BackgroundFillSolid.TYPE.equals(discriminator)) {
return context.deserialize(object, BackgroundFillSolid.class);
Expand All @@ -23,6 +24,6 @@ public BackgroundFill deserialize(JsonElement element, Type type, JsonDeserializ
return context.deserialize(object, BackgroundFillFreeformGradient.class);
}

return context.deserialize(object, BackgroundFill.class);
return new BackgroundFill(discriminator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class BackgroundTypeAdapter implements JsonDeserializer<BackgroundType> {
@Override
public BackgroundType deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = element.getAsJsonObject();
String discriminator = object.getAsJsonPrimitive("type").getAsString();
JsonPrimitive primitive = object.getAsJsonPrimitive("type");
String discriminator = primitive != null ? primitive.getAsString() : "unknown";

if (BackgroundTypeFill.TYPE.equals(discriminator)) {
return context.deserialize(object, BackgroundTypeFill.class);
Expand All @@ -26,6 +27,6 @@ public BackgroundType deserialize(JsonElement element, Type type, JsonDeserializ
return context.deserialize(object, BackgroundTypeChatTheme.class);
}

return context.deserialize(object, BackgroundType.class);
return new BackgroundType(discriminator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class ChatBoostSourceTypeAdapter implements JsonDeserializer<ChatBoostSou
@Override
public ChatBoostSource deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = element.getAsJsonObject();
String discriminator = object.getAsJsonPrimitive("source").getAsString();
JsonPrimitive primitive = object.getAsJsonPrimitive("source");
String discriminator = primitive != null ? primitive.getAsString() : "unknown";

if (ChatBoostSourceGiftCode.CHAT_BOOST_TYPE.equals(discriminator)) {
return context.deserialize(object, ChatBoostSourceGiftCode.class);
Expand All @@ -23,6 +24,6 @@ public ChatBoostSource deserialize(JsonElement element, Type type, JsonDeseriali
return context.deserialize(object, ChatBoostSourcePremium.class);
}

return context.deserialize(object, ChatBoostSource.class);
return new ChatBoostSource(discriminator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public MaybeInaccessibleMessage deserialize(JsonElement element, Type type, Json
JsonObject object = element.getAsJsonObject();
JsonPrimitive discriminator = object.getAsJsonPrimitive("date");

if (!discriminator.isJsonNull() && discriminator.getAsInt() != InaccessibleMessage.INACCESSIBLE_MESSAGE_DATE) {
if (discriminator != null && !discriminator.isJsonNull() && discriminator.getAsInt() != InaccessibleMessage.INACCESSIBLE_MESSAGE_DATE) {
return context.deserialize(object, Message.class);
} else {
return context.deserialize(object, InaccessibleMessage.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class MessageOriginTypeAdapter implements JsonDeserializer<MessageOrigin>
@Override
public MessageOrigin deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = element.getAsJsonObject();
String discriminator = object.getAsJsonPrimitive("type").getAsString();
JsonPrimitive primitive = object.getAsJsonPrimitive("type");
String discriminator = primitive != null ? primitive.getAsString() : "unknown";

if (MessageOriginChannel.MESSAGE_ORIGIN_TYPE.equals(discriminator)) {
return context.deserialize(object, MessageOriginChannel.class);
Expand All @@ -22,6 +23,6 @@ public MessageOrigin deserialize(JsonElement element, Type type, JsonDeserializa
return context.deserialize(object, MessageOriginUser.class);
}

return context.deserialize(object, MessageOrigin.class);
return new MessageOrigin(discriminator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class PaidMediaTypeAdapter implements JsonDeserializer<PaidMedia> {
@Override
public PaidMedia deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = element.getAsJsonObject();
String discriminator = object.getAsJsonPrimitive("type").getAsString();
JsonPrimitive primitive = object.getAsJsonPrimitive("type");
String discriminator = primitive != null ? primitive.getAsString() : "unknown";

if (PaidMediaPreview.TYPE.equals(discriminator)) {
return context.deserialize(object, PaidMediaPreview.class);
Expand All @@ -23,6 +24,6 @@ public PaidMedia deserialize(JsonElement element, Type type, JsonDeserialization
return context.deserialize(object, PaidMediaVideo.class);
}

return context.deserialize(object, PaidMedia.class);
return new PaidMedia(discriminator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ public class ReactionTypeAdapter implements JsonDeserializer<ReactionType> {
@Override
public ReactionType deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = element.getAsJsonObject();
String discriminator = object.getAsJsonPrimitive("type").getAsString();
JsonPrimitive primitive = object.getAsJsonPrimitive("type");
String discriminator = primitive != null ? primitive.getAsString() : "unknown";

if (ReactionTypeEmoji.EMOJI_TYPE.equals(discriminator)) {
return context.deserialize(object, ReactionTypeEmoji.class);
} else if (ReactionTypeCustomEmoji.CUSTOM_EMOJI_TYPE.equals(discriminator)) {
return context.deserialize(object, ReactionTypeCustomEmoji.class);
}

return context.deserialize(object, ReactionType.class);
return new ReactionType(discriminator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class RevenueWithdrawalStateTypeAdapter implements JsonDeserializer<Reven
@Override
public RevenueWithdrawalState deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = element.getAsJsonObject();
String discriminator = object.getAsJsonPrimitive("type").getAsString();
JsonPrimitive primitive = object.getAsJsonPrimitive("type");
String discriminator = primitive != null ? primitive.getAsString() : "unknown";

if (RevenueWithdrawalStatePending.TYPE.equals(discriminator)) {
return context.deserialize(object, RevenueWithdrawalStatePending.class);
Expand All @@ -23,6 +24,6 @@ public RevenueWithdrawalState deserialize(JsonElement element, Type type, JsonDe
return context.deserialize(object, RevenueWithdrawalStateFailed.class);
}

return context.deserialize(object, RevenueWithdrawalState.class);
return new RevenueWithdrawalState(discriminator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class TransactionPartnerTypeAdapter implements JsonDeserializer<Transacti
@Override
public TransactionPartner deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = element.getAsJsonObject();
String discriminator = object.getAsJsonPrimitive("type").getAsString();
JsonPrimitive primitive = object.getAsJsonPrimitive("type");
String discriminator = primitive != null ? primitive.getAsString() : "unknown";

if (TransactionPartnerUser.TYPE.equals(discriminator)) {
return context.deserialize(object, TransactionPartnerUser.class);
Expand All @@ -26,6 +27,6 @@ public TransactionPartner deserialize(JsonElement element, Type type, JsonDeseri
return context.deserialize(object, TransactionPartnerOther.class);
}

return context.deserialize(object, TransactionPartner.class);
return new TransactionPartner(discriminator);
}
}
23 changes: 23 additions & 0 deletions library/src/test/java/com/pengrad/telegrambot/GsonAdaptersTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.pengrad.telegrambot

import com.google.gson.JsonDeserializer
import com.pengrad.telegrambot.utility.BotUtils.GSON
import org.junit.Assert.assertNotNull
import org.junit.Test
import org.reflections.Reflections
import java.lang.reflect.ParameterizedType


class GsonAdaptersTest {

@Test
fun `all adapters fallback to base class`() {
val reflections = Reflections(TelegramBot::class.java.getPackage().name)
val adapters = reflections.getSubTypesOf(JsonDeserializer::class.java)
val types = adapters.map { (it.genericInterfaces[0] as ParameterizedType).actualTypeArguments[0] }
types.forEach {
assertNotNull(it.typeName, GSON.fromJson("{}", it))
}
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- This module was also published with a richer model, Gradle metadata, -->
<!-- which should be used instead. Do not delete the following line which -->
Expand Down

0 comments on commit ba791f7

Please sign in to comment.