Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Doc] SchemaDefinition must be used to create Avro schema in a certain case #15655

Closed
1 task done
BewareMyPower opened this issue May 18, 2022 · 4 comments
Closed
1 task done
Assignees
Labels
doc-required Your PR changes impact docs and you will update later. Stale

Comments

@BewareMyPower
Copy link
Contributor

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:

@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class User {
    String name;
    int age;
}

We can create an producer with Avro schema like:

client.newProducer(Schema.AVRO(User.class))

However, if the class has a nullable field with a default value like:

    @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:

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:

    @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

  • I have researched my question.
@BewareMyPower
Copy link
Contributor Author

In addition, the description of Changes allowed column in Schema compatibility check strategy is wrong.

Optional fields, which are decorated with @AvroDefault annotation, can be added or deleted for all strategies other than ALWAYS_INCOMPATIBLE. The description "Modify optional fields" is confused and ambiguous and should be removed.

@github-actions
Copy link

The issue had no activity for 30 days, mark with Stale label.

@github-actions github-actions bot added the Stale label Jun 18, 2022
@momo-jun
Copy link
Contributor

momo-jun commented Nov 29, 2022

@congbobo184 @liangyepianzhou can you pls take a look at this issue of schema docs? I guess it hasn't been addressed. Any chance we can take care of it through this doc improvement PR?

@github-actions github-actions bot removed the Stale label Nov 30, 2022
@github-actions
Copy link

The issue had no activity for 30 days, mark with Stale label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-required Your PR changes impact docs and you will update later. Stale
Projects
None yet
Development

No branches or pull requests

3 participants