Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -508,8 +509,8 @@ public async Task JsonContent_DelegateInvoked(string newline, bool trickle, bool
trickle);

List<SseItem<Book>> items = useAsync ?
await ReadAllEventsAsync(stream, static (eventType, data) => JsonSerializer.Deserialize<Book>(data)) :
ReadAllEvents(stream, static (eventType, data) => JsonSerializer.Deserialize<Book>(data));
await ReadAllEventsAsync(stream, static (eventType, data) => JsonSerializer.Deserialize(data, JsonSerializerTestContext.Default.Book)) :
ReadAllEvents(stream, static (eventType, data) => JsonSerializer.Deserialize(data, JsonSerializerTestContext.Default.Book));

Assert.Equal(2, items.Count);
AssertSseItemEqual(new SseItem<Book>(new Book { title = "The Catcher in the Rye", author = "J.D. Salinger", published_year = 1951, genre = "Fiction" }, "message"), items[0]);
Expand Down Expand Up @@ -654,7 +655,7 @@ public async Task LongLines_ItemsProducedCorrectly(string newline, bool trickle,
{
string[] expected = Enumerable.Range(1, 100).Select(i => string.Concat(Enumerable.Repeat($"{i} ", i))).ToArray();

using Stream stream = GetStream([..expected.Select(s => $"data: {s}{newline}{newline}").SelectMany(Encoding.UTF8.GetBytes)], trickle);
using Stream stream = GetStream([.. expected.Select(s => $"data: {s}{newline}{newline}").SelectMany(Encoding.UTF8.GetBytes)], trickle);

List<SseItem<string>> items = useAsync ?
await ReadAllEventsAsync(stream) :
Expand Down Expand Up @@ -741,7 +742,7 @@ public async Task MultipleItemParsers_OpenAI_StreamingResponse(string newline, b
{
return bytes.SequenceEqual("[DONE]"u8) ?
new ChunkOrDone { Done = true } :
new ChunkOrDone { Json = JsonSerializer.Deserialize<JsonElement>(bytes) };
new ChunkOrDone { Json = JsonSerializer.Deserialize(bytes, JsonSerializerTestContext.Default.JsonElement) };
};

List<SseItem<ChunkOrDone>> items = useAsync ?
Expand Down Expand Up @@ -959,5 +960,9 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
}
#endif
}

[JsonSerializable(typeof(Book))]
[JsonSerializable(typeof(JsonElement))]
private sealed partial class JsonSerializerTestContext : JsonSerializerContext;
}
}