Apicurio / apicurio-registry

An API/Schema registry - stores APIs and Schemas.
https://www.apicur.io/registry/
Apache License 2.0
581 stars 260 forks source link

apicurio.registry.headers.enabled=false causes NullPointerException #2663

Closed merlor closed 2 years ago

merlor commented 2 years ago

I'm using apicurio 2.2.5.Final with spring cloud stream kafka streams and avroSerdes, I have to read/write the artifact identifier from/to payload instead of headers so I have added this configuration to my props file:

spring.cloud.stream.kafka.streams.binder.configuration.apicurio.registry.headers.enabled=false

But after this addition every message received cause:

java.lang.NullPointerException: Cannot invoke "io.apicurio.registry.serde.headers.HeadersHandler.readHeaders(org.apache.kafka.common.header.Headers)" because "this.headersHandler" is null

The problem is in the method deserialize(String topic, Headers headers, byte[] data) inside the class io.apicurio.registry.serde.AbstractKafkaDeserializer; this is the code:

    public U deserialize(String topic, Headers headers, byte[] data) {
        if (data == null) {
            return null;
        }
        ArtifactReference artifactReference = null;
        if (headers != null) {                                            <-- headers isn't null, It's an object with an empty array
            artifactReference = headersHandler.readHeaders(headers);      <-- headersHandler is null and causes the NPE

            if (artifactReference.hasValue()) {
                return readData(topic, headers, data, artifactReference);
            }
        }
        if (data[0] == MAGIC_BYTE) {
            return deserialize(topic, data);
        } else if (headers == null){
            throw new IllegalStateException("Headers cannot be null");
        } else {
            //try to read data even if artifactReference has no value, maybe there is a fallbackArtifactProvider configured
            return readData(topic, headers, data, artifactReference);
        }
    }

There is no headers in the message but the condition (headers != null) is true (because the variable is valorized with an empty array) and the call headersHandler.readHeaders(headers) with headersHandler null causes a NPE.

Am I doing all right or there are other configurations to add?

A possible solution could be change headers != null into headers != null && headers.toArray().size() > 0 or check if headersHandler is not null

ajborley commented 2 years ago

I think this is the same as https://github.com/Apicurio/apicurio-registry/issues/2641 and should be fixed in the next release via this PR: https://github.com/Apicurio/apicurio-registry/pull/2642

merlor commented 2 years ago

@ajborley It's exactly the same issue (my bad, I didn't see it). You can close, thank you so much for the fast response.