Skip to content

Commit

Permalink
PR comment - generic T on JSONSchema
Browse files Browse the repository at this point in the history
Signed-off-by: Shivesh Ranjan <[email protected]>
  • Loading branch information
shiveshr committed Jul 17, 2020
1 parent fe0974f commit d464b6b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package io.pravega.schemaregistry.schemas;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;
Expand Down Expand Up @@ -105,18 +106,20 @@ public static JSONSchema<Object> of(String type, JsonSchema schema) {
}

/**
* Method to create a typed JSONSchema of type {@link Object} from the given schema string.
* Method to create a typed JSONSchema of type T from the given schema string.
*
* @param type type of object identified by {@link SchemaInfo#getType()}.
* @param schemaString Schema string to use.
* @param tClass class for the type of object
* @param <T> Type of object
* @return Returns an JSONSchema with {@link Object} type.
*/
public static JSONSchema<Object> of(String type, String schemaString) {
public static <T> JSONSchema<T> of(String type, String schemaString, Class<T> tClass) {
Preconditions.checkNotNull(type, "Type cannot be null.");
Preconditions.checkArgument(!Strings.isNullOrEmpty(schemaString), "Schema String cannot be null or empty.");
try {
JsonSchema schema = OBJECT_MAPPER.readValue(schemaString, JsonSchema.class);
return new JSONSchema<>(schema, type, schemaString, Object.class);
return new JSONSchema<>(schema, type, schemaString, tClass);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Unable to parse schema string", e);
}
Expand Down Expand Up @@ -147,18 +150,18 @@ public static <T> JSONSchema<T> ofBaseType(Class<? extends T> tDerived, Class<T>
}

/**
* Method to create a typed JSONSchema of type {@link Object} from the given schema.
* Method to create a typed JSONSchema of type {@link JsonNode} from the given schema.
*
* @param schemaInfo Schema info to translate into json schema.
* @return Returns an JSONSchema with {@link Object} type.
* @return Returns an JSONSchema with {@link JsonNode} type.
*/
public static JSONSchema<Object> from(SchemaInfo schemaInfo) {
public static JSONSchema<JsonNode> from(SchemaInfo schemaInfo) {
Preconditions.checkNotNull(schemaInfo);
try {
String schemaString = new String(schemaInfo.getSchemaData().array(), Charsets.UTF_8);

JsonSchema schema = OBJECT_MAPPER.readValue(schemaString, JsonSchema.class);
return new JSONSchema<>(schemaInfo, schema, schemaString, Object.class);
return new JSONSchema<>(schemaInfo, schema, schemaString, JsonNode.class);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Unable to get json schema from schema info", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class JsonGenericDeserializer extends AbstractDeserializer<WithSchema<JsonNode>>
super(groupId, client, null, false, decoders, encodingCache, encodeHeader);
this.objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
objectMapper.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,13 @@ private static AbstractSerializer<Object> getPravegaSerializer(
return new AvroSerializer<>(groupId, schemaRegistryClient,
AvroSchema.from(schemaInfo), config.getEncoder(), config.isRegisterSchema());
case Protobuf:
ProtobufSerializer<?> m = new ProtobufSerializer<>(groupId, schemaRegistryClient,
ProtobufSerializer<?> pSerializer = new ProtobufSerializer<>(groupId, schemaRegistryClient,
ProtobufSchema.from(schemaInfo), config.getEncoder(), config.isRegisterSchema(), config.isWriteEncodingHeader());
return (AbstractSerializer<Object>) m;
return (AbstractSerializer<Object>) pSerializer;
case Json:
return new JsonSerializer<>(groupId, schemaRegistryClient, JSONSchema.from(schemaInfo),
JsonSerializer<?> jsonSerializer = new JsonSerializer<>(groupId, schemaRegistryClient, JSONSchema.from(schemaInfo),
config.getEncoder(), config.isRegisterSchema(), config.isWriteEncodingHeader());
return (AbstractSerializer<Object>) jsonSerializer;
case Custom:
return getCustomSerializer(config, customSerializers, schemaRegistryClient, groupId, schemaInfo);
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.google.protobuf.DescriptorProtos;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.GeneratedMessageV3;
Expand Down Expand Up @@ -212,6 +213,8 @@ private static String toJsonString(SerializationFormat format, Object deserializ
* @return A WithSchema object which has Avro Schema and the corresponding object.
*/
public static <T> WithSchema<T> avro(T object, AvroSchema<T> avroSchema) {
Preconditions.checkNotNull(object, "object cannot be null");
Preconditions.checkNotNull(avroSchema, "schema cannot be null");
return new WithSchema<>(avroSchema.getSchemaInfo(), object, (x, y) -> object);
}

Expand All @@ -224,6 +227,8 @@ public static <T> WithSchema<T> avro(T object, AvroSchema<T> avroSchema) {
* @return A WithSchema object which has Protobuf Schema and the corresponding object.
*/
public static <T extends GeneratedMessageV3> WithSchema<T> proto(T object, ProtobufSchema<T> protobufSchema) {
Preconditions.checkNotNull(object, "object cannot be null");
Preconditions.checkNotNull(protobufSchema, "schema cannot be null");
return new WithSchema<>(protobufSchema.getSchemaInfo(), object, (x, y) -> object);
}

Expand All @@ -236,6 +241,8 @@ public static <T extends GeneratedMessageV3> WithSchema<T> proto(T object, Proto
* @return A WithSchema object which has Json schema and the corresponding object.
*/
public static <T> WithSchema<T> json(T object, JSONSchema<T> jsonSchema) {
Preconditions.checkNotNull(object, "object cannot be null");
Preconditions.checkNotNull(jsonSchema, "schema cannot be null");
return new WithSchema<>(jsonSchema.getSchemaInfo(), object, (x, y) -> object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
package io.pravega.schemaregistry.schemas;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.protobuf.DescriptorProtos;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.GeneratedMessageV3;
Expand Down Expand Up @@ -99,12 +98,12 @@ public void testProtobufSchema() throws IOException {
}

@Test
public void testJsonSchema() throws JsonProcessingException {
public void testJsonSchema() {
JSONSchema<User> schema = JSONSchema.of(User.class);
assertNotNull(schema.getSchema());
assertEquals(schema.getSchemaInfo().getSerializationFormat(), SerializationFormat.Json);

JSONSchema<Object> schema2 = JSONSchema.of("Person", JSON_SCHEMA_STRING);
JSONSchema<Object> schema2 = JSONSchema.of("Person", JSON_SCHEMA_STRING, Object.class);
assertNotNull(schema2.getSchema());
assertEquals(schema2.getSchemaInfo().getSerializationFormat(), SerializationFormat.Json);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,34 +279,34 @@ public void testJsonSerializers() throws JsonProcessingException {

String schemaString = "{\"type\": \"object\",\"title\": \"The external data schema\",\"properties\": {\"content\": {\"type\": \"string\"}}}";

JSONSchema<Object> myData = JSONSchema.of("MyData", schemaString);
JSONSchema<HashMap> myData = JSONSchema.of("MyData", schemaString, HashMap.class);
VersionInfo versionInfo3 = new VersionInfo("myData", 0, 2);
doAnswer(x -> versionInfo3).when(client).getVersionForSchema(anyString(), eq(myData.getSchemaInfo()));
doAnswer(x -> new EncodingId(2)).when(client).getEncodingId(anyString(), eq(versionInfo3), any());
doAnswer(x -> new EncodingInfo(versionInfo3, myData.getSchemaInfo(), Codecs.None.getCodec().getCodecType())).when(client).getEncodingInfo(anyString(), eq(new EncodingId(2)));

Serializer<Object> serializer2 = SerializerFactory.jsonSerializer(config, myData);
Map<String, String> jsonObject = new HashMap<>();
Serializer<HashMap> serializer2 = SerializerFactory.jsonSerializer(config, myData);
HashMap<String, String> jsonObject = new HashMap<>();
jsonObject.put("content", "mxx");

ByteBuffer s = serializer2.serialize(jsonObject);
str = stringDeserializer.deserialize(s);

String stringSchema = new ObjectMapper().writeValueAsString(JsonSchema.minimalForFormat(JsonFormatTypes.STRING));

JSONSchema<Object> strSchema = JSONSchema.of("string", stringSchema);
JSONSchema<String> strSchema = JSONSchema.of("string", stringSchema, String.class);
VersionInfo versionInfo4 = new VersionInfo("myData", 0, 3);
doAnswer(x -> versionInfo4).when(client).getVersionForSchema(anyString(), eq(strSchema.getSchemaInfo()));
doAnswer(x -> new EncodingId(3)).when(client).getEncodingId(anyString(), eq(versionInfo4), any());
doAnswer(x -> new EncodingInfo(versionInfo4, strSchema.getSchemaInfo(), Codecs.None.getCodec().getCodecType())).when(client).getEncodingInfo(anyString(), eq(new EncodingId(3)));

Serializer<Object> serializer3 = SerializerFactory.jsonSerializer(config, strSchema);
Serializer<Object> deserializer3 = SerializerFactory.jsonDeserializer(config, strSchema);
Serializer<String> serializer3 = SerializerFactory.jsonSerializer(config, strSchema);
Serializer<String> deserializer3 = SerializerFactory.jsonDeserializer(config, strSchema);
Serializer<WithSchema<JsonNode>> generic3 = SerializerFactory.jsonGenericDeserializer(config);
String string = "a";
s = serializer3.serialize(string);
Object x = deserializer3.deserialize(s);
assertTrue(x instanceof String);
assertNotNull(x);
assertEquals(x, string);
s = serializer3.serialize(string);
Object jsonNode = generic3.deserialize(s);
Expand Down

0 comments on commit d464b6b

Please sign in to comment.