From 8e40820378ea551826615243e1216ee6f7fa8adf Mon Sep 17 00:00:00 2001 From: Sergii Zhevzhyk Date: Fri, 21 Feb 2020 23:40:52 +0100 Subject: [PATCH] [Issue 3762][Schema] Fix the problem with parsing of an Avro schema related to shading in pulsar-client. --- .../pulsar/client/impl/schema/StructSchema.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/StructSchema.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/StructSchema.java index 1302d7c84985f..f9faa0b1156bc 100644 --- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/StructSchema.java +++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/StructSchema.java @@ -150,8 +150,7 @@ protected static org.apache.avro.Schema createAvroSchema(SchemaDefinition schema try { // Disable validation of default values for compatibility validateDefaults.set(false); - return schemaDefinition.getAlwaysAllowNull() ? ReflectData.AllowNull.get().getSchema(pojo) - : ReflectData.get().getSchema(pojo); + return extractAvroSchema(schemaDefinition, pojo); } finally { validateDefaults.set(savedValidateDefaults); } @@ -160,6 +159,15 @@ protected static org.apache.avro.Schema createAvroSchema(SchemaDefinition schema } } + protected static Schema extractAvroSchema(SchemaDefinition schemaDefinition, Class pojo) { + 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); + } + } + protected static org.apache.avro.Schema parseAvroSchema(String schemaJson) { final Parser parser = new Parser(); parser.setValidateDefaults(false);