Skip to content

Commit 54f164b

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

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
@@ -264,8 +264,12 @@ private Interval[] getArrayOfIntervals(int pos) {
264264
return (Interval[]) getValue(pos);
265265
}
266266

267+
@SuppressWarnings({"unchecked", "rawtypes"})
267268
private Object[] getArrayOfEnums(Class enumType, int pos) {
268269
Object val = getValue(pos);
270+
if (val == null) {
271+
return null;
272+
}
269273
if (val instanceof String[]) {
270274
String[] array = (String[]) val;
271275
Object[] ret = (Object[]) Array.newInstance(enumType, array.length);
@@ -276,7 +280,8 @@ private Object[] getArrayOfEnums(Class enumType, int pos) {
276280
}
277281
}
278282
return ret;
279-
} else if (val instanceof Number[]) {
283+
}
284+
if (val instanceof Number[]) {
280285
Number[] array = (Number[]) val;
281286
Object[] ret = (Object[]) Array.newInstance(enumType, array.length);
282287
Object[] constants = enumType.getEnumConstants();
@@ -290,8 +295,7 @@ private Object[] getArrayOfEnums(Class enumType, int pos) {
290295
}
291296
}
292297
return ret;
293-
} else {
294-
throw new ClassCastException();
295298
}
299+
throw new ClassCastException();
296300
}
297301
}

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

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

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

0 commit comments

Comments
 (0)