spring-projects / spring-data-relational

Spring Data Relational. Home of Spring Data JDBC and Spring Data R2DBC.
https://spring.io/projects/spring-data-jdbc
Apache License 2.0
737 stars 339 forks source link

Mapping empty postgres text[] column sets field to null in 3.3.1 #1826

Closed Tree4Free closed 1 week ago

Tree4Free commented 2 weeks ago

As written in this comment https://github.com/spring-projects/spring-data-relational/issues/1737#issuecomment-2189169423 after upgrading to spring-data-jdbc 3.3.1 when I load an entity with a text[] column (mapped as List<String>) that is empty using findById the column is set to null.

My guess is that bug was introduced by this issue https://github.com/spring-projects/spring-data-relational/issues/1737 but I didn't look into the changes to confirm that.


An example of my setup taken from the comment linked above:

Using a combination of Java + Kotlin: My Entity: (Java + Lombok)

@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class MyTable extends Entity {
    List<String> tags;
}

My database table (postgres):

create table my_table
(
    id uuid not null,
    version bigint not null,
    tags text[] not null,
    constraint pk_my_table primary key (id)
);

My test case (Kotlin)

@Repository
interface MyTableRepository: CrudRepository<MyTable, UUID>, PagingAndSortingRepository<MyTable, UUID>

@Test
fun test() {
    val instance = myTableRepository.save(MyTable(null, 0, emptyList())) // instance.tags is still an empty list
    assertNotNull(myTableRepository.findById(instance.id).get().tags) // data from db tags is null
}
schauder commented 2 weeks ago

I tried to reproduce the issue but failed

Please provide a Minimimal Reproducable Example, preferable as a Github repository. Make sure to include the database, either as an in memory database or if that is not possible using Testcontainers.

Tree4Free commented 2 weeks ago

Here is the repo:

https://github.com/Tree4Free/spring-data-jdbc-issue-1826

I used a postgres testcontainer because thats where I also discovered the issue in my code. You can find the failing test under com.example.springjdbcnullissue.SpringJdbcNullIssueApplicationTests#testNullTextArray

(JDK used is 21)

When changing the version of the org.springframework.boot plugin in the build.gradle.kts to 3.3.0 from 3.3.1 the test case succeeds.