Skip to content

Commit

Permalink
Avoid casting DbDataReader to NpgsqlDataReader
Browse files Browse the repository at this point in the history
...in SystemTextJsonSerializer. The DbDataReader might be implemented
by a NpgsqlNestedDataReader instead of  a NpgsqlDataReader.
Use GetFieldValueAsync<Stream>() instead, which is called by
NpgsqlDataReader.GetStreamAsync(),

See: https://github.com/npgsql/npgsql/blob/fbbb28245ed3475d0857888622939ba7f13cb04f/src/Npgsql/NpgsqlDataReader.cs#L1456
  • Loading branch information
e-tobi committed Mar 25, 2024
1 parent fdc086f commit 2bad7bd
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Marten/Services/SystemTextJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public async ValueTask<T> FromJsonAsync<T>(Stream stream, CancellationToken canc
public async ValueTask<T> FromJsonAsync<T>(DbDataReader reader, int index,
CancellationToken cancellationToken = default)
{
await using var stream = await reader.As<NpgsqlDataReader>().GetStreamAsync(index, cancellationToken)
await using var stream = await reader.GetFieldValueAsync<Stream>(index, cancellationToken)
.ConfigureAwait(false);
return await FromJsonAsync<T>(stream, cancellationToken).ConfigureAwait(false);
}
Expand All @@ -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<NpgsqlDataReader>().GetStream(index));
return FromJson(type, reader.GetFieldValue<Stream>(index));
}

public async ValueTask<object> FromJsonAsync(Type type, Stream stream,
Expand All @@ -108,7 +108,7 @@ public async ValueTask<object> FromJsonAsync(Type type, Stream stream,
public async ValueTask<object> FromJsonAsync(Type type, DbDataReader reader, int index,
CancellationToken cancellationToken = default)
{
await using var stream = await reader.As<NpgsqlDataReader>().GetStreamAsync(index, cancellationToken)
await using var stream = await reader.GetFieldValueAsync<Stream>(index, cancellationToken)
.ConfigureAwait(false);
return await FromJsonAsync(type, stream, cancellationToken).ConfigureAwait(false);
}
Expand Down Expand Up @@ -211,6 +211,6 @@ public JsonDocument JsonDocumentFromJson(Stream stream)

public JsonDocument JsonDocumentFromJson(DbDataReader reader, int index)
{
return JsonDocumentFromJson(reader.As<NpgsqlDataReader>().GetStream(index));
return JsonDocumentFromJson(reader.GetFieldValue<Stream>(index));
}
}

0 comments on commit 2bad7bd

Please sign in to comment.