Skip to content

Commit

Permalink
Merge pull request #248 from MartinM85/fix/247-multipartbody-serializ…
Browse files Browse the repository at this point in the history
…ation

When MultipartBody is serialized the values are generated with CRLF
  • Loading branch information
andrueastman authored May 31, 2024
2 parents 0787139 + 8dbcd9e commit b2249c0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ 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
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
2 changes: 1 addition & 1 deletion src/Microsoft.Kiota.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageProjectUrl>https://aka.ms/kiota/docs</PackageProjectUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.9.3</VersionPrefix>
<VersionPrefix>1.9.4</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<SignAssembly>false</SignAssembly>
Expand Down
21 changes: 14 additions & 7 deletions src/MultipartBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +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)
{
writer.WriteStringValue(string.Empty, currentString);
using var partWriter = RequestAdapter.SerializationWriterFactory.GetSerializationWriter(part.ContentType);
partWriter.WriteStringValue(string.Empty, currentString);
WriteSerializedContent(writer, partWriter);
}
else if(part.Content is MemoryStream originalMemoryStream)
{
Expand Down Expand Up @@ -184,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

Check warning on line 194 in src/MultipartBody.cs

View workflow job for this annotation

GitHub Actions / Build

Private classes which are not derived in the current assembly should be marked as 'sealed'. (https://rules.sonarsource.com/csharp/RSPEC-3260)
{
public Part(string name, object content, string contentType, string? fileName)
Expand Down

0 comments on commit b2249c0

Please sign in to comment.