From 55f72445f9f0b9e063fc6a32329d26c7558a0f95 Mon Sep 17 00:00:00 2001 From: Martin Machacek Date: Thu, 30 May 2024 10:45:43 +0200 Subject: [PATCH 1/2] When MultipartBody is serialized the values are generated with CRLF --- CHANGELOG.md | 1 + .../MultipartBodyTests.cs | 31 +++++++++++++++++-- src/MultipartBody.cs | 9 +++++- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09e76078..5777bd06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Fix time formatting for other cultures. +- Fix MultipartBody serialization ## [1.9.2] - 2024-05-24 diff --git a/Microsoft.Kiota.Abstractions.Tests/MultipartBodyTests.cs b/Microsoft.Kiota.Abstractions.Tests/MultipartBodyTests.cs index cdd388af..d06afbdb 100644 --- a/Microsoft.Kiota.Abstractions.Tests/MultipartBodyTests.cs +++ b/Microsoft.Kiota.Abstractions.Tests/MultipartBodyTests.cs @@ -1,4 +1,5 @@ -using Microsoft.Kiota.Abstractions.Serialization; +using System.IO; +using Microsoft.Kiota.Abstractions.Serialization; using Moq; using Xunit; @@ -10,10 +11,22 @@ public class MultipartBodyTests public void KeepsFilename() { var writerMock = new Mock(); + var jsonWriterMock = new Mock(); var requestAdapterMock = new Mock(); var serializationFactoryMock = new Mock(); var body = new MultipartBody(); + jsonWriterMock.Setup(w => w.WriteStringValue("", "fileContent")); + using var ms = new MemoryStream(); + using var sr = new StreamWriter(ms); + + sr.Write("fileContent"); + sr.Flush(); + jsonWriterMock.Setup(w => w.GetSerializedContent()).Returns(ms); + + serializationFactoryMock + .Setup(r => r.GetSerializationWriter("application/json")) + .Returns(jsonWriterMock.Object); requestAdapterMock .Setup(r => r.SerializationWriterFactory) @@ -24,7 +37,7 @@ public void KeepsFilename() writerMock.Setup(w => w.WriteStringValue("", "--" + body.Boundary)); writerMock.Setup(w => w.WriteStringValue("Content-Type", "application/json")); writerMock.Setup(w => w.WriteStringValue("Content-Disposition", "form-data; name=\"file\"; filename=\"file.json\"")); - writerMock.Setup(w => w.WriteStringValue("", "fileContent")); + writerMock.Setup(w => w.WriteByteArrayValue("", ms.ToArray())); writerMock.Setup(w => w.WriteStringValue("", "")); writerMock.Setup(w => w.WriteStringValue("", "--" + body.Boundary + "--")); @@ -40,10 +53,22 @@ public void KeepsFilename() public void WorksWithoutFilename() { var writerMock = new Mock(); + var jsonWriterMock = new Mock(); var requestAdapterMock = new Mock(); var serializationFactoryMock = new Mock(); var body = new MultipartBody(); + jsonWriterMock.Setup(w => w.WriteStringValue("", "fileContent")); + using var ms = new MemoryStream(); + using var sr = new StreamWriter(ms); + + sr.Write("fileContent"); + sr.Flush(); + jsonWriterMock.Setup(w => w.GetSerializedContent()).Returns(ms); + + serializationFactoryMock + .Setup(r => r.GetSerializationWriter("application/json")) + .Returns(jsonWriterMock.Object); requestAdapterMock .Setup(r => r.SerializationWriterFactory) @@ -54,7 +79,7 @@ public void WorksWithoutFilename() writerMock.Setup(w => w.WriteStringValue("", "--" + body.Boundary)); writerMock.Setup(w => w.WriteStringValue("Content-Type", "application/json")); writerMock.Setup(w => w.WriteStringValue("Content-Disposition", "form-data; name=\"file\"")); - writerMock.Setup(w => w.WriteStringValue("", "fileContent")); + writerMock.Setup(w => w.WriteByteArrayValue("", ms.ToArray())); writerMock.Setup(w => w.WriteStringValue("", "")); writerMock.Setup(w => w.WriteStringValue("", "--" + body.Boundary + "--")); diff --git a/src/MultipartBody.cs b/src/MultipartBody.cs index f2b2a4a1..6ced3fb1 100644 --- a/src/MultipartBody.cs +++ b/src/MultipartBody.cs @@ -148,7 +148,14 @@ public void Serialize(ISerializationWriter writer) } else if(part.Content is string currentString) { - writer.WriteStringValue(string.Empty, currentString); + using var partWriter = RequestAdapter.SerializationWriterFactory.GetSerializationWriter(part.ContentType); + partWriter.WriteStringValue(string.Empty, currentString); + using var partContent = partWriter.GetSerializedContent(); + if(partContent.CanSeek) + partContent.Seek(0, SeekOrigin.Begin); + using var ms = new MemoryStream(); + partContent.CopyTo(ms); + writer.WriteByteArrayValue(string.Empty, ms.ToArray()); } else if(part.Content is MemoryStream originalMemoryStream) { From 8dbcd9e89c16f0ead35433e11cd7c613bc609adf Mon Sep 17 00:00:00 2001 From: Martin Machacek Date: Thu, 30 May 2024 12:50:17 +0200 Subject: [PATCH 2/2] When MultipartBody is serialized the values are generated with CRLF --- CHANGELOG.md | 7 ++++++- src/Microsoft.Kiota.Abstractions.csproj | 2 +- src/MultipartBody.cs | 24 ++++++++++++------------ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5777bd06..2e48571c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,12 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.9.4] - 2024-05-31 + +### Changed + +- Fix MultipartBody serialization + ## [1.9.3] - 2024-05-28 ### Changed - Fix time formatting for other cultures. -- Fix MultipartBody serialization ## [1.9.2] - 2024-05-24 diff --git a/src/Microsoft.Kiota.Abstractions.csproj b/src/Microsoft.Kiota.Abstractions.csproj index d4365c58..6d4a9f69 100644 --- a/src/Microsoft.Kiota.Abstractions.csproj +++ b/src/Microsoft.Kiota.Abstractions.csproj @@ -15,7 +15,7 @@ https://aka.ms/kiota/docs true true - 1.9.3 + 1.9.4 true false diff --git a/src/MultipartBody.cs b/src/MultipartBody.cs index 6ced3fb1..c785cccb 100644 --- a/src/MultipartBody.cs +++ b/src/MultipartBody.cs @@ -139,23 +139,13 @@ public void Serialize(ISerializationWriter writer) { using var partWriter = RequestAdapter.SerializationWriterFactory.GetSerializationWriter(part.ContentType); partWriter.WriteObjectValue(string.Empty, parsable); - using var partContent = partWriter.GetSerializedContent(); - if(partContent.CanSeek) - partContent.Seek(0, SeekOrigin.Begin); - using var ms = new MemoryStream(); - partContent.CopyTo(ms); - writer.WriteByteArrayValue(string.Empty, ms.ToArray()); + WriteSerializedContent(writer, partWriter); } else if(part.Content is string currentString) { using var partWriter = RequestAdapter.SerializationWriterFactory.GetSerializationWriter(part.ContentType); partWriter.WriteStringValue(string.Empty, currentString); - using var partContent = partWriter.GetSerializedContent(); - if(partContent.CanSeek) - partContent.Seek(0, SeekOrigin.Begin); - using var ms = new MemoryStream(); - partContent.CopyTo(ms); - writer.WriteByteArrayValue(string.Empty, ms.ToArray()); + WriteSerializedContent(writer, partWriter); } else if(part.Content is MemoryStream originalMemoryStream) { @@ -191,6 +181,16 @@ private void AddNewLine(ISerializationWriter writer) writer.WriteStringValue(string.Empty, string.Empty); } + private void WriteSerializedContent(ISerializationWriter writer, ISerializationWriter partWriter) + { + using var partContent = partWriter.GetSerializedContent(); + if(partContent.CanSeek) + partContent.Seek(0, SeekOrigin.Begin); + using var ms = new MemoryStream(); + partContent.CopyTo(ms); + writer.WriteByteArrayValue(string.Empty, ms.ToArray()); + } + private class Part { public Part(string name, object content, string contentType, string? fileName)