kmongo icon indicating copy to clipboard operation
kmongo copied to clipboard

Custom Id<*> with UUID implementation not supported (Jackson)

Open rvplauborg opened this issue 3 years ago • 3 comments

Hi,

I cannot get a proper setup working using UUID for entity ids with KMongo. Here are some relevant snippets from my setup:

abstract class DomainEntity<T : DomainEntity<T>> {
    @get:JsonProperty("_id")
    abstract val id: EntityId<T>
    ...
}

class SomeEntity(override val id: EntityId<SomeEntity>) : DomainEntity<SomeEntity>()

@JvmInline
value class EntityId<T : DomainEntity<T>>(val value: UUID) {
    init {
        validate(this) {
            validate(EntityId<T>::value).isNotEqualTo(emptyUUID)
        }
    }

    override fun toString(): String {
        return value.toString()
    }
}
KMongo.createClient(
        MongoClientSettings.builder()
            .uuidRepresentation(UuidRepresentation.STANDARD)
            .applyConnectionString(ConnectionString(mongoDBContainer.replicaSetUrl))
            .build()
    )

I can create an entity, but id is not on the specified standard representation: BinData(3, "DEvqK/vTeQUtpMdfG8LrmQ==").

But other than creating a new entity, operations such as mongoCollection.deleteOneById(clientSession, entity.id) or other operations (find etc.) fail with com.fasterxml.jackson.databind.JsonMappingException: unsupported id type 7e4e1aa4-4a53-482a-bb43-91ac7588740b (example UUID).

Am I doing it wrong or is there no support for UUID entity ids - and how come my uuidRepresentation configuration does not work (i.e. the id is still using version 3 as shown above)?

rvplauborg avatar Dec 09 '21 18:12 rvplauborg

You are right, UUID is not supported for now as id type

zigzago avatar Dec 19 '21 12:12 zigzago

The way I fixed the issue that it was still using version 3 instead of standard was by also setting the following: KMongoJacksonFeature.setUUIDRepresentation(UuidRepresentation.STANDARD)

jurredejongh avatar Apr 05 '22 13:04 jurredejongh

I change the title of the issue. What is not supported is Id<*> implementation with UUID (related to "unsupported id type" error message). Direct UUID _id type declaration is supported.

zigzago avatar Apr 08 '22 22:04 zigzago