Skip to content

Possible regression in Criteria.and(String) behavior between Spring Data MongoDB 5.0.7 and 5.1.0 #5214

Description

@Alfagun74

Description

After upgrading from Spring Data MongoDB 5.0.7 to 5.1.0, we observed a behavior change when using org.springframework.data.mongodb.core.query.Criteria.

The following test passes with Spring Data MongoDB 5.0.7:

class CriteriaTest {

    @Test
    void test() {
        final Criteria criteria = Criteria.where("ID1").is("1");
        criteria.and("ID2").is("2");

        Query query = Query.query(criteria);

        Assertions.assertEquals(
                "Query: { \"ID1\" : \"1\", \"ID2\" : \"2\"}, Fields: {}, Sort: {}",
                query.toString());
    }
}

The resulting query is:

{ "ID1" : "1", "ID2" : "2" }

After upgrading to Spring Data MongoDB 5.1.0, the same test fails. The resulting query only contains the first criterion:

{ "ID1" : "1" }

Changing the code to use the returned Criteria instance restores the expected behavior:

Criteria criteria = Criteria.where("ID1").is("1");
criteria = criteria.and("ID2").is("2");

Impact

The observed behavior suggests that Criteria.and(String) no longer updates the original Criteria instance and instead only applies changes to the returned object.

Existing code continues to compile successfully, but query semantics may change at runtime after upgrading. This can be difficult to detect, especially in applications without dedicated tests covering the generated MongoDB queries.

We were unable to find any mention of this behavior change in the Spring Data MongoDB 5.1.0 release notes and therefore wanted to report it as a possible regression.

Environment

  • Spring Boot 4.0.7 / Spring Data MongoDB 5.0.7
  • Spring Boot 4.1.0 / Spring Data MongoDB 5.1.0
  • Java: 25

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions