Skip to content

Commit

Permalink
Merge pull request #161 from yvasyliev/160-bug-messageevent-doesnt-su…
Browse files Browse the repository at this point in the history
…pport-plain-string-in-payload-probable-fix-provided

Fixed #160
  • Loading branch information
yvasyliev authored Apr 5, 2023
2 parents c9141a7 + 894bb56 commit eefe04d
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ This library uses the next third-party dependencies:
<dependency>
<groupId>com.github.yvasyliev</groupId>
<artifactId>java-vk-bots-longpoll-api</artifactId>
<version>4.1.0</version>
<version>4.1.1</version>
</dependency>
```
4. Extend `LongPollBot` class and override necessary methods:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.github.yvasyliev</groupId>
<artifactId>java-vk-bots-longpoll-api</artifactId>
<packaging>jar</packaging>
<version>4.1.0</version>
<version>4.1.1</version>
<name>Java VK Bots Long Poll API</name>
<description>A Java library to create VK bots using Bots Long Poll API</description>
<url>https://github.com/yvasyliev/java-vk-bots-long-poll-api</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;

import java.lang.reflect.Type;

Expand All @@ -20,7 +21,21 @@ public class PayloadDeserializer implements JsonDeserializer<JsonElement> {
@Override
public JsonElement deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
return jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isString()
? GSON.fromJson(jsonElement.getAsString(), JsonElement.class)
? parseString(jsonElement)
: jsonElement;
}

/**
* Parses string.
*
* @param jsonElement string value.
* @return JSON if provided string is a valid JSON, same string otherwise.
*/
private JsonElement parseString(JsonElement jsonElement) {
try {
return GSON.fromJson(jsonElement.getAsString(), JsonElement.class);
} catch (JsonSyntaxException e) {
return jsonElement;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package api.longpoll.bots.adapters.deserializers;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.annotations.JsonAdapter;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class PayloadDeserializerTest {
Gson gson = new Gson();

@Test
void parseBooleanTest() {
String json = "{\"payload\":true}";

JsonElement payload = gson.fromJson(json, PayloadHolder.class).payload;
assertTrue(payload.isJsonPrimitive());

JsonPrimitive jsonPrimitive = payload.getAsJsonPrimitive();
assertTrue(jsonPrimitive.isBoolean());
assertTrue(jsonPrimitive.getAsBoolean());
}

@Test
void parseStringTest() {
String json = "{\"payload\":\"Hello World!\"}";

JsonElement payload = gson.fromJson(json, PayloadHolder.class).payload;
assertTrue(payload.isJsonPrimitive());

JsonPrimitive jsonPrimitive = payload.getAsJsonPrimitive();
assertTrue(jsonPrimitive.isString());
assertEquals("Hello World!", jsonPrimitive.getAsString());
}

@Test
void parseJsonTest() {
String json = "{\"payload\":{\"foo\":\"bar\"}}";

JsonElement payload = gson.fromJson(json, PayloadHolder.class).payload;
assertTrue(payload.isJsonObject());

JsonObject jsonObject = payload.getAsJsonObject();
assertTrue(jsonObject.has("foo"));

JsonElement foo = jsonObject.get("foo");
assertTrue(foo.isJsonPrimitive());

JsonPrimitive jsonPrimitive = foo.getAsJsonPrimitive();
assertTrue(jsonPrimitive.isString());
assertEquals("bar", jsonPrimitive.getAsString());
}

@Test
void parseStringJsonTest() {
String json = "{\"payload\":\"{\\\"foo\\\":\\\"bar\\\"}\"}";

JsonElement payload = gson.fromJson(json, PayloadHolder.class).payload;
assertTrue(payload.isJsonObject());

JsonObject jsonObject = payload.getAsJsonObject();
assertTrue(jsonObject.has("foo"));

JsonElement foo = jsonObject.get("foo");
assertTrue(foo.isJsonPrimitive());

JsonPrimitive jsonPrimitive = foo.getAsJsonPrimitive();
assertTrue(jsonPrimitive.isString());
assertEquals("bar", jsonPrimitive.getAsString());
}

static class PayloadHolder {
@JsonAdapter(PayloadDeserializer.class)
JsonElement payload;
}
}

0 comments on commit eefe04d

Please sign in to comment.