Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MultiJson when direct streaming is disabled #149

Merged
merged 1 commit into from
Dec 19, 2024
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
27 changes: 15 additions & 12 deletions src/Elastic.Transport/Requests/Body/PostData.MultiJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public override void Write(Stream writableStream, ITransportConfiguration settin
$"{nameof(PostDataMultiJson<T>)} only does not support {nameof(PostType)}.{Type.GetStringValue()}");

MemoryStream? buffer = null;
var stream = writableStream;

switch (Type)
{
Expand All @@ -60,12 +61,12 @@ public override void Write(Stream writableStream, ITransportConfiguration settin
if (!enumerator.MoveNext())
return;

BufferIfNeeded(settings.MemoryStreamFactory, disableDirectStreaming, ref buffer, ref writableStream);
BufferIfNeeded(settings.MemoryStreamFactory, disableDirectStreaming, ref buffer, ref stream);
do
{
var bytes = enumerator.Current.Utf8Bytes();
writableStream.Write(bytes, 0, bytes.Length);
writableStream.Write(NewLineByteArray, 0, 1);
stream.Write(bytes, 0, bytes.Length);
stream.Write(NewLineByteArray, 0, 1);
} while (enumerator.MoveNext());

break;
Expand All @@ -78,12 +79,12 @@ public override void Write(Stream writableStream, ITransportConfiguration settin
if (!enumerator.MoveNext())
return;

BufferIfNeeded(settings.MemoryStreamFactory, disableDirectStreaming, ref buffer, ref writableStream);
BufferIfNeeded(settings.MemoryStreamFactory, disableDirectStreaming, ref buffer, ref stream);
do
{
var o = enumerator.Current;
settings.RequestResponseSerializer.Serialize(o, writableStream, SerializationFormatting.None);
writableStream.Write(NewLineByteArray, 0, 1);
settings.RequestResponseSerializer.Serialize(o, stream, SerializationFormatting.None);
stream.Write(NewLineByteArray, 0, 1);
} while (enumerator.MoveNext());

break;
Expand All @@ -102,6 +103,8 @@ public override async Task WriteAsync(Stream writableStream, ITransportConfigura
$"{nameof(PostDataMultiJson<T>)} only does not support {nameof(PostType)}.{Type.GetStringValue()}");

MemoryStream? buffer = null;
var stream = writableStream;

switch (Type)
{
case PostType.EnumerableOfString:
Expand All @@ -113,12 +116,12 @@ public override async Task WriteAsync(Stream writableStream, ITransportConfigura
if (!enumerator.MoveNext())
return;

BufferIfNeeded(settings.MemoryStreamFactory, disableDirectStreaming, ref buffer, ref writableStream);
BufferIfNeeded(settings.MemoryStreamFactory, disableDirectStreaming, ref buffer, ref stream);
do
{
var bytes = enumerator.Current.Utf8Bytes();
await writableStream.WriteAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false);
await writableStream.WriteAsync(NewLineByteArray, 0, 1, cancellationToken).ConfigureAwait(false);
await stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false);
await stream.WriteAsync(NewLineByteArray, 0, 1, cancellationToken).ConfigureAwait(false);
} while (enumerator.MoveNext());

break;
Expand All @@ -132,14 +135,14 @@ public override async Task WriteAsync(Stream writableStream, ITransportConfigura
if (!enumerator.MoveNext())
return;

BufferIfNeeded(settings.MemoryStreamFactory, disableDirectStreaming, ref buffer, ref writableStream);
BufferIfNeeded(settings.MemoryStreamFactory, disableDirectStreaming, ref buffer, ref stream);
do
{
var o = enumerator.Current;
await settings.RequestResponseSerializer.SerializeAsync(o, writableStream,
await settings.RequestResponseSerializer.SerializeAsync(o, stream,
SerializationFormatting.None, cancellationToken)
.ConfigureAwait(false);
await writableStream.WriteAsync(NewLineByteArray, 0, 1, cancellationToken).ConfigureAwait(false);
await stream.WriteAsync(NewLineByteArray, 0, 1, cancellationToken).ConfigureAwait(false);
} while (enumerator.MoveNext());

break;
Expand Down
Loading