codelipenghui edited a comment on issue #4271: Pulsar Avro Schema Enum 
improvement request
URL: https://github.com/apache/pulsar/issues/4271#issuecomment-492986438
 
 
   @moonboots300 Sorry, I didn't fully understand your question. I wrote a 
test, hope can help you.
   ```java
   package org.apache.pulsar;
   
   import org.apache.avro.reflect.Nullable;
   import org.apache.pulsar.broker.service.schema.AvroSchemaCompatibilityCheck;
   import org.apache.pulsar.broker.service.schema.SchemaCompatibilityCheck;
   import org.apache.pulsar.broker.service.schema.SchemaCompatibilityStrategy;
   import org.apache.pulsar.client.api.schema.SchemaDefinition;
   import org.apache.pulsar.client.impl.schema.AvroSchema;
   import org.apache.pulsar.common.schema.SchemaData;
   import org.apache.pulsar.common.schema.SchemaType;
   import org.testng.Assert;
   import org.testng.annotations.Test;
   
   import java.util.Objects;
   
   public class AvroTest {
   
       private SchemaCompatibilityCheck checker = new 
AvroSchemaCompatibilityCheck();
   
       @Test
       public void test() {
   
           // generic schema via pojo
           AvroSchema<Example> schema1 = 
AvroSchema.of(SchemaDefinition.<Example>builder().withPojo(Example.class)
                   .withAlwaysAllowNull(false).build());
   
           // generic schema one field nullable
           AvroSchema<Example> schema2 = 
AvroSchema.of(SchemaDefinition.<Example>builder().withJsonDef("{\"type\":\"record\",\"name\":\"Example\",\"namespace\":\"org.apache.pulsar.AvroTest$\",\"fields\":[{\"name\":\"enumA\",\"type\":[\"null\",{\"type\":\"enum\",\"name\":\"ExampleEnumA\",\"symbols\":[\"YES\",\"NO\"]}],\"default\":null},{\"name\":\"enumB\",\"type\":[\"null\",{\"type\":\"enum\",\"name\":\"ExampleEnumB\",\"symbols\":[\"UP\",\"DOWN\"]}]}]}").withAlwaysAllowNull(false).build());
   
           Example example = new Example();
   
           byte[] bytes = schema2.encode(example);
           Example decoded = schema2.decode(bytes);
           Assert.assertEquals(example, decoded);
   
           example.setEnumA(ExampleEnumA.NO);
           example.setEnumB(ExampleEnumB.DOWN);
           bytes = schema1.encode(example);
           decoded = schema1.decode(bytes);
           Assert.assertEquals(example, decoded);
   
           Assert.assertTrue(checker.isCompatible(
                   getSchemaData(new 
String(schema1.getSchemaInfo().getSchema())),
                   getSchemaData(new 
String(schema2.getSchemaInfo().getSchema())),
                   SchemaCompatibilityStrategy.BACKWARD)
           );
       }
   
       static class Example {
   
           private ExampleEnumA enumA;
   
           private ExampleEnumB enumB;
   
           public ExampleEnumA getEnumA() {
               return enumA;
           }
   
           public void setEnumA(ExampleEnumA enumA) {
               this.enumA = enumA;
           }
   
           public ExampleEnumB getEnumB() {
               return enumB;
           }
   
           public void setEnumB(ExampleEnumB enumB) {
               this.enumB = enumB;
           }
   
           @Override
           public boolean equals(Object o) {
               if (this == o) return true;
               if (o == null || getClass() != o.getClass()) return false;
               Example example = (Example) o;
               return enumA == example.enumA &&
                       enumB == example.enumB;
           }
   
           @Override
           public int hashCode() {
               return Objects.hash(enumA, enumB);
           }
       }
   
       enum  ExampleEnumA {
           YES,
           NO
       }
   
       enum  ExampleEnumB {
           UP,
           DOWN
       }
   
       private static SchemaData getSchemaData(String schemaJson) {
           return 
SchemaData.builder().data(schemaJson.getBytes()).type(SchemaType.AVRO).build();
       }
   }
   
   
   ```
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to