Registring two JsonFormatters for a case class using generic types

Hi all,

i’m having trouble configuring the JsonSerializerRegistry in Lagom Scala 1.4.5.
I build a generic CrudEntity extending PersistentEntity. This crud entity is in charge of accepting CrudCommands, emitting CrudEvents and managing the current CrudState.

The developper using this abstraction as to provide onPrivateCommand and onPrivateEvent methods that work on the domain specific private payload.

class CrudEntity[
    PrivateCommand <: CrudPrivateCommand,
    PrivateState <: CrudPrivateState,
    PrivateEvent <: CrudPrivateEvent
](
    onPrivateCommand: (PrivateCommand, Option[PrivateState]) => Try[Seq[PrivateEvent]],
    onPrivateEvent: (PrivateEvent, Option[PrivateState]) => Try[PrivateState]
) extends PersistentEntity

Here is an example of the create command handled by the CrudEntity.

sealed trait CrudPrivateEntityCommand[EntityId, PrivateState <: CrudPrivateState]
  extends ReplyType[Try[CrudState[EntityId, PrivateState]]]

//Create
case class CreatePrivateEntityCommand[
    EntityId,
    PrivateCommand <: CrudPrivateCommand,
    PrivateState <: CrudPrivateState
](
    id: EntityId,
    user: User,
    command: PrivateCommand
) extends CrudPrivateEntityCommand[EntityId, PrivateState]

Here is an example of the created event emitted by the CrudEntity.

trait CrudPrivateEntityEvent[EntityId, PrivateEvent <: CrudPrivateEvent]
  extends AbstractEvent[CrudPrivateEntityEvent[EntityId, PrivateEvent]]

case class PrivateEntityCreatedEvent[EntityId: ClassTag, PrivateEvent <: CrudPrivateEvent: ClassTag](
    eventId: EventId,
    correlationId: CorrelationId,
    entityId: EntityId,
    privateEvent: PrivateEvent,
    createdAt: Instant,
    user: User
) extends CrudPrivateEntityEvent[EntityId, PrivateEvent]

object PrivateEntityCreatedEvent {
  implicit def format[EntityId: ClassTag, PrivateEvent <: CrudPrivateEvent: ClassTag](
      implicit fmtEntityId: Format[EntityId],
      fmtPrivateEvent: OFormat[PrivateEvent]
  ): OFormat[PrivateEntityCreatedEvent[EntityId, PrivateEvent]] = ???
}

My issue is that when registering the PrivateEntityCreatedEvent for two different CrudEntity, the second formatter is used for all the PrivateEntityCreatedEvents.
I try using ClassTag but without success.

 override def serializers: immutable.Seq[JsonSerializer[_]] = Vector(
       ...,
      JsonSerializer[EntityId],
      JsonSerializer[PrivateEvent1],
      JsonSerializer[PrivateEvent2],
      JsonSerializer(PrivateEntityCreatedEvent.format[EntityId, PrivateEvent1]),
      JsonSerializer(PrivateEntityCreatedEvent.format[EntityId, PrivateEvent2]),
      ...
    )

Similar issue here https://stackoverflow.com/q/44314942
No real solution I’m afraid.