Skip to content

Commit

Permalink
Add AvroSchema UUID support
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Royer authored and eolivelli committed May 14, 2021
1 parent 1a94bbc commit d5aeb66
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public static void addLogicalTypeConversions(ReflectData reflectData, boolean js
// Skip if have not provide joda-time dependency.
}
}
reflectData.addLogicalTypeConversion(new Conversions.UUIDConversion());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
*/
package org.apache.pulsar.client.impl.schema.util;

import org.apache.avro.Conversions;
import org.apache.avro.Schema;
import org.apache.avro.reflect.ReflectData;
import org.apache.commons.lang3.StringUtils;
import org.apache.pulsar.client.api.schema.SchemaDefinition;
import org.apache.pulsar.client.impl.schema.AvroSchema;
import org.apache.pulsar.client.impl.schema.SchemaDefinitionBuilderImpl;
import org.apache.pulsar.common.schema.SchemaInfo;
import org.apache.pulsar.common.schema.SchemaType;
Expand Down Expand Up @@ -88,8 +90,10 @@ public static Schema extractAvroSchema(SchemaDefinition schemaDefinition, Class
try {
return parseAvroSchema(pojo.getDeclaredField("SCHEMA$").get(null).toString());
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException ignored) {
return schemaDefinition.getAlwaysAllowNull() ? ReflectData.AllowNull.get().getSchema(pojo)
: ReflectData.get().getSchema(pojo);
ReflectData reflectData = schemaDefinition.getAlwaysAllowNull() ? ReflectData.AllowNull.get()
: ReflectData.get();
AvroSchema.addLogicalTypeConversions(reflectData, schemaDefinition.isJsr310ConversionEnabled());
return reflectData.getSchema(pojo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import java.util.UUID;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -426,4 +428,17 @@ public void testAvroSchemaUserDefinedReadAndWriter() {
assertEquals(field1, foo.getField1());
}

static class MyPojo {
public UUID uid;
}

@Test
public void testAvroUUID() {
org.apache.pulsar.client.api.Schema<MyPojo> schema = org.apache.pulsar.client.api.Schema.AVRO(MyPojo.class);
MyPojo pojo1 = new MyPojo();
pojo1.uid = UUID.randomUUID();
MyPojo pojo2 = schema.decode(schema.encode(pojo1));
assertEquals(pojo1.uid, pojo2.uid);
}

}

0 comments on commit d5aeb66

Please sign in to comment.