From 541ca43120e6e942f874550daa568e582c1dbcd6 Mon Sep 17 00:00:00 2001 From: Thomas Segismont Date: Fri, 17 Jan 2025 18:34:08 +0100 Subject: [PATCH] PgClient: NPE when inserting null param with default param extractor (#1486) (#1487) See #1477 This is a regression introduced in #1464 When the value is null, the extractor throws a failure. Then, in ErrorMessageFactory.buildWhenArgumentsTypeNotMatched, a NPE is thrown because the value of the class cannot be determined. Signed-off-by: Thomas Segismont --- .../io/vertx/pgclient/impl/codec/DataType.java | 5 ++++- .../data/PreparedStatementParamCoercionTest.java | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/codec/DataType.java b/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/codec/DataType.java index e81c014de..707d093ef 100644 --- a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/codec/DataType.java +++ b/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/codec/DataType.java @@ -237,7 +237,10 @@ private static class DefaultParamExtractor implements ParamExtractor { @Override public T get(TupleInternal tuple, int idx) { Object value = tuple.getValue(idx); - if (value != null && encodingType.isAssignableFrom(value.getClass())) { + if (value == null) { + return null; + } + if (encodingType.isAssignableFrom(value.getClass())) { return encodingType.cast(value); } throw FAILURE; diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/data/PreparedStatementParamCoercionTest.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/data/PreparedStatementParamCoercionTest.java index bc2db2152..0288af029 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/data/PreparedStatementParamCoercionTest.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/data/PreparedStatementParamCoercionTest.java @@ -1,9 +1,9 @@ package io.vertx.pgclient.data; -import io.vertx.pgclient.PgConnection; -import io.vertx.sqlclient.Tuple; import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; +import io.vertx.pgclient.PgConnection; +import io.vertx.sqlclient.Tuple; import io.vertx.sqlclient.data.Numeric; import org.junit.Test; @@ -64,4 +64,16 @@ public void testCoercionError(TestContext ctx) { })); })); } + + @Test + public void testNoCoercionErrorWithNull(TestContext ctx) { + PgConnection.connect(vertx, options).onComplete(ctx.asyncAssertSuccess(conn -> { + conn.prepare("SELECT $1::POINT").onComplete(ctx.asyncAssertSuccess(pq -> { + pq + .query() + .execute(Tuple.of(null)) + .onComplete(ctx.asyncAssertSuccess()); + })); + })); + } }