Skip to content

Fix Nullability Error #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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