Skip to content

Commit

Permalink
Add support for generic response
Browse files Browse the repository at this point in the history
  • Loading branch information
antas-marcin committed Mar 5, 2024
1 parent 6cd8937 commit 762b169
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/main/java/io/weaviate/client/base/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import io.weaviate.client.v1.graphql.model.GraphQLTypedResponse;
import java.lang.reflect.Type;

public class Serializer {
private Gson gson;
Expand All @@ -11,7 +14,16 @@ public Serializer() {
}

public <C> C toResponse(String response, Class<C> classOfT) {
return gson.fromJson(response, classOfT);
return gson.fromJson(response, TypeToken.get(classOfT));
}

public <C> GraphQLTypedResponse<C> toResponseTyped(String response, Class<C> classOfT) {
return gson.fromJson(response,
TypeToken.getParameterized(GraphQLTypedResponse.class, classOfT).getType());
}

public <C> C toResponse(String response, Type typeOfT) {
return gson.fromJson(response, typeOfT);
}

public String toJsonString(Object object) {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/io/weaviate/client/base/TypedSerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.weaviate.client.base;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import io.weaviate.client.v1.graphql.model.GraphQLTypedResponse;
import java.lang.reflect.Type;

public class TypedSerializer<T> {
private Class<T> givenClass;
private final Gson gson;

public TypedSerializer() {
this.gson = new GsonBuilder().disableHtmlEscaping().create();
}

public GraphQLTypedResponse<T> toResponseTyped(String response, Class<T> classOfT) {
return gson.fromJson(response,
TypeToken.getParameterized(GraphQLTypedResponse.class, classOfT).getType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.weaviate.client.v1.graphql.model;

import com.google.gson.annotations.SerializedName;
import java.util.Map;
import lombok.Getter;

@Getter
public class GraphQLBaseObject {
@SerializedName(value = "_additional")
Additional additional;

@Getter
public static class Additional {
String id;
Float certainty;
Float[] vector;
Map<String, Float[]> vectors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@ToString
@EqualsAndHashCode
@FieldDefaults(level = AccessLevel.PRIVATE)
public class GraphQLResponse {
Object data;
public class GraphQLResponse<T> {
T data;
GraphQLError[] errors;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.weaviate.client.v1.graphql.model;

import com.google.gson.annotations.SerializedName;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import lombok.experimental.FieldDefaults;

@Getter
@Builder
@ToString
@EqualsAndHashCode
@FieldDefaults(level = AccessLevel.PRIVATE)
public class GraphQLTypedResponse<T> {
Operation<T> data;
GraphQLError[] errors;

@Getter
@ToString
@EqualsAndHashCode
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public static class Operation<T> {
@SerializedName(value = "Get", alternate = {"Aggregate", "Explore"})
private T objects;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.weaviate.client.v1.graphql.model;

import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import io.weaviate.client.base.Serializer;
import io.weaviate.client.base.TypedSerializer;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Spliterator;
import lombok.Getter;
import static org.assertj.core.api.Assertions.assertThat;

import junit.framework.TestCase;
import org.junit.Test;

public class GraphQLTypedResponseTest extends TestCase {

@Test
public void testGraphQLGetResponse() throws IOException {
// given
Serializer s = new Serializer();
String json = new String(Files.readAllBytes(Paths.get("src/test/resources/json/graphql-response.json")));
// when
// Type responseType = new TypeToken<GraphQLTypedResponse<Soups>>() {}.getType();
Type responseType = TypeToken.getParameterized(GraphQLTypedResponse.class, Soups.class).getType();
GraphQLTypedResponse < Soups > resp = s.toResponse(json, responseType);
//
assertThat(resp).isNotNull()
.extracting(o -> o.getData().getObjects().getSoups())
.extracting(o -> o.get(0)).isNotNull()
.extracting(Soups.Soup::getName).isEqualTo("JustSoup");
}

@Test
public void testGraphQLGetResponseTyped() throws IOException {
// given
Serializer s = new Serializer();
String json = new String(Files.readAllBytes(Paths.get("src/test/resources/json/graphql-response.json")));
// when
// Type responseType = new TypeToken<GraphQLTypedResponse<Soups>>() {}.getType();
GraphQLTypedResponse<Soups> resp = s.toResponseTyped(json, Soups.class);
//
assertThat(resp).isNotNull()
.extracting(o -> o.getData().getObjects().getSoups())
.extracting(o -> o.get(0)).isNotNull()
.extracting(Soups.Soup::getName).isEqualTo("JustSoup");
}
}

@Getter
class Soups {
@SerializedName(value = "Soup")
List<Soup> soups;

@Getter
public static class Soup extends GraphQLBaseObject {
String name;
}
}
14 changes: 14 additions & 0 deletions src/test/resources/json/graphql-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"data": {
"Get": {
"Soup": [
{
"_additional": {
"certainty": 0.9999998211860657
},
"name": "JustSoup"
}
]
}
}
}

0 comments on commit 762b169

Please sign in to comment.