Skip to content

Commit

Permalink
make JsonCodec.Config.ignoreEmptyCollections work with Options
Browse files Browse the repository at this point in the history
  • Loading branch information
a-morales committed Sep 12, 2024
1 parent 41892d5 commit ddc29af
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ object JsonCodec {
case e: Throwable => throw new RuntimeException(s"Failed to encode field '${s.name}' in $schema'", e)
}
val value = s.get(a)
if (!enc.isNothing(value) && !isEmptyOptionalValue(s, value, cfg)) {
if (!isEmptyOptionalValue(s, value, cfg)) {
if (first)
first = false
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import zio.Console._
import zio._
import zio.json.JsonDecoder.JsonError
import zio.json.ast.Json
import zio.json.{ DeriveJsonEncoder, JsonEncoder }
import zio.json.{ DeriveJsonEncoder, JsonEncoder, jsonExplicitNull }
import zio.schema.CaseSet._
import zio.schema._
import zio.schema.annotation._
Expand Down Expand Up @@ -118,33 +118,41 @@ object JsonCodecSpec extends ZIOSpecDefault {
suite("empty collections config")(
test("list empty") {
assertEncodesJson(
Schema[ListAndMap],
ListAndMap(Nil, Map("foo" -> 1)),
"""{"map":{"foo":1}}""",
Schema[ListAndMapAndOption],
ListAndMapAndOption(Nil, Map("foo" -> 1), Some("foo")),
"""{"map":{"foo":1},"option":"foo"}""",
JsonCodec.Config(ignoreEmptyCollections = true)
)
},
test("map empty") {
assertEncodesJson(
Schema[ListAndMap],
ListAndMap(List("foo"), Map.empty),
"""{"list":["foo"]}""",
Schema[ListAndMapAndOption],
ListAndMapAndOption(List("foo"), Map.empty, Some("foo")),
"""{"list":["foo"],"option":"foo"}""",
JsonCodec.Config(ignoreEmptyCollections = true)
)
},
test("option empty") {
assertEncodesJson(
Schema[ListAndMapAndOption],
ListAndMapAndOption(List("foo"), Map("foo" -> 1), None),
"""{"list":["foo"],"map":{"foo":1}}""",
JsonCodec.Config(ignoreEmptyCollections = true)
)
},
test("all empty") {
assertEncodesJson(
Schema[ListAndMap],
ListAndMap(Nil, Map.empty),
Schema[ListAndMapAndOption],
ListAndMapAndOption(Nil, Map.empty, None),
"""{}""",
JsonCodec.Config(ignoreEmptyCollections = true)
)
},
test("all empty, but don't ignore empty collections") {
assertEncodesJson(
Schema[ListAndMap],
ListAndMap(Nil, Map.empty),
"""{"list":[],"map":{}}""",
Schema[ListAndMapAndOption],
ListAndMapAndOption(Nil, Map.empty, None),
"""{"list":[],"map":{},"option":null}""",
JsonCodec.Config(ignoreEmptyCollections = false)
)
}
Expand Down Expand Up @@ -269,7 +277,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
assertEncodes(
WithOptionFields.schema,
WithOptionFields(Some("s"), None),
charSequenceToByteChunk("""{"a":"s"}""")
charSequenceToByteChunk("""{"a":"s","b":null}""")
)
}
),
Expand Down Expand Up @@ -327,7 +335,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
assertEncodes(
Subscription.schema,
Subscription.Unlimited(None),
charSequenceToByteChunk("""{"type":"unlimited"}""")
charSequenceToByteChunk("""{"type":"unlimited","until":null}""")
)
},
suite("with no discriminator")(
Expand Down Expand Up @@ -871,7 +879,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
assertDecodes(
WithOptionFields.schema,
WithOptionFields(Some("s"), None),
charSequenceToByteChunk("""{"a":"s"}""")
charSequenceToByteChunk("""{"a":"s","b":null}""")
)
},
test("case class with option fields accept empty json object as value") {
Expand Down Expand Up @@ -1510,8 +1518,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
Enumeration3(StringValue3("foo"))
) &> assertEncodesThenDecodes(
Schema[Enumeration3],
Enumeration3(StringValue3Multi("foo", "bar")),
print = true
Enumeration3(StringValue3Multi("foo", "bar"))
)
},
test("of case classes with discriminator") {
Expand Down Expand Up @@ -1878,6 +1885,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
charSequenceToByteChunk(encoded)
}

@jsonExplicitNull
case class SearchRequest(query: String, pageNumber: Int, resultPerPage: Int, nextPage: Option[String])

object SearchRequest {
Expand Down Expand Up @@ -2159,10 +2167,10 @@ object JsonCodecSpec extends ZIOSpecDefault {
implicit lazy val schema: Schema[WithOptField] = DeriveSchema.gen[WithOptField]
}

final case class ListAndMap(list: List[String], map: Map[String, Int])
final case class ListAndMapAndOption(list: List[String], map: Map[String, Int], option: Option[String])

object ListAndMap {
implicit lazy val schema: Schema[ListAndMap] = DeriveSchema.gen[ListAndMap]
object ListAndMapAndOption {
implicit lazy val schema: Schema[ListAndMapAndOption] = DeriveSchema.gen[ListAndMapAndOption]
}

final case class KeyWrapper(key: String)
Expand Down

0 comments on commit ddc29af

Please sign in to comment.