File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed
main/scala/io/cequence/openaiscala/gemini
test/scala/io/cequence/openaiscala/gemini Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -79,7 +79,20 @@ trait JsonFormats {
7979
8080 implicit val partFormat : Format [Part ] = Format (partReads, partWrites)
8181
82- implicit val contentFormat : Format [Content ] = Json .format[Content ]
82+ implicit val contentWrites : Writes [Content ] = (
83+ (__ \ " parts" ).write[Seq [Part ]] and
84+ (__ \ " role" ).writeNullable[ChatRole ]
85+ )(
86+ // somehow unlift(Content.unapply) is not working in Scala3
87+ (x : Content ) => (x.parts, x.role)
88+ )
89+
90+ implicit val contentReads : Reads [Content ] = (
91+ (__ \ " parts" ).readWithDefault[Seq [Part ]](Nil ) and
92+ (__ \ " role" ).readNullable[ChatRole ]
93+ )(Content .apply(_ : Seq [Part ], _ : Option [ChatRole ]))
94+
95+ implicit val contentFormat : Format [Content ] = Format (contentReads, contentWrites)
8396
8497 // Tools
8598 implicit val toolPrefixFormat : Format [ToolPrefix ] = enumFormat(ToolPrefix .values: _* )
Original file line number Diff line number Diff line change @@ -67,6 +67,19 @@ class JsonFormatsSpec extends AnyWordSpecLike with Matchers {
6767 )
6868 }
6969
70+ " deserialize candidate with empty content" in {
71+ testDeserialization[Candidate ](
72+ Candidate (
73+ content = Content .apply()
74+ ),
75+ """ {
76+ | "content" : {},
77+ | "safetyRatings" : [ ],
78+ | "groundingAttributions" : [ ]
79+ |}""" .stripMargin
80+ )
81+ }
82+
7083 " serialize and deserialize top candidate" in {
7184 prettyTestCodec[TopCandidates ](
7285 TopCandidates (
You can’t perform that action at this time.
0 commit comments