Skip to content
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

When using Jackson as the default object mapping engine, updateMany() doesn't serialize objects properly #394

Open
rxsto opened this issue Feb 5, 2023 · 0 comments
Labels

Comments

@rxsto
Copy link

rxsto commented Feb 5, 2023

I am using the fun <T : Any> MongoCollection<T>.updateMany(filter: Bson, vararg updates: SetTo<*>, updateOptions: UpdateOptions = UpdateOptions()): UpdateResult method on version 4.8.0 to update multiple properties at once for many documents that match the filter's criteria.

The updateMany() call in itself works and continues to edit the correct documents in the Mongo cluster, but it avoids serializing values properly. In my example I am trying to update an enum on the entity, which has been set correctly when inserting earlier, but is now being updated to the non-serialized value.

collection
    .updateMany(
        Filters.and(
            Entity::id eq id,
            Entity::property eq property
        ),
        SetTo(
            Entity::name,
            newName
        ),
        SetTo(
            Entity::type,
            Type.NEW_TYPE
        )
    )

The enum Type is being serialized like the following:

enum class Type(@JsonValue val value: Int) {

    DEFAULT_TYPE(0),
    NEW_TYPE(1);

    companion object {
        @JvmStatic
        @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
        fun forType(value: Int): Type? {
            return Type.values().asSequence()
                .firstOrNull { type -> type.value == value }
        }
    }
}

In methods like replaceOne() custom-serializable values are properly edited. We're using the following in a for loop as a workaround:

collection
    .replaceOne(
        Entity::id eq entry.id,
        entry,
        ReplaceOptions().upsert(true)
    )

Is there something we are missing, or is this a bug? We would expect the updateMany() call to behave the same in regards to serialization.

~ Cheers

@zigzago zigzago added the bug label Apr 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants