Skip to content

SchemaRegistrySerde: Avro deserialization via topic name #4521

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/e2e-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ jobs:
id: compose_app
# use the following command until #819 will be fixed
run: |
docker-compose -f kafka-ui-e2e-checks/docker/selenoid-git.yaml up -d
docker-compose -f ./documentation/compose/e2e-tests.yaml up -d && until [ "$(docker exec kafka-ui wget --spider --server-response http://localhost:8080/actuator/health 2>&1 | grep -c 'HTTP/1.1 200 OK')" == "1" ]; do echo "Waiting for kafka-ui ..." && sleep 1; done
docker compose -f kafka-ui-e2e-checks/docker/selenoid-git.yaml up -d
docker compose -f ./documentation/compose/e2e-tests.yaml up -d && until [ "$(docker exec kafka-ui wget --spider --server-response http://localhost:8080/actuator/health 2>&1 | grep -c 'HTTP/1.1 200 OK')" == "1" ]; do echo "Waiting for kafka-ui ..." && sleep 1; done
- name: Run test suite
run: |
./mvnw -B -ntp versions:set -DnewVersion=${{ github.event.pull_request.head.sha }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public Serializer serializer(String topic, Target type) {
@Override
public Deserializer deserializer(String topic, Target type) {
return (headers, data) -> {
var schemaId = extractSchemaIdFromMsg(data);
int schemaId = getSchemaIdFromMessageOrTopic(data, topic, type);
SchemaType format = getMessageFormatBySchemaId(schemaId);
MessageFormatter formatter = schemaRegistryFormatters.get(format);
return new DeserializeResult(
Expand All @@ -293,22 +293,30 @@ public Deserializer deserializer(String topic, Target type) {
};
}

private int getSchemaIdFromMessageOrTopic(byte[] data, String topic, Target type) {
return extractSchemaIdFromMsg(data).orElseGet(
() -> {
String subject = schemaSubject(topic, type);
return getSchemaBySubject(subject)
.map(SchemaMetadata::getId)
.orElseThrow(() -> new ValidationException(
String.format("No schema for subject '%s' found and no magic byte in avro data", subject)));
}
);
}

private SchemaType getMessageFormatBySchemaId(int schemaId) {
return getSchemaById(schemaId)
.map(ParsedSchema::schemaType)
.flatMap(SchemaType::fromString)
.orElseThrow(() -> new ValidationException(String.format("Schema for id '%d' not found ", schemaId)));
}

private int extractSchemaIdFromMsg(byte[] data) {
private Optional<Integer> extractSchemaIdFromMsg(byte[] data) {
ByteBuffer buffer = ByteBuffer.wrap(data);
if (buffer.remaining() >= SR_PAYLOAD_PREFIX_LENGTH && buffer.get() == SR_PAYLOAD_MAGIC_BYTE) {
return buffer.getInt();
return Optional.of(buffer.getInt());
}
throw new ValidationException(
String.format(
"Data doesn't contain magic byte and schema id prefix, so it can't be deserialized with %s serde",
name())
);
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,39 @@ void deserializeReturnsJsonAvroMsgJsonRepresentation() throws RestClientExceptio
.contains(Map.entry("schemaId", schemaId));
}

@Test
void deserializeReturnsJsonAvroMsgJsonRepresentationViaTopicNameOnly() throws RestClientException, IOException {
AvroSchema schema = new AvroSchema(
"{"
+ " \"type\": \"record\","
+ " \"name\": \"TestAvroRecord1\","
+ " \"fields\": ["
+ " {"
+ " \"name\": \"field1\","
+ " \"type\": \"string\""
+ " },"
+ " {"
+ " \"name\": \"field2\","
+ " \"type\": \"int\""
+ " }"
+ " ]"
+ "}"
);
String jsonValue = "{ \"field1\":\"testStr\", \"field2\": 123 }";

String topic = "test";
int schemaId = registryClient.register(topic + "-value", schema);

byte[] data = jsonToAvro(jsonValue, schema); // No magic byte no schema id registered
var result = serde.deserializer(topic, Serde.Target.VALUE).deserialize(null, data);

assertJsonsEqual(jsonValue, result.getResult());
assertThat(result.getType()).isEqualTo(DeserializeResult.Type.JSON);
assertThat(result.getAdditionalProperties())
.contains(Map.entry("type", "AVRO"))
.contains(Map.entry("schemaId", schemaId));
}

@Nested
class SerdeWithDisabledSubjectExistenceCheck {

Expand Down
Loading