Skip to content

Commit 84c8438

Browse files
authored
Merge pull request #913 from jeffgbutler/null-fix
Fix Nullability Error
2 parents 01cda3e + c263366 commit 84c8438

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

src/main/java/org/mybatis/dynamic/sql/util/ValueWhenPresentMapping.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ private ValueWhenPresentMapping(SqlColumn<T> column, Supplier<@Nullable T> value
3535
}
3636

3737
public Optional<Object> value() {
38-
return Optional.ofNullable(valueSupplier.get()).map(this::convert);
38+
return Optional.ofNullable(valueSupplier.get()).flatMap(this::convert);
3939
}
4040

41-
private @Nullable Object convert(@Nullable T value) {
42-
return localColumn.convertParameterType(value);
41+
private Optional<Object> convert(T value) {
42+
return Optional.ofNullable(localColumn.convertParameterType(value));
4343
}
4444

4545
@Override

src/test/java/examples/spring/LastNameParameterConverter.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@
1616
package examples.spring;
1717

1818
import org.jspecify.annotations.NullMarked;
19+
import org.jspecify.annotations.Nullable;
1920
import org.mybatis.dynamic.sql.ParameterTypeConverter;
2021
import org.springframework.core.convert.converter.Converter;
2122

2223
@NullMarked
2324
public class LastNameParameterConverter implements ParameterTypeConverter<LastName, String>,
2425
Converter<LastName, String> {
2526
@Override
26-
public String convert(LastName source) {
27-
return source.getName();
27+
public @Nullable String convert(LastName source) {
28+
if ("Slate".equals(source.getName())) {
29+
return null;
30+
} else {
31+
return source.getName();
32+
}
2833
}
2934
}

src/test/java/examples/spring/PersonTemplateTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.mybatis.dynamic.sql.insert.GeneralInsertModel;
3232
import org.mybatis.dynamic.sql.insert.InsertModel;
3333
import org.mybatis.dynamic.sql.insert.MultiRowInsertModel;
34+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
35+
import org.mybatis.dynamic.sql.render.RenderingStrategies;
3436
import org.mybatis.dynamic.sql.select.SelectModel;
3537
import org.mybatis.dynamic.sql.update.UpdateModel;
3638
import org.mybatis.dynamic.sql.util.Buildable;
@@ -379,6 +381,32 @@ void testInsertSelective() {
379381
assertThat(rows).isEqualTo(1);
380382
}
381383

384+
@Test
385+
void testGeneralInsertWhenTypeConverterReturnsNull() {
386+
387+
GeneralInsertStatementProvider insertStatement = insertInto(person)
388+
.set(id).toValue(100)
389+
.set(firstName).toValue("Joe")
390+
.set(lastName).toValueWhenPresent(LastName.of("Slate"))
391+
.set(birthDate).toValue(new Date())
392+
.set(employed).toValue(true)
393+
.set(occupation).toValue("Quarry Owner")
394+
.set(addressId).toValue(1)
395+
.build()
396+
.render(RenderingStrategies.SPRING_NAMED_PARAMETER);
397+
398+
assertThat(insertStatement.getInsertStatement())
399+
.isEqualTo("insert into Person (id, first_name, birth_date, employed, occupation, address_id) values (:p1, :p2, :p3, :p4, :p5, :p6)");
400+
int rows = template.generalInsert(insertStatement);
401+
assertThat(rows).isEqualTo(1);
402+
403+
Buildable<SelectModel> selectStatement = select(id, firstName, lastName, birthDate, employed, occupation, addressId)
404+
.from(person)
405+
.where(id, isEqualTo(100));
406+
Optional<PersonRecord> newRecord = template.selectOne(selectStatement, personRowMapper);
407+
assertThat(newRecord).hasValueSatisfying(r -> assertThat(r.getLastName().getName()).isNull());
408+
}
409+
382410
@Test
383411
void testUpdateByPrimaryKey() {
384412

src/test/resources/examples/simple/CreateSimpleDB.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ create table Address (
3030
create table Person (
3131
id int not null,
3232
first_name varchar(30) not null,
33-
last_name varchar(30) not null,
33+
last_name varchar(30) null,
3434
birth_date date not null,
3535
employed varchar(3) not null,
3636
occupation varchar(30) null,

0 commit comments

Comments
 (0)