Skip to content

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

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

Closed
gavvvr opened this issue Sep 5, 2022 · 1 comment
Labels
status: ideal-for-contribution An issue that a contributor can help us with type: enhancement A general enhancement

Comments

@gavvvr
Copy link
Contributor

gavvvr commented Sep 5, 2022

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 5, 2022
@christophstrobl christophstrobl added status: ideal-for-contribution An issue that a contributor can help us with type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 5, 2022
@christophstrobl
Copy link
Member

@gavvvr thanks for pointing that out. If you have the time - a PR would be welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: ideal-for-contribution An issue that a contributor can help us with type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants