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

When MultipartBody is serialized the values are generated with CRLF #248

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
MartinM85 marked this conversation as resolved.
Show resolved Hide resolved

## [1.9.2] - 2024-05-24

Expand Down
31 changes: 28 additions & 3 deletions Microsoft.Kiota.Abstractions.Tests/MultipartBodyTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Kiota.Abstractions.Serialization;
using System.IO;
using Microsoft.Kiota.Abstractions.Serialization;
using Moq;
using Xunit;

Expand All @@ -10,10 +11,22 @@ public class MultipartBodyTests
public void KeepsFilename()
{
var writerMock = new Mock<ISerializationWriter>();
var jsonWriterMock = new Mock<ISerializationWriter>();
var requestAdapterMock = new Mock<IRequestAdapter>();
var serializationFactoryMock = new Mock<ISerializationWriterFactory>();

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)
Expand All @@ -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 + "--"));

Expand All @@ -40,10 +53,22 @@ public void KeepsFilename()
public void WorksWithoutFilename()
{
var writerMock = new Mock<ISerializationWriter>();
var jsonWriterMock = new Mock<ISerializationWriter>();
var requestAdapterMock = new Mock<IRequestAdapter>();
var serializationFactoryMock = new Mock<ISerializationWriterFactory>();

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)
Expand All @@ -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 + "--"));

Expand Down
9 changes: 8 additions & 1 deletion src/MultipartBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
MartinM85 marked this conversation as resolved.
Show resolved Hide resolved
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)
{
Expand Down