Skip to content

Commit 80af49e

Browse files
Polish StringOrBytesSerializer
* Use `Assertions#assertThatIllegalStateException` Signed-off-by: Tran Ngoc Nhan <[email protected]>
1 parent fd0b0db commit 80af49e

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

spring-kafka/src/main/java/org/springframework/kafka/support/serializer/StringOrBytesSerializer.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* Convenient when used with one of the Json message converters.
2828
*
2929
* @author Gary Russell
30+
* @author Ngoc Nhan
3031
* @since 2.3
3132
*
3233
*/
@@ -42,21 +43,23 @@ public void configure(Map<String, ?> configs, boolean isKey) {
4243
@SuppressWarnings("NullAway") // Dataflow analysis limitation
4344
@Override
4445
public byte[] serialize(String topic, Object data) {
45-
if (data instanceof byte[]) {
46-
return (byte[]) data;
47-
}
48-
else if (data instanceof Bytes) {
49-
return ((Bytes) data).get();
46+
if (data == null) {
47+
return null;
5048
}
51-
else if (data instanceof String) {
52-
return this.stringSerializer.serialize(topic, (String) data);
49+
50+
if (data instanceof byte[] bytes) {
51+
return bytes;
5352
}
54-
else if (data == null) {
55-
return null;
53+
54+
if (data instanceof Bytes bytes) {
55+
return bytes.get();
5656
}
57-
else {
58-
throw new IllegalStateException("This serializer can only handle byte[], Bytes or String values");
57+
58+
if (data instanceof String string) {
59+
return this.stringSerializer.serialize(topic, string);
5960
}
61+
62+
throw new IllegalStateException("This serializer can only handle byte[], Bytes or String values");
6063
}
6164

6265
@Override

spring-kafka/src/test/java/org/springframework/kafka/support/serializer/StringOrBytesSerializerTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2024 the original author or authors.
2+
* Copyright 2019-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,10 +26,12 @@
2626
import org.springframework.kafka.test.utils.KafkaTestUtils;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2930

3031
/**
3132
* @author Gary Russell
3233
* @author Soby Chacko
34+
* @author Ngoc Nhan
3335
* @since 2.3
3436
*
3537
*/
@@ -51,6 +53,9 @@ void test() {
5153
Map<String, Object> configs = Collections.singletonMap("serializer.encoding", "UTF-16");
5254
serializer.configure(configs, false);
5355
assertThat(KafkaTestUtils.getPropertyValue(serializer, "stringSerializer.encoding")).isEqualTo(StandardCharsets.UTF_16);
56+
assertThat(serializer.serialize("null", null)).isNull();
57+
assertThatIllegalStateException().isThrownBy(() -> serializer.serialize("ex", 0))
58+
.withMessage("This serializer can only handle byte[], Bytes or String values");
5459
}
5560

5661
}

0 commit comments

Comments
 (0)