Skip to content

Commit 2e32608

Browse files
committed
Allow having a serializer with Memory<byte>
1 parent 908f179 commit 2e32608

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/Confluent.Kafka/Producer.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -506,21 +506,21 @@ private void InitializeSerializers(
506506
IAsyncSerializer<TValue> asyncValueSerializer)
507507
{
508508
// setup key serializer.
509-
if (typeof(TKey) == typeof(Memory<byte>) || typeof(TKey) == typeof(ReadOnlyMemory<byte>))
509+
if (keySerializer == null && asyncKeySerializer == null)
510510
{
511-
if (keySerializer != null || asyncKeySerializer != null)
511+
if (defaultSerializers.TryGetValue(typeof(TKey), out object serializer))
512512
{
513-
throw new ArgumentNullException(null, "Key serializer should not be specified for Memory<byte>");
513+
this.keySerializer = (ISerializer<TKey>)serializer;
514514
}
515-
}
516-
else if (keySerializer == null && asyncKeySerializer == null)
517-
{
518-
if (!defaultSerializers.TryGetValue(typeof(TKey), out object serializer))
515+
else if (typeof(TValue) == typeof(Memory<byte>) || typeof(TValue) == typeof(ReadOnlyMemory<byte>))
516+
{
517+
// Serializers are not used for Memory<byte>.
518+
}
519+
else
519520
{
520521
throw new ArgumentNullException(
521522
$"Key serializer not specified and there is no default serializer defined for type {typeof(TKey).Name}.");
522523
}
523-
this.keySerializer = (ISerializer<TKey>)serializer;
524524
}
525525
else if (keySerializer == null && asyncKeySerializer != null)
526526
{
@@ -536,21 +536,22 @@ private void InitializeSerializers(
536536
}
537537

538538
// setup value serializer.
539-
if (typeof(TValue) == typeof(Memory<byte>) || typeof(TValue) == typeof(ReadOnlyMemory<byte>))
539+
if (valueSerializer == null && asyncValueSerializer == null)
540540
{
541-
if (valueSerializer != null || asyncValueSerializer != null)
541+
if (defaultSerializers.TryGetValue(typeof(TValue), out object serializer))
542542
{
543-
throw new ArgumentNullException(null, "Value serializer should not be specified for Memory<byte>");
543+
this.valueSerializer = (ISerializer<TValue>)serializer;
544544
}
545-
}
546-
else if (valueSerializer == null && asyncValueSerializer == null)
547-
{
548-
if (!defaultSerializers.TryGetValue(typeof(TValue), out object serializer))
545+
else if (typeof(TValue) == typeof(Memory<byte>) || typeof(TValue) == typeof(ReadOnlyMemory<byte>))
546+
{
547+
// Serializers are not used for Memory<byte>.
548+
}
549+
else
549550
{
550551
throw new ArgumentNullException(
551552
$"Value serializer not specified and there is no default serializer defined for type {typeof(TValue).Name}.");
552553
}
553-
this.valueSerializer = (ISerializer<TValue>)serializer;
554+
554555
}
555556
else if (valueSerializer == null && asyncValueSerializer != null)
556557
{

0 commit comments

Comments
 (0)