Skip to content

Commit c10d11d

Browse files
committed
Fix serialization of FunctionArguments
1 parent fb7b70f commit c10d11d

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.ClientModel.Primitives;
3+
using System.Runtime.CompilerServices;
4+
using System.Text.Json;
5+
6+
namespace OpenAI.Responses;
7+
8+
[CodeGenSerialization(nameof(FunctionArguments), SerializationValueHook = nameof(SerializeFunctionArgumentsValue), DeserializationValueHook = nameof(DeserializeFunctionArgumentsValue))]
9+
public partial class FunctionCallResponseItem : IJsonModel<FunctionCallResponseItem>
10+
{
11+
// CUSTOM: The REST API serializes this as a string.
12+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
13+
private void SerializeFunctionArgumentsValue(Utf8JsonWriter writer, ModelReaderWriterOptions options)
14+
{
15+
string value = FunctionArguments.ToMemory().IsEmpty
16+
? string.Empty
17+
: FunctionArguments.ToString();
18+
writer.WriteStringValue(value);
19+
}
20+
21+
// CUSTOM: Replaced the call to GetRawText() for a call to GetString() because otherwise the starting and ending
22+
// quotes of the string are included in the BinaryData. While this is actually a string in the REST API, we want to
23+
// handle it as JSON binary data instead.
24+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
25+
private static void DeserializeFunctionArgumentsValue(JsonProperty property, ref BinaryData functionArguments, ModelReaderWriterOptions options = null)
26+
{
27+
functionArguments = BinaryData.FromString(property.Value.GetString());
28+
}
29+
}

src/Generated/Models/Responses/FunctionCallResponseItem.Serialization.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,7 @@ protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWri
4444
if (_additionalBinaryDataProperties?.ContainsKey("arguments") != true)
4545
{
4646
writer.WritePropertyName("arguments"u8);
47-
#if NET6_0_OR_GREATER
48-
writer.WriteRawValue(FunctionArguments);
49-
#else
50-
using (JsonDocument document = JsonDocument.Parse(FunctionArguments))
51-
{
52-
JsonSerializer.Serialize(writer, document.RootElement);
53-
}
54-
#endif
47+
SerializeFunctionArgumentsValue(writer, options);
5548
}
5649
// Plugin customization: remove options.Format != "W" check
5750
// Plugin customization: apply Optional.Is*Defined() check based on type name dictionary lookup
@@ -112,7 +105,7 @@ internal static FunctionCallResponseItem DeserializeFunctionCallResponseItem(Jso
112105
}
113106
if (prop.NameEquals("arguments"u8))
114107
{
115-
functionArguments = BinaryData.FromString(prop.Value.GetRawText());
108+
DeserializeFunctionArgumentsValue(prop, ref functionArguments);
116109
continue;
117110
}
118111
if (prop.NameEquals("status"u8))

0 commit comments

Comments
 (0)