Skip to content

Switch MongoOperations.updateMulti Kotlin extension function to accept a more generic UpdateDefinition #4153

Closed
@gavvvr

Description

@gavvvr

Today I was surprised, that I can't express the same update with Kotlin, which I can express with Java's MongoOperations API. My code was

mongoTemplate.updateMulti(
    Query.query(Criteria.where("someField").type(JsonSchemaObject.Type.DOUBLE)),
    AggregationUpdate.update()
        .set(...),
  MyEnt::class.java
)

Usually Kotlin extension allows me to write the same code, but without ugly ::class.java. So I would expect the following to work:

mongoTemplate.updateMulti<MyEnt>(
    Query.query(Criteria.where("someField").type(JsonSchemaObject.Type.DOUBLE)),
    AggregationUpdate.update()
        .set(...)
)

but it does not compile.

The reason is that the signature of Kotlin extension for MongoOperations is different from the one defined on the original MongoOperations interface method:

inline fun <reified T : Any> MongoOperations.updateMulti(query: Query, update: Update, collectionName: String? = null): UpdateResult =
if (collectionName != null) updateMulti(query, update, T::class.java, collectionName)
else updateMulti(query, update, T::class.java)

UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass);

The Kotlin extension accepts a more narrow type org.springframework.data.mongodb.core.query.Update type, while the Java interface accepts a more generic org.springframework.data.mongodb.core.query.UpdateDefinition.

By looking at the implementation of the Kotlin extension, I see not reasons for it to accept a different type. So, I think the type accepted by Kotlin extension function should be aligned with the one defined on the original interface it extends.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions