#2199 - ScalarTypeWrapper doesn't handle "nullValue" correctly

Handles the case for ScalarTypeConverter with custom null value and binding the custom null value (via query parameter, CallableSql parameter etc)
This commit is contained in:
Robin Bygrave
2021-03-18 10:03:35 +13:00
parent 1837c3443d
commit 2e16f05a70
3 changed files with 33 additions and 8 deletions
@@ -177,9 +177,6 @@ public class ScalarTypeWrapper<B, S> implements ScalarType<B> {
@SuppressWarnings("unchecked")
public Object toJdbcType(Object value) {
Object sv = converter.unwrapValue((B) value);
if (sv == null) {
return nullValue;
}
return scalarType.toJdbcType(sv);
}
@@ -0,0 +1,29 @@
package io.ebeaninternal.server.type;
import org.junit.Test;
import org.tests.model.ivo.Oid;
import org.tests.model.ivo.converter.OidTypeConverter;
import static org.assertj.core.api.Assertions.assertThat;
@SuppressWarnings({"rawtypes", "unchecked"})
public class ScalarTypeWrapperOidTest {
private final OidTypeConverter oidTypeConverter = new OidTypeConverter();
private final ScalarTypeLong longType = new ScalarTypeLong();
private final ScalarTypeWrapper<Oid<?>,Long> wrapper = new ScalarTypeWrapper(Oid.class, longType, oidTypeConverter);
@Test
public void toJdbcType() {
assertThat(wrapper.toJdbcType(new Oid<String>(42))).isEqualTo(42L);
assertThat(wrapper.toJdbcType(new Oid<String>(98))).isEqualTo(98L);
}
@Test
public void toJdbcType_when_nullValue() {
assertThat(wrapper.toJdbcType(OidTypeConverter.NULL_VALUE)).isNull();
}
}
@@ -5,22 +5,21 @@ import org.tests.model.ivo.Oid;
public class OidTypeConverter implements ScalarTypeConverter<Oid<?>,Long> {
public static final Oid<?> NULL_VALUE = new Oid<>(0);
@Override
public Oid<?> getNullValue() {
return null;
return NULL_VALUE;
}
@Override
public Oid<?> wrapValue(Long scalarType) {
if (scalarType == null) {
return null;
}
return new Oid<>(scalarType);
}
@Override
public Long unwrapValue(Oid<?> beanType) {
if (beanType == null) {
if (NULL_VALUE.equals(beanType)) {
return null;
}
return beanType.getValue();