Skip to content

Commit

Permalink
[Issue #10427] Add AvroSchema UUID support fix (#10428)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Royer committed Jul 13, 2021
1 parent be63e87 commit 8501345
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public static void addLogicalTypeConversions(ReflectData reflectData, boolean js
// Skip if have not provide joda-time dependency.
}
}
reflectData.addLogicalTypeConversion(new Conversions.UUIDConversion());
}

public static class TimestampConversion extends Conversion<DateTime> {
Expand Down
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.client.impl.schema.SchemaInfoImpl;
import org.apache.pulsar.common.schema.SchemaInfo;
Expand Down Expand Up @@ -89,8 +91,11 @@ 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()
? new ReflectData.AllowNull()
: new ReflectData();
AvroSchema.addLogicalTypeConversions(reflectData, schemaDefinition.isJsr310ConversionEnabled());
return reflectData.getSchema(pojo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.UUID;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.avro.Schema;
Expand Down Expand Up @@ -423,4 +425,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 8501345

Please sign in to comment.