Discriminator does not have effect when deriving sealed trait
See original GitHub issueThere’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:
- Created 6 years ago
- Reactions:5
- Comments:6
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
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]