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

Union type json readers should check discriminator #911

Open
mbryzek opened this issue Jan 20, 2023 · 1 comment
Open

Union type json readers should check discriminator #911

mbryzek opened this issue Jan 20, 2023 · 1 comment
Labels

Comments

@mbryzek
Copy link
Collaborator

mbryzek commented Jan 20, 2023

No description provided.

@jackl
Copy link
Contributor

jackl commented Jan 20, 2023

One possibility for a fix is to check for the discriminator, for example change

    implicit def jsonReadsBillingExternalEventTransactionDeleted: play.api.libs.json.Reads[TransactionDeleted] = {
      for {
        eventId <- (__ \ "event_id").read[String]
        timestamp <- (__ \ "timestamp").read[_root_.org.joda.time.DateTime]
        organization <- (__ \ "organization").read[String]
        transaction <- (__ \ "transaction").read[io.flow.billing.v0.models.Transaction]
      } yield TransactionDeleted(eventId, timestamp, organization, transaction)
    }

to

    implicit def jsonReadsBillingExternalEventTransactionDeleted: play.api.libs.json.Reads[TransactionDeleted] = {
      for {
        discriminator <- (__ \ "discriminator").read[String]
        eventId <- (__ \ "event_id").read[String]
        timestamp <- (__ \ "timestamp").read[_root_.org.joda.time.DateTime]
        organization <- (__ \ "organization").read[String]
        transaction <- (__ \ "transaction").read[io.flow.billing.v0.models.Transaction]
      } yield if (discriminator == "transaction_deleted")  TransactionDeleted(eventId, timestamp, organization, transaction) else sys.error(s"Invalid discriminator: $discriminator") // possibly JsError instead?
    }

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