Skip to content

Commit

Permalink
Merge pull request #2911 from confluentinc/fix-mock-client-npe
Browse files Browse the repository at this point in the history
Fix NPE for getAllVersionsById in MockSchemaRegistryClient
  • Loading branch information
rayokota authored Dec 19, 2023
2 parents b861ec6 + b32455e commit c62d8f9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,18 @@ public Collection<SubjectVersion> getAllVersionsById(int id) throws IOException,
RestClientException {
return idToSchemaCache.entrySet().stream()
.filter(entry -> entry.getValue().containsKey(id))
.map(e -> {
.flatMap(e -> {
ParsedSchema schema = e.getValue().get(id);
int version = schemaToVersionCache.get(e.getKey()).get(schema);
return new SubjectVersion(e.getKey(), version);
}).collect(Collectors.toList());
Map<ParsedSchema, Integer> schemaVersionMap = schemaToVersionCache.get(e.getKey());
if (schemaVersionMap != null) {
int version = schemaVersionMap.get(schema);
return Stream.of(new SubjectVersion(e.getKey(), version));
} else {
return Stream.empty();
}
})
.distinct()
.collect(Collectors.toList());
}

private int getLatestVersion(String subject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import io.confluent.kafka.schemaregistry.ClusterTestHarness;
import io.confluent.kafka.schemaregistry.avro.AvroSchema;
import io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider;
import io.confluent.kafka.schemaregistry.client.rest.entities.SubjectVersion;
import java.util.Collection;
import java.util.Collections;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
Expand Down Expand Up @@ -200,5 +202,16 @@ public void testRegisterAndGetId() throws Exception {
id = client.getId("test2", schema2);
assertEquals(2, id);
}

@Test
public void testGetAllVersionsById() throws Exception {
AvroSchema avroSchema = new AvroSchema("{\"type\":\"record\",\"name\":\"ts1\","
+ "\"fields\":[{\"name\": \"fld1\",\"type\": \"int\"}]}");
MockSchemaRegistryClient client =
new MockSchemaRegistryClient(Collections.singletonList(new AvroSchemaProvider()));
int id = client.register("test-value", avroSchema);
Collection<SubjectVersion> versions = client.getAllVersionsById(id);
assertEquals(new SubjectVersion("test-value", 1), versions.iterator().next());
}
}

0 comments on commit c62d8f9

Please sign in to comment.