From 9a035ecdacdd6c4df2141bf3f674a8a4ff360881 Mon Sep 17 00:00:00 2001 From: Tobias Grimm Date: Fri, 15 Mar 2024 16:57:34 +0100 Subject: [PATCH] Avoid casting DbDataReader to NpgsqlDataReader ...in SystemTextJsonSerializer. The DbDataReader might be implemented by a NpgsqlNestedDataReader instead of a NpgsqlDataReader. Use GetFieldValueAsync() instead, which is called by NpgsqlDataReader.GetStreamAsync(), See: https://github.com/npgsql/npgsql/blob/fbbb28245ed3475d0857888622939ba7f13cb04f/src/Npgsql/NpgsqlDataReader.cs#L1456 --- src/Marten/Services/SystemTextJsonSerializer.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Marten/Services/SystemTextJsonSerializer.cs b/src/Marten/Services/SystemTextJsonSerializer.cs index 3a9207cde70..d7b2f2a0cc3 100644 --- a/src/Marten/Services/SystemTextJsonSerializer.cs +++ b/src/Marten/Services/SystemTextJsonSerializer.cs @@ -81,7 +81,7 @@ public async ValueTask FromJsonAsync(Stream stream, CancellationToken canc public async ValueTask FromJsonAsync(DbDataReader reader, int index, CancellationToken cancellationToken = default) { - await using var stream = await reader.As().GetStreamAsync(index, cancellationToken) + await using var stream = await reader.GetFieldValueAsync(index, cancellationToken) .ConfigureAwait(false); return await FromJsonAsync(stream, cancellationToken).ConfigureAwait(false); } @@ -94,7 +94,7 @@ public object FromJson(Type type, Stream stream) public object FromJson(Type type, DbDataReader reader, int index) { - return FromJson(type, reader.As().GetStream(index)); + return FromJson(type, reader.GetFieldValue(index)); } public async ValueTask FromJsonAsync(Type type, Stream stream, @@ -108,7 +108,7 @@ public async ValueTask FromJsonAsync(Type type, Stream stream, public async ValueTask FromJsonAsync(Type type, DbDataReader reader, int index, CancellationToken cancellationToken = default) { - await using var stream = await reader.As().GetStreamAsync(index, cancellationToken) + await using var stream = await reader.GetFieldValueAsync(index, cancellationToken) .ConfigureAwait(false); return await FromJsonAsync(type, stream, cancellationToken).ConfigureAwait(false); } @@ -211,6 +211,6 @@ public JsonDocument JsonDocumentFromJson(Stream stream) public JsonDocument JsonDocumentFromJson(DbDataReader reader, int index) { - return JsonDocumentFromJson(reader.As().GetStream(index)); + return JsonDocumentFromJson(reader.GetFieldValue(index)); } }