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

#313 solved serializers casting problems using SpecificDatumWriter #315

Merged
merged 14 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -10,9 +10,12 @@
import java.io.IOException;

import lombok.extern.slf4j.Slf4j;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.specific.SpecificDatumWriter;
import org.apache.avro.specific.SpecificRecord;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.serialization.Serializer;

Expand All @@ -21,7 +24,12 @@ public class GenericAvroRecordBinarySerializer<T extends GenericRecord> implemen

@Override
public final byte[] serialize(final String s, final T data) {
final var writer = new SpecificDatumWriter<>(data.getSchema());
DatumWriter<T> writer = new GenericDatumWriter<>(data.getSchema());
if (data instanceof SpecificRecord) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing test

writer = new SpecificDatumWriter<>(data.getSchema());
} else {
writer = new GenericDatumWriter<>(data.getSchema());
}
var result = new byte[]{};
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
final var encoder = EncoderFactory.get().binaryEncoder(baos, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import java.io.IOException;

import lombok.extern.slf4j.Slf4j;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.Encoder;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.specific.SpecificDatumWriter;
import org.apache.avro.specific.SpecificRecord;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.serialization.Serializer;

Expand All @@ -23,7 +26,13 @@ public class GenericAvroRecordSerializer<T extends GenericRecord> implements Ser
@Override
public final byte[] serialize(final String topic, final T data) {

final var writer = new SpecificDatumWriter<>(data.getSchema());
DatumWriter<T> writer = new GenericDatumWriter<>(data.getSchema());
if (data instanceof SpecificRecord) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing test

writer = new SpecificDatumWriter<>(data.getSchema());
} else {
writer = new GenericDatumWriter<>(data.getSchema());
}

byte[] dataBytes = new byte[0];
final var stream = new ByteArrayOutputStream();
final Encoder jsonEncoder;
Expand Down