Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2027,6 +2027,11 @@ remoteAddress, new String(commandGetSchema.getSchemaVersion()),
long requestId = commandGetSchema.getRequestId();
SchemaVersion schemaVersion = SchemaVersion.Latest;
if (commandGetSchema.hasSchemaVersion()) {
if (commandGetSchema.getSchemaVersion().length == 0) {
commandSender.sendGetSchemaErrorResponse(requestId, ServerError.IncompatibleSchema,
"Empty schema version");
return;
}
Comment on lines +2030 to +2034

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I debugged testAvroSchemaWithTcpLookup and testAvroSchemaWithHttpLookup, it never went here. Then I removed these lines, tests still passed.

schemaVersion = schemaService.versionFromBytes(commandGetSchema.getSchemaVersion());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
import static org.testng.Assert.fail;
import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;

import com.google.common.base.Throwables;
import lombok.EqualsAndHashCode;
import org.apache.avro.Schema.Parser;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Sets;

import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -44,7 +45,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.client.BKException;
Expand All @@ -59,7 +59,9 @@
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.api.SchemaSerializationException;
import org.apache.pulsar.client.api.SubscriptionInitialPosition;
import org.apache.pulsar.client.api.SubscriptionType;
import org.apache.pulsar.client.api.TypedMessageBuilder;
import org.apache.pulsar.client.api.schema.GenericRecord;
import org.apache.pulsar.client.api.schema.SchemaDefinition;
Expand Down Expand Up @@ -1112,4 +1114,72 @@ private void checkSchemaForAutoSchema(Message<GenericRecord> message) {
}
}

@Test
public void testAvroSchemaWithHttpLookup() throws Exception {
stopBroker();
isTcpLookup = false;
setup();
testEmptySchema();
}

@Test
public void testAvroSchemaWithTcpLookup() throws Exception {
Comment thread
Technoboy- marked this conversation as resolved.
stopBroker();
isTcpLookup = true;
setup();
testEmptySchema();
}

private void testEmptySchema() throws Exception {
final String namespace = "test-namespace-" + randomName(16);
String ns = PUBLIC_TENANT + "/" + namespace;
admin.namespaces().createNamespace(ns, Sets.newHashSet(CLUSTER_NAME));

final String autoProducerTopic = getTopicName(ns, "testEmptySchema");

@Cleanup
Consumer<User> consumer = pulsarClient
.newConsumer(Schema.AVRO(User.class))
.topic(autoProducerTopic)
.subscriptionType(SubscriptionType.Shared)
.subscriptionName("sub-1")
.subscribe();

@Cleanup
Producer<User> userProducer = pulsarClient
.newProducer(Schema.AVRO(User.class))
.topic(autoProducerTopic)
.enableBatching(false)
.create();

@Cleanup
Producer<byte[]> producer = pulsarClient
.newProducer()
.topic(autoProducerTopic)
.enableBatching(false)
.create();

User test = new User("test");
userProducer.send(test);
producer.send("test".getBytes(StandardCharsets.UTF_8));
Message<User> message1 = consumer.receive();
Assert.assertEquals(test, message1.getValue());
try {
Message<User> message2 = consumer.receive();
message2.getValue();
} catch (Throwable ex) {
Assert.assertTrue(Throwables.getRootCause(ex) instanceof SchemaSerializationException);
Assert.assertEquals(Throwables.getRootCause(ex).getMessage(),"Empty schema version");
}
}

@EqualsAndHashCode
static class User implements Serializable {
private String name;
public User() {}
public User(String name) {
this.name = name;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.SchemaSerializationException;
import org.apache.pulsar.common.api.proto.CommandGetTopicsOfNamespace.Mode;
import org.apache.pulsar.common.api.proto.CommandLookupTopicResponse;
import org.apache.pulsar.common.api.proto.CommandLookupTopicResponse.LookupType;
Expand Down Expand Up @@ -222,9 +223,12 @@ public CompletableFuture<Optional<SchemaInfo>> getSchema(TopicName topicName) {

@Override
public CompletableFuture<Optional<SchemaInfo>> getSchema(TopicName topicName, byte[] version) {
InetSocketAddress socketAddress = serviceNameResolver.resolveHost();
CompletableFuture<Optional<SchemaInfo>> schemaFuture = new CompletableFuture<>();

if (version != null && version.length == 0) {
schemaFuture.completeExceptionally(new SchemaSerializationException("Empty schema version"));
return schemaFuture;
}
InetSocketAddress socketAddress = serviceNameResolver.resolveHost();
client.getCnxPool().getConnection(socketAddress).thenAccept(clientCnx -> {
long requestId = client.newRequestId();
ByteBuf request = Commands.newGetSchema(requestId, topicName.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.commons.lang3.tuple.Pair;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.PulsarClientException.NotFoundException;
import org.apache.pulsar.client.api.SchemaSerializationException;
import org.apache.pulsar.client.impl.conf.ClientConfigurationData;
import org.apache.pulsar.client.impl.schema.SchemaInfoUtil;
import org.apache.pulsar.client.impl.schema.SchemaUtils;
Expand Down Expand Up @@ -159,6 +160,10 @@ public CompletableFuture<Optional<SchemaInfo>> getSchema(TopicName topicName, by
String schemaName = topicName.getSchemaName();
String path = String.format("admin/v2/schemas/%s/schema", schemaName);
if (version != null) {
if (version.length == 0) {
future.completeExceptionally(new SchemaSerializationException("Empty schema version"));
return future;
}
path = String.format("admin/v2/schemas/%s/schema/%s",
schemaName,
ByteBuffer.wrap(version).getLong());
Expand Down