sksamuel / avro4s

Avro schema generation and serialization / deserialization for Scala
Apache License 2.0
719 stars 238 forks source link

issue with sealed trait support #769

Closed GijsbertvanVliet closed 1 year ago

GijsbertvanVliet commented 1 year ago

This is an issue regarding the scala3 version of avro4s, but I don't seem to be able to add the required label to this issue for some reason. I have an issue with the support for sealed traits in avro4s. The avro schema has the following datatype:

{
  "type": "enum",
  "name": "NotificationTypes",
  "symbols": [
    "Unknown",
    "None",
    "Email",
    "Sms"
  ],
  "default": "Unknown"
}

which is converted to the following sealed trait by the avrohugger plugin:

sealed trait NotificationTypes

object NotificationTypes{
  case object Unknown extends NotificationTypes
  case object None    extends NotificationTypes
  case object Email   extends NotificationTypes
  case object Sms     extends NotificationTypes
}

The SealedTraits decoder, however, seems to scramble these values. It looks like this has something to do with the fact that ctx.subtypes.sorted(SubtypeOrdering) returns elements in order Email, None, Sms, Unknown while schema.getEnumSymbols returns elements in order Unknown, None, Email, Sms.

altrack commented 1 year ago

Check the Enums docs. The solution for this are ordering annotations, though it maybe not supported by Avro hugger.

GijsbertvanVliet commented 1 year ago

Yes, Avro hugger unfortunately does not support this. The problem does not occur if the Schema object is generated from the case classes instead of generating it from the .avsc file. But even if the schema is generated from the .avsc file, is there a reason why case objects are not matched to the enum symbols purely by matching the names Unknown, None, Email and Sms?

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

ThijsBroersen commented 1 year ago

I think just issue should not have been closed. The issue still exists. I looked into SubtypeOrdering and it looks like it is sorting one the names of the types. I think this is wrong, as the AVRO spec explicitly states the order is based on the symbols position in the enum schema: https://avro.apache.org/docs/1.11.1/specification/#sort-order