question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Discriminator does not have effect when deriving sealed trait

See original GitHub issue

There’s a problem with semiautomatic derivations with discriminator when deriving sealed trait.

object Adt {
  sealed trait Result
  case class SimpleResult(res: String) extends Result
  case class ResultWrapper(res: Result)
}

object Codec extends AutoDerivation {
  import io.circe.generic.semiauto._

  implicit val configuration: Configuration = Configuration.default
    .withDiscriminator("type")
    .withSnakeCaseConstructorNames
    .withSnakeCaseMemberNames

  implicit val encWrap: Encoder[Result] = deriveEncoder[Result]
}

object Application extends App {
  import Codec._

  val unwrappedEncoded = {
    val r: Result = SimpleResult("this is result")
    r.asJson.spaces2
  }

  println(s"Unwrapped Encoded: $unwrappedEncoded")
}

results into:

{
  "SimpleResult" : {
    "res" : "this is result"
  }
}

but expected result is:

{
    "res" : "this is result",
    "$type" : "simple_result"
  }

if I wrap result in ResultWrapper, result is correct.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:6

github_iconTop GitHub Comments

4reactions
scalwaycommented, Jan 5, 2018

Ok… You have wrong import. This is next fiddle that shows what is changed! https://scalafiddle.io/sf/8XAonaP/4

assume that this issue should be closed!

1reaction
ruioliveirascommented, Jan 25, 2022

After some trouble with the same issue discribed here, i found the sollution. import io.circe.generic.semiauto._ != import io.circe.generic.extras.semiauto._

Example if you want to use: implicit lazy val config: Configuration = Configuration.default .withDiscriminator("type")

You must import impor io.circe.generic.**extras**.semiauto._ (Note the extras before the semi auto 🥲

and use it like this: implicit lazy val codec: Codec[Foo] = deriveConfiguredCodec[Foo]

Read more comments on GitHub >

github_iconTop Results From Across the Web

Output object with Circe with sealed trait + case classes adds ...
Effectively, I just want to delete the "Failed" block but keep everything within that block.
Read more >
How to decode an ADT with circe without disambiguating ...
Enumerating the ADT constructors. The most straightforward way to get the representation you want is to use generic derivation for the case ...
Read more >
circe/circe - Gitter
Hi, Has anybody got codec derivation working with Dotty. I tried Circe 0.13.0 with Dotty 0.22.0-RC1 as per info in the release notes...
Read more >
Sealed traits - language design - Rust Internals
Whether a trait is sealed has no effect on anything other than ... impl Sealed for A {} #[derive(Debug)] struct B {} impl...
Read more >
tapir documentation - SoftwareMill
If you don't have it already, you'll also need partial unification ... Schema derivation for coproduct types (sealed trait hierarchies) is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found