-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
160 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
zio-json/shared/src/test/scala-2.x/zio/json/ConfigurableDeriveCodecSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package zio.json | ||
|
||
import zio.json.JsonCodecConfiguration.SumTypeHandling.DiscriminatorField | ||
import zio.test._ | ||
|
||
object ConfigurableDeriveCodecSpec extends ZIOSpecDefault { | ||
case class ClassWithFields(someField: Int, someOtherField: String) | ||
|
||
sealed trait ST | ||
|
||
object ST { | ||
case object CaseObj extends ST | ||
case class CaseClass(i: Int) extends ST | ||
} | ||
|
||
def spec = suite("ConfigurableDeriveCodecSpec")( | ||
suite("defaults")( | ||
test("should not map field names by default") { | ||
val expectedStr = """{"someField":1,"someOtherField":"a"}""" | ||
val expectedObj = ClassWithFields(1, "a") | ||
|
||
implicit val codec: JsonCodec[ClassWithFields] = DeriveJsonCodec.gen | ||
|
||
assertTrue( | ||
expectedStr.fromJson[ClassWithFields].toOption.get == expectedObj, | ||
expectedObj.toJson == expectedStr | ||
) | ||
}, | ||
test("should not use discriminator by default") { | ||
val expectedStr = """{"CaseObj":{}}""" | ||
val expectedObj: ST = ST.CaseObj | ||
|
||
implicit val codec: JsonCodec[ST] = DeriveJsonCodec.gen | ||
|
||
assertTrue( | ||
expectedStr.fromJson[ST].toOption.get == expectedObj, | ||
expectedObj.toJson == expectedStr | ||
) | ||
}, | ||
test("should allow extra fields by default") { | ||
val jsonStr = """{"someField":1,"someOtherField":"a","extra":123}""" | ||
val expectedObj = ClassWithFields(1, "a") | ||
|
||
implicit val codec: JsonCodec[ClassWithFields] = DeriveJsonCodec.gen | ||
|
||
assertTrue( | ||
jsonStr.fromJson[ClassWithFields].toOption.get == expectedObj | ||
) | ||
} | ||
), | ||
suite("overrides")( | ||
test("should override field name mapping") { | ||
val expectedStr = """{"some_field":1,"some_other_field":"a"}""" | ||
val expectedObj = ClassWithFields(1, "a") | ||
|
||
implicit val config: JsonCodecConfiguration = | ||
JsonCodecConfiguration(fieldNameMapping = SnakeCase) | ||
implicit val codec: JsonCodec[ClassWithFields] = DeriveJsonCodec.gen | ||
|
||
assertTrue( | ||
expectedStr.fromJson[ClassWithFields].toOption.get == expectedObj, | ||
expectedObj.toJson == expectedStr | ||
) | ||
}, | ||
test("should specify discriminator") { | ||
val expectedStr = """{"$type":"CaseClass","i":1}""" | ||
val expectedObj: ST = ST.CaseClass(i = 1) | ||
|
||
implicit val config: JsonCodecConfiguration = | ||
JsonCodecConfiguration(sumTypeHandling = DiscriminatorField("$type")) | ||
implicit val codec: JsonCodec[ST] = DeriveJsonCodec.gen | ||
|
||
assertTrue( | ||
expectedStr.fromJson[ST].toOption.get == expectedObj, | ||
expectedObj.toJson == expectedStr | ||
) | ||
}, | ||
test("should prevent extra fields") { | ||
val jsonStr = """{"someField":1,"someOtherField":"a","extra":123}""" | ||
|
||
implicit val config: JsonCodecConfiguration = | ||
JsonCodecConfiguration(allowExtraFields = false) | ||
implicit val codec: JsonCodec[ClassWithFields] = DeriveJsonCodec.gen | ||
|
||
assertTrue( | ||
jsonStr.fromJson[ClassWithFields].isLeft | ||
) | ||
} | ||
) | ||
) | ||
} |