Skip to content

Commit a039bd8

Browse files
yyyu-googlecopybara-github
authored andcommitted
fix: remove redundant json stringify in converters
PiperOrigin-RevId: 885762877
1 parent 6ec6c07 commit a039bd8

File tree

11 files changed

+786
-835
lines changed

11 files changed

+786
-835
lines changed

Google.GenAI.E2E.Tests/Netstandard2_0Tests/packages.lock.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@
6464
},
6565
"System.Text.Json": {
6666
"type": "Direct",
67-
"requested": "[10.0.3, )",
68-
"resolved": "10.0.3",
69-
"contentHash": "NTUt9DL+maqbgrIYCAmeZUbX0NoXaueySyjW/bdOlFdSUDC1l51XnsbVEuj5tuad12vdq5Sviskp9uMVGgCNLw==",
67+
"requested": "[10.0.4, )",
68+
"resolved": "10.0.4",
69+
"contentHash": "1tRPRt8D/kzjGL7em1uJ3iJlvVIC3G/sZJ+ZgSvtVYLXGGO26Clkqy2b5uts/pyR706Yw8/xA7exeI2PI50dpw==",
7070
"dependencies": {
71-
"System.IO.Pipelines": "10.0.3",
72-
"System.Text.Encodings.Web": "10.0.3"
71+
"System.IO.Pipelines": "10.0.4",
72+
"System.Text.Encodings.Web": "10.0.4"
7373
}
7474
},
7575
"TestServerSdk": {
@@ -240,8 +240,8 @@
240240
},
241241
"System.IO.Pipelines": {
242242
"type": "Transitive",
243-
"resolved": "10.0.3",
244-
"contentHash": "WMxiA2jGdHnRBmoVK55YUq5VPaxW0Sg2frPtXV+urUMvpqHIga6lleV/YuryHIuGsAKVjQAjv6PrQ6IJpoLohQ=="
243+
"resolved": "10.0.4",
244+
"contentHash": "V7+RO17s/tzCpgqyj6t5vb54HFCvrRaMEwTcKDwpoQK66DRROzSff6kqtzHyiWRj6hrQQUmW80NL4pFSNhYpYA=="
245245
},
246246
"System.Management": {
247247
"type": "Transitive",
@@ -271,8 +271,8 @@
271271
},
272272
"System.Text.Encodings.Web": {
273273
"type": "Transitive",
274-
"resolved": "10.0.3",
275-
"contentHash": "l8QNBPp92bVzl9Kw8nNtm1uYRNNhUrdulZjM4a8YK/QGNa8z9utKsC0bDoPB+Vq8LOlbD3fIyGlabtz80jT7cw=="
274+
"resolved": "10.0.4",
275+
"contentHash": "6g3B7jNsPRNf4luuYt1qE4R8S3JI+zMsfGWL9Idv4Mk1Z9Gh+rCagp9sG3AejPS87yBj1DjopM4i3wSz0WnEqg=="
276276
},
277277
"YamlDotNet": {
278278
"type": "Transitive",
@@ -283,7 +283,7 @@
283283
"type": "Project",
284284
"dependencies": {
285285
"Google.Apis.Auth": "[1.69.0, )",
286-
"Microsoft.Extensions.AI.Abstractions": "[10.3.0, )",
286+
"Microsoft.Extensions.AI.Abstractions": "[10.4.0, )",
287287
"MimeTypes": "[2.5.2, )"
288288
}
289289
},
@@ -300,11 +300,11 @@
300300
},
301301
"Microsoft.Extensions.AI.Abstractions": {
302302
"type": "CentralTransitive",
303-
"requested": "[10.3.0, )",
304-
"resolved": "10.3.0",
305-
"contentHash": "hDjDvUERvUH3HBMs2MDusOcGJBjAHOG5pJIU2x/HZEa4e1UthNKt89cwMi3B+ogJo6skki1XFjfgGN3ksnVqvQ==",
303+
"requested": "[10.4.0, )",
304+
"resolved": "10.4.0",
305+
"contentHash": "t3S2H4do4YeNheIfE3GEl3MnKIrnxpbLu7a88spfApYR3in9ddhIq/GMtxgMaFjn/PUMTCFv5YH7Y6Q91dsDXQ==",
306306
"dependencies": {
307-
"System.Text.Json": "10.0.3"
307+
"System.Text.Json": "10.0.4"
308308
}
309309
},
310310
"MimeTypes": {

Google.GenAI.Tests/CommonTest.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,5 +302,81 @@ public void GetValueByPath_ArrayCollect_PreservesValuesAcrossAllElementsAndIsClo
302302
Assert.AreNotSame(element0DataNode, result[0]);
303303
Assert.AreNotSame(element1DataNode, result[1]);
304304
}
305+
306+
[TestMethod]
307+
public void ParseToJsonNode_WithJsonNodeInput_PreservesValueAndIsCloned()
308+
{
309+
string largeBase64 = new string('E', 100_000);
310+
var sourceNode = JsonValue.Create(largeBase64);
311+
312+
var result = Common.ParseToJsonNode(sourceNode);
313+
314+
Assert.IsNotNull(result);
315+
Assert.AreEqual(largeBase64, result.GetValue<string>());
316+
Assert.AreNotSame(sourceNode, result);
317+
}
318+
319+
[TestMethod]
320+
public void ParseToJsonNode_WithJsonObjectInput_PreservesAllPropertiesAndIsCloned()
321+
{
322+
string largeBase64 = new string('F', 100_000);
323+
var sourceNode = new JsonObject
324+
{
325+
["data"] = largeBase64,
326+
["mimeType"] = "image/png"
327+
};
328+
var innerDataNode = sourceNode["data"];
329+
330+
var result = Common.ParseToJsonNode(sourceNode) as JsonObject;
331+
332+
Assert.IsNotNull(result);
333+
Assert.AreEqual(largeBase64, result["data"].GetValue<string>());
334+
Assert.AreEqual("image/png", result["mimeType"].GetValue<string>());
335+
Assert.AreNotSame(sourceNode, result);
336+
// Inner nodes must also be cloned, not the same references.
337+
Assert.AreNotSame(innerDataNode, result["data"]);
338+
}
339+
340+
[TestMethod]
341+
public void ParseToJsonNode_WithNullInput_ReturnsNull()
342+
{
343+
var result = Common.ParseToJsonNode(null);
344+
345+
Assert.IsNull(result);
346+
}
347+
348+
[TestMethod]
349+
public void ParseToJsonNode_WithClrObjectInput_SerializesCorrectly()
350+
{
351+
var clrObject = new { mimeType = "image/png", width = 3840 };
352+
353+
var result = Common.ParseToJsonNode(clrObject) as JsonObject;
354+
355+
Assert.IsNotNull(result);
356+
Assert.AreEqual("image/png", result["mimeType"].GetValue<string>());
357+
Assert.AreEqual(3840, result["width"].GetValue<int>());
358+
}
359+
360+
[TestMethod]
361+
public void ParseToJsonNode_WithContentClrObject_SerializesCorrectly()
362+
{
363+
var content = new Types.Content
364+
{
365+
Role = "user",
366+
Parts = new List<Types.Part>
367+
{
368+
new Types.Part { Text = "Why is the sky blue?" }
369+
}
370+
};
371+
372+
var result = Common.ParseToJsonNode(content) as JsonObject;
373+
374+
Assert.IsNotNull(result);
375+
Assert.AreEqual("user", result["role"].GetValue<string>());
376+
var parts = result["parts"] as JsonArray;
377+
Assert.IsNotNull(parts);
378+
Assert.AreEqual(1, parts.Count);
379+
Assert.AreEqual("Why is the sky blue?", parts[0]["text"].GetValue<string>());
380+
}
305381
}
306382
}

0 commit comments

Comments
 (0)