Skip to content

Commit aaf3bdc

Browse files
committed
null check missing for arrays of enums (#1505)
See #1470 Signed-off-by: Thomas Segismont <[email protected]>
1 parent b410327 commit aaf3bdc

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

vertx-pg-client/src/main/java/io/vertx/pgclient/impl/RowImpl.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,12 @@ private Interval[] getArrayOfIntervals(int pos) {
267267
return (Interval[]) getValue(pos);
268268
}
269269

270+
@SuppressWarnings({"unchecked", "rawtypes"})
270271
private Object[] getArrayOfEnums(Class enumType, int pos) {
271272
Object val = getValue(pos);
273+
if (val == null) {
274+
return null;
275+
}
272276
if (val instanceof String[]) {
273277
String[] array = (String[]) val;
274278
Object[] ret = (Object[]) Array.newInstance(enumType, array.length);
@@ -279,7 +283,8 @@ private Object[] getArrayOfEnums(Class enumType, int pos) {
279283
}
280284
}
281285
return ret;
282-
} else if (val instanceof Number[]) {
286+
}
287+
if (val instanceof Number[]) {
283288
Number[] array = (Number[]) val;
284289
Object[] ret = (Object[]) Array.newInstance(enumType, array.length);
285290
Object[] constants = enumType.getEnumConstants();
@@ -293,8 +298,7 @@ private Object[] getArrayOfEnums(Class enumType, int pos) {
293298
}
294299
}
295300
return ret;
296-
} else {
297-
throw new ClassCastException();
298301
}
302+
throw new ClassCastException();
299303
}
300304
}

vertx-pg-client/src/test/java/io/vertx/pgclient/data/JavaEnumTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ private void testJavaEnumToArrayColumn(TestContext ctx, String value, String sql
8181
}));
8282
}
8383

84+
@Test
85+
public void testJavaEnumToNullArrayColumn(TestContext ctx) {
86+
PgConnection.connect(vertx, options).onComplete(ctx.asyncAssertSuccess(conn -> {
87+
conn
88+
.preparedQuery("SELECT NULL")
89+
.execute()
90+
.onComplete(ctx.asyncAssertSuccess(v -> {
91+
RowIterator<Row> it = v.iterator();
92+
ctx.assertTrue(it.hasNext());
93+
Row row = it.next();
94+
Mood[] result = row.get(Mood[].class, 0);
95+
ctx.assertNull(result);
96+
}));
97+
}));
98+
}
99+
84100
@Test
85101
public void testJavaEnumToEnumParam(TestContext ctx) {
86102
testJavaEnumToParam(ctx, "happy", "Mood");

0 commit comments

Comments
 (0)