Skip to content

Commit

Permalink
Merge pull request #913 from jeffgbutler/null-fix
Browse files Browse the repository at this point in the history
Fix Nullability Error
  • Loading branch information
jeffgbutler authored Feb 22, 2025
2 parents 01cda3e + c263366 commit 84c8438
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ private ValueWhenPresentMapping(SqlColumn<T> column, Supplier<@Nullable T> value
}

public Optional<Object> value() {
return Optional.ofNullable(valueSupplier.get()).map(this::convert);
return Optional.ofNullable(valueSupplier.get()).flatMap(this::convert);
}

private @Nullable Object convert(@Nullable T value) {
return localColumn.convertParameterType(value);
private Optional<Object> convert(T value) {
return Optional.ofNullable(localColumn.convertParameterType(value));
}

@Override
Expand Down
9 changes: 7 additions & 2 deletions src/test/java/examples/spring/LastNameParameterConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
package examples.spring;

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import org.mybatis.dynamic.sql.ParameterTypeConverter;
import org.springframework.core.convert.converter.Converter;

@NullMarked
public class LastNameParameterConverter implements ParameterTypeConverter<LastName, String>,
Converter<LastName, String> {
@Override
public String convert(LastName source) {
return source.getName();
public @Nullable String convert(LastName source) {
if ("Slate".equals(source.getName())) {
return null;
} else {
return source.getName();
}
}
}
28 changes: 28 additions & 0 deletions src/test/java/examples/spring/PersonTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.mybatis.dynamic.sql.insert.GeneralInsertModel;
import org.mybatis.dynamic.sql.insert.InsertModel;
import org.mybatis.dynamic.sql.insert.MultiRowInsertModel;
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.util.Buildable;
Expand Down Expand Up @@ -379,6 +381,32 @@ void testInsertSelective() {
assertThat(rows).isEqualTo(1);
}

@Test
void testGeneralInsertWhenTypeConverterReturnsNull() {

GeneralInsertStatementProvider insertStatement = insertInto(person)
.set(id).toValue(100)
.set(firstName).toValue("Joe")
.set(lastName).toValueWhenPresent(LastName.of("Slate"))
.set(birthDate).toValue(new Date())
.set(employed).toValue(true)
.set(occupation).toValue("Quarry Owner")
.set(addressId).toValue(1)
.build()
.render(RenderingStrategies.SPRING_NAMED_PARAMETER);

assertThat(insertStatement.getInsertStatement())
.isEqualTo("insert into Person (id, first_name, birth_date, employed, occupation, address_id) values (:p1, :p2, :p3, :p4, :p5, :p6)");
int rows = template.generalInsert(insertStatement);
assertThat(rows).isEqualTo(1);

Buildable<SelectModel> selectStatement = select(id, firstName, lastName, birthDate, employed, occupation, addressId)
.from(person)
.where(id, isEqualTo(100));
Optional<PersonRecord> newRecord = template.selectOne(selectStatement, personRowMapper);
assertThat(newRecord).hasValueSatisfying(r -> assertThat(r.getLastName().getName()).isNull());
}

@Test
void testUpdateByPrimaryKey() {

Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/examples/simple/CreateSimpleDB.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ create table Address (
create table Person (
id int not null,
first_name varchar(30) not null,
last_name varchar(30) not null,
last_name varchar(30) null,
birth_date date not null,
employed varchar(3) not null,
occupation varchar(30) null,
Expand Down

0 comments on commit 84c8438

Please sign in to comment.