BewareMyPower opened a new issue, #15655:
URL: https://github.com/apache/pulsar/issues/15655

   ### What issue do you find in Pulsar docs?
   
   Currently Pulsar docs only shows a simple way to create Avro schema. See 
https://pulsar.apache.org/docs/next/schema-understand#usage.
   
   Given the following `User` class:
   
   ```java
   @Builder
   @AllArgsConstructor
   @NoArgsConstructor
   public static class User {
       String name;
       int age;
   }
   ```
   
   We can create an producer with Avro schema like:
   
   ```java
   client.newProducer(Schema.AVRO(User.class))
   ```
   
   However, if the class has a nullable field with a default value like:
   
   ```java
       @Builder
       @AllArgsConstructor
       @NoArgsConstructor
       public static class User {
           @AvroDefault("\"user\"")
           String name;
           int age;
       }
   ```
   
   We must create the Avro schema via a `SchemaDefinition` and disable the 
`alwaysAllowNull` field like:
   
   ```java
   SchemaDefinition schemaDefinition = 
SchemaDefinition.builder().withPojo(User.class).withAlwaysAllowNull(false).build();
   client.newProducer(Schema.AVRO(schemaDefinition))
   ```
   
   Otherwise, the schema info would be invalid. Run following test to reproduce:
   
   ```java
       @Test
       public void test() throws Exception {
           var topic = "test-topic";
           try (var client = 
PulsarClient.builder().serviceUrl(brokerUrl.toString()).build()) {
               client.newProducer(Schema.AVRO(User.class))
                       .topic(topic)
                       .create();
               client.newProducer(Schema.AVRO(User.class))
                       .topic(topic)
                       .create();
           }
       }
   ```
   
   The 2nd creation of the producer will fail with following error messages:
   
   ```
   2022-05-18T20:37:42,013 - ERROR - 
[mock-pulsar-bk-OrderedExecutor-0-0:ServerCnx@1292] - Try add schema failed, 
remote address /127.0.0.1:62344, topic persistent://public/default/test-topic, 
producerId 1
   java.util.concurrent.CompletionException: org.apache.avro.AvroTypeException: 
Invalid default for field name: "user" not a ["null","string"]
        at 
java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:315)
 ~[?:?]
           ...
   ```
   
   ### What is your suggestion?
   
   1. Add examples about how to create Avro schema with a `SchemaDefinition`.
   2. Note that this way must be used **when the class has a nullable field 
with a default value**.
   
   ### Do you have any references?
   
   https://pulsar.apache.org/docs/next/schema-understand#usage
   
   ### Would you like to fix this issue?
   
   Yes
   
   ### Note
   
   - [X] I have researched my question.


-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to