This is an automated email from the ASF dual-hosted git repository.

eolivelli pushed a commit to branch branch-2.8
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/branch-2.8 by this push:
     new 414352b  [Issue #10427] Add AvroSchema UUID support fix  (#10428)
414352b is described below

commit 414352b861de215c63d7d3cd2dc617ffa086566e
Author: Vincent Royer <[email protected]>
AuthorDate: Tue Jul 13 09:33:24 2021 +0200

    [Issue #10427] Add AvroSchema UUID support fix  (#10428)
    
    (cherry picked from commit 8501345c90ee524647cef49a2d1110301e505092)
---
 .../org/apache/pulsar/client/impl/schema/AvroSchema.java  |  1 +
 .../apache/pulsar/client/impl/schema/util/SchemaUtil.java |  9 +++++++--
 .../apache/pulsar/client/impl/schema/AvroSchemaTest.java  | 15 +++++++++++++++
 3 files changed, 23 insertions(+), 2 deletions(-)

diff --git 
a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/AvroSchema.java
 
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/AvroSchema.java
index be4b3fc..cc31b19 100644
--- 
a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/AvroSchema.java
+++ 
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/AvroSchema.java
@@ -121,6 +121,7 @@ public class AvroSchema<T> extends AvroBaseStructSchema<T> {
                 // Skip if have not provide joda-time dependency.
             }
         }
+        reflectData.addLogicalTypeConversion(new Conversions.UUIDConversion());
     }
 
     public static class TimestampConversion extends Conversion<DateTime> {
diff --git 
a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/util/SchemaUtil.java
 
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/util/SchemaUtil.java
index 70d7fc0..2d0c810 100644
--- 
a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/util/SchemaUtil.java
+++ 
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/util/SchemaUtil.java
@@ -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;
@@ -89,8 +91,11 @@ public class SchemaUtil {
         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);
         }
     }
 }
diff --git 
a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/AvroSchemaTest.java
 
b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/AvroSchemaTest.java
index 66653e3..00cbbdd 100644
--- 
a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/AvroSchemaTest.java
+++ 
b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/AvroSchemaTest.java
@@ -35,6 +35,8 @@ import java.time.LocalDate;
 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;
@@ -423,4 +425,17 @@ public class AvroSchemaTest {
         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);
+    }
+
 }

Reply via email to