From ec5f19d37594c5caa7c5892131cc109d2b390133 Mon Sep 17 00:00:00 2001 From: Daniel Wust Date: Thu, 19 Dec 2024 13:33:46 +0100 Subject: [PATCH] Fix MultiJson when direct streaming is disabled (#149) --- .../Requests/Body/PostData.MultiJson.cs | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Elastic.Transport/Requests/Body/PostData.MultiJson.cs b/src/Elastic.Transport/Requests/Body/PostData.MultiJson.cs index b6cb7fd..96cdb31 100644 --- a/src/Elastic.Transport/Requests/Body/PostData.MultiJson.cs +++ b/src/Elastic.Transport/Requests/Body/PostData.MultiJson.cs @@ -49,6 +49,7 @@ public override void Write(Stream writableStream, ITransportConfiguration settin $"{nameof(PostDataMultiJson)} only does not support {nameof(PostType)}.{Type.GetStringValue()}"); MemoryStream? buffer = null; + var stream = writableStream; switch (Type) { @@ -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; @@ -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; @@ -102,6 +103,8 @@ public override async Task WriteAsync(Stream writableStream, ITransportConfigura $"{nameof(PostDataMultiJson)} only does not support {nameof(PostType)}.{Type.GetStringValue()}"); MemoryStream? buffer = null; + var stream = writableStream; + switch (Type) { case PostType.EnumerableOfString: @@ -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; @@ -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;