Skip to content

Commit b0a79d5

Browse files
committed
Handle null value in AMQP 1.0 codecs
Fixes #206
1 parent c895ec9 commit b0a79d5

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

src/main/java/com/rabbitmq/stream/codec/QpidProtonCodec.java

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ private static Object convertApplicationProperty(Object value) {
9696
return ((Date) value).getTime();
9797
} else if (value instanceof Symbol) {
9898
return ((Symbol) value).toString();
99+
} else if (value == null) {
100+
return null;
99101
} else {
100102
throw new IllegalArgumentException(
101103
"Type not supported for an application property: " + value.getClass());
@@ -289,6 +291,8 @@ protected Object convertToQpidType(Object value) {
289291
return Symbol.getSymbol(value.toString());
290292
} else if (value instanceof byte[]) {
291293
return new Binary((byte[]) value);
294+
} else if (value == null) {
295+
return null;
292296
} else {
293297
throw new IllegalArgumentException(
294298
"Type not supported for an application property: " + value.getClass());

src/main/java/com/rabbitmq/stream/codec/SwiftMqCodec.java

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ private static Object convertAmqpMapValue(AMQPType value) {
6666
return ((AMQPUuid) value).getValue();
6767
} else if (value instanceof AMQPSymbol) {
6868
return ((AMQPSymbol) value).getValue();
69+
} else if (value instanceof AMQPNull) {
70+
return null;
71+
} else if (value == null) {
72+
return null;
6973
} else {
7074
throw new IllegalArgumentException(
7175
"Type not supported for an application property: " + value.getClass());
@@ -311,6 +315,8 @@ protected AMQPType convertToSwiftMqType(Object value) {
311315
return new AMQPSymbol(value.toString());
312316
} else if (value instanceof UUID) {
313317
return new AMQPUuid((UUID) value);
318+
} else if (value == value) {
319+
return AMQPNull.NULL;
314320
} else {
315321
throw new IllegalArgumentException(
316322
"Type not supported for an application property: " + value.getClass());

src/main/java/com/rabbitmq/stream/codec/SwiftMqMessageBuilder.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -301,19 +301,19 @@ protected void addEntryTimestamp(String key, long value) {
301301
}
302302

303303
protected void addEntry(String key, UUID value) {
304-
map.put(keyMaker.apply(key), new AMQPUuid(value));
304+
map.put(keyMaker.apply(key), value == null ? AMQPNull.NULL : new AMQPUuid(value));
305305
}
306306

307307
protected void addEntry(String key, byte[] value) {
308-
map.put(keyMaker.apply(key), new AMQPBinary(value));
308+
map.put(keyMaker.apply(key), value == null ? AMQPNull.NULL : new AMQPBinary(value));
309309
}
310310

311311
protected void addEntry(String key, String value) {
312-
map.put(keyMaker.apply(key), new AMQPString(value));
312+
map.put(keyMaker.apply(key), value == null ? AMQPNull.NULL : new AMQPString(value));
313313
}
314314

315315
protected void addEntrySymbol(String key, String value) {
316-
map.put(keyMaker.apply(key), new AMQPSymbol(value));
316+
map.put(keyMaker.apply(key), value == null ? AMQPNull.NULL : new AMQPSymbol(value));
317317
}
318318
}
319319

src/test/java/com/rabbitmq/stream/codec/CodecsTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ void codecs(CodecCouple codecCouple) {
212212
.entry("binary", binary)
213213
.entry("string", string)
214214
.entrySymbol("symbol", symbol)
215+
.entry("null", (String) null)
215216
.messageBuilder()
216217
.messageAnnotations()
217218
.entry("annotations.boolean", Boolean.FALSE)
@@ -235,6 +236,7 @@ void codecs(CodecCouple codecCouple) {
235236
.entry("annotations.binary", binary)
236237
.entry("annotations.string", string)
237238
.entrySymbol("annotations.symbol", symbol)
239+
.entry("annotations.null", (String) null)
238240
.messageBuilder()
239241
.build();
240242
Codec.EncodedMessage encoded = serializer.encode(outboundMessage);
@@ -356,6 +358,7 @@ void codecs(CodecCouple codecCouple) {
356358
.isNotNull()
357359
.isInstanceOf(String.class)
358360
.isEqualTo(symbol);
361+
assertThat(inboundMessage.getApplicationProperties().get("null")).isNull();
359362

360363
// message annotations
361364
assertThat(inboundMessage.getMessageAnnotations().get("annotations.boolean"))
@@ -453,6 +456,7 @@ void codecs(CodecCouple codecCouple) {
453456
.isNotNull()
454457
.isInstanceOf(String.class)
455458
.isEqualTo(symbol);
459+
assertThat(inboundMessage.getMessageAnnotations().get("annotations.null")).isNull();
456460
});
457461
}
458462

0 commit comments

Comments
 (0)