Skip to content

Commit

Permalink
Deserialize xml content not containing namespace (#763)
Browse files Browse the repository at this point in the history
* Deserialize xml content not containing namespace

* Optimize

* Format

* dispose using streams

* Fixes

* addresses git actions/warnings

---------

Co-authored-by: Thomas Paterson <tpaterson@redboxvoice.com>
Co-authored-by: Ersan <ersan.bozduman@gmail.com>
  • Loading branch information
3 people authored Apr 14, 2023
1 parent c43ef12 commit 6a45116
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 135 deletions.
2 changes: 1 addition & 1 deletion Minio.Examples/Cases/PutObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static async Task Run(IMinioClient minio,
{
ReadOnlyMemory<byte> bs = await File.ReadAllBytesAsync(fileName).ConfigureAwait(false);
Console.WriteLine("Running example for API: PutObjectAsync");
var filestream = bs.AsStream();
using var filestream = bs.AsStream();

var fileInfo = new FileInfo(fileName);
var metaData = new Dictionary<string, string>
Expand Down
2 changes: 1 addition & 1 deletion Minio.Examples/Cases/SelectObjectContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static async Task Run(IMinioClient minio,
csvString.AppendLine("Employee5,Employee1,500");
csvString.AppendLine("Employee2,Employee1,800");
ReadOnlyMemory<byte> csvBytes = Encoding.UTF8.GetBytes(csvString.ToString());
var stream = csvBytes.AsStream();
using var stream = csvBytes.AsStream();
var putObjectArgs = new PutObjectArgs()
.WithBucket(bucketName)
.WithObject(newObjectName)
Expand Down
12 changes: 6 additions & 6 deletions Minio.Functional.Tests/FunctionalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ internal static async Task PutGetStatEncryptedObject_Test1(MinioClient minio)
.WithServerSideEncryption(ssec)
.WithCallbackStream(async (stream, cancellationToken) =>
{
var fileStream = File.Create(tempFileName);
using var fileStream = File.Create(tempFileName);
#if NETFRAMEWORK
await stream.CopyToAsync(fileStream).ConfigureAwait(false);
Expand Down Expand Up @@ -805,7 +805,7 @@ internal static async Task PutGetStatEncryptedObject_Test2(MinioClient minio)
.WithServerSideEncryption(ssec)
.WithCallbackStream(async (stream, cancellationToken) =>
{
var fileStream = File.Create(tempFileName);
using var fileStream = File.Create(tempFileName);
#if NETFRAMEWORK
await stream.CopyToAsync(fileStream).ConfigureAwait(false);
Expand Down Expand Up @@ -893,7 +893,7 @@ internal static async Task PutGetStatEncryptedObject_Test3(MinioClient minio)
.WithObject(objectName)
.WithCallbackStream(async (stream, cancellationToken) =>
{
var fileStream = File.Create(tempFileName);
using var fileStream = File.Create(tempFileName);
#if NETFRAMEWORK
await stream.CopyToAsync(fileStream).ConfigureAwait(false);
fileStream.Dispose();
Expand Down Expand Up @@ -1478,7 +1478,7 @@ internal static async Task SelectObjectContent_Test(MinioClient minio)
csvString.AppendLine("Employee5,Employee1,500");
csvString.AppendLine("Employee2,Employee1,800");
ReadOnlyMemory<byte> csvBytes = Encoding.UTF8.GetBytes(csvString.ToString());
var stream = csvBytes.AsStream();
using var stream = csvBytes.AsStream();

var putObjectArgs = new PutObjectArgs()
.WithBucket(bucketName)
Expand Down Expand Up @@ -2846,7 +2846,7 @@ void Notify(MinioNotificationRaw data)
await Task.Delay(sleepTime).ConfigureAwait(false);

var modelJson = "{\"test\": \"test\"}";
var stream = Encoding.UTF8.GetBytes(modelJson).AsMemory().AsStream();
using var stream = Encoding.UTF8.GetBytes(modelJson).AsMemory().AsStream();
var putObjectArgs = new PutObjectArgs()
.WithObject("test.json")
.WithBucket(bucketName)
Expand Down Expand Up @@ -2928,7 +2928,7 @@ internal static async Task ListenBucketNotificationsAsync_Test3(MinioClient mini

var modelJson = "{\"test\": \"test\"}";

var stream = Encoding.UTF8.GetBytes(modelJson).AsMemory().AsStream();
using var stream = Encoding.UTF8.GetBytes(modelJson).AsMemory().AsStream();
var putObjectArgs = new PutObjectArgs()
.WithObject("test.json")
.WithBucket(bucketName)
Expand Down
15 changes: 9 additions & 6 deletions Minio.Tests/NotificationTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text;
using System.Xml.Serialization;
using CommunityToolkit.HighPerformance;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Minio.DataModel;
Expand All @@ -12,17 +11,21 @@ namespace Minio.Tests;
[TestClass]
public class NotificationTest
{
[TestMethod]
public void TestNotificationStringHydration()
[DataTestMethod]
[DataRow(true)]
[DataRow(false)]
public void TestNotificationStringHydration(bool contentContainsNamespace)
{
var notificationString =
"<NotificationConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><TopicConfiguration><Id>YjVkM2Y0YmUtNGI3NC00ZjQyLWEwNGItNDIyYWUxY2I0N2M4 </Id><Arn>arnstring</Arn><Topic> arn:aws:sns:us-east-1:account-id:s3notificationtopic2 </Topic><Event> s3:ReducedRedundancyLostObject </Event><Event> s3:ObjectCreated: *</Event></TopicConfiguration></NotificationConfiguration>";
"<NotificationConfiguration" +
(contentContainsNamespace ? " xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"" : string.Empty) +
"><TopicConfiguration><Id>YjVkM2Y0YmUtNGI3NC00ZjQyLWEwNGItNDIyYWUxY2I0N2M4 </Id><Arn>arnstring</Arn><Topic> arn:aws:sns:us-east-1:account-id:s3notificationtopic2 </Topic><Event> s3:ReducedRedundancyLostObject </Event><Event> s3:ObjectCreated: *</Event></TopicConfiguration></NotificationConfiguration>";

try
{
ReadOnlyMemory<byte> contentBytes = Encoding.UTF8.GetBytes(notificationString);
var notification =
(BucketNotification)new XmlSerializer(typeof(BucketNotification)).Deserialize(contentBytes.AsStream());
using var stream = contentBytes.AsStream();
var notification = Utils.DeserializeXml<BucketNotification>(stream);
Assert.AreEqual(1, notification.TopicConfigs.Count);
}
catch (Exception ex)
Expand Down
4 changes: 2 additions & 2 deletions Minio.Tests/OperationsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public async Task PresignedGetObject()
if (!await ObjectExistsAsync(client, bucket, objectName).ConfigureAwait(false))
{
var helloData = Encoding.UTF8.GetBytes("hello world");
var helloStream = helloData.AsMemory().AsStream();
using var helloStream = helloData.AsMemory().AsStream();
var PutObjectArgs = new PutObjectArgs()
.WithBucket(bucket)
.WithObject(objectName)
Expand Down Expand Up @@ -114,7 +114,7 @@ public async Task PresignedGetObjectWithHeaders()
if (!await ObjectExistsAsync(client, bucket, objectName).ConfigureAwait(false))
{
var helloData = Encoding.UTF8.GetBytes("hello world");
var helloStream = helloData.AsMemory().AsStream();
using var helloStream = helloData.AsMemory().AsStream();
var PutObjectArgs = new PutObjectArgs()
.WithBucket(bucket)
.WithObject(objectName)
Expand Down
8 changes: 4 additions & 4 deletions Minio/ApiEndpoints/BucketOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

using System.Net;
using System.Reactive.Linq;
using System.Xml.Serialization;
using CommunityToolkit.HighPerformance;
using Minio.DataModel;
using Minio.DataModel.ILM;
Expand Down Expand Up @@ -190,9 +189,10 @@ await ExecuteTaskAsync(NoErrorHandlers, requestMessageBuilder, cancellationToken

var bucketList = new ListAllMyBucketsResult();
if (HttpStatusCode.OK.Equals(response.StatusCode))
bucketList =
new XmlSerializer(typeof(ListAllMyBucketsResult)).Deserialize(response.ContentBytes.AsStream()) as
ListAllMyBucketsResult;
{
using var stream = response.ContentBytes.AsStream();
bucketList = Utils.DeserializeXml<ListAllMyBucketsResult>(stream);
}

return bucketList;
}
Expand Down
33 changes: 17 additions & 16 deletions Minio/ApiEndpoints/ObjectOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using System.Reactive.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Serialization;
using CommunityToolkit.HighPerformance;
using Minio.DataModel;
using Minio.DataModel.ObjectLock;
Expand Down Expand Up @@ -1476,19 +1475,19 @@ private async Task<Tuple<ListPartsResult, List<Part>>> GetListPartsAsync(string
await ExecuteTaskAsync(NoErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken)
.ConfigureAwait(false);

var listPartsResult =
(ListPartsResult)new XmlSerializer(typeof(ListPartsResult)).Deserialize(Encoding.UTF8
.GetBytes(response.Content).AsMemory().AsStream());
using var stream = Encoding.UTF8.GetBytes(response.Content).AsMemory().AsStream();
var listPartsResult = Utils.DeserializeXml<ListPartsResult>(stream);

var root = XDocument.Parse(response.Content);
XNamespace ns = Utils.DetermineNamespace(root);

var uploads = from c in root.Root.Descendants("{http://s3.amazonaws.com/doc/2006-03-01/}Part")
var uploads = from c in root.Root.Descendants(ns + "Part")
select new Part
{
PartNumber = int.Parse(c.Element("{http://s3.amazonaws.com/doc/2006-03-01/}PartNumber").Value,
PartNumber = int.Parse(c.Element(ns + "PartNumber").Value,
CultureInfo.CurrentCulture),
ETag = c.Element("{http://s3.amazonaws.com/doc/2006-03-01/}ETag").Value.Replace("\"", string.Empty),
Size = long.Parse(c.Element("{http://s3.amazonaws.com/doc/2006-03-01/}Size").Value,
ETag = c.Element(ns + "ETag").Value.Replace("\"", string.Empty),
Size = long.Parse(c.Element(ns + "Size").Value,
CultureInfo.CurrentCulture)
};

Expand Down Expand Up @@ -1517,9 +1516,8 @@ private async Task<string> NewMultipartUploadAsync(string bucketName, string obj
using var response = await ExecuteTaskAsync(NoErrorHandlers,
requestMessageBuilder, cancellationToken: cancellationToken).ConfigureAwait(false);

var newUpload = (InitiateMultipartUploadResult)new XmlSerializer(typeof(InitiateMultipartUploadResult))
.Deserialize(response.ContentBytes.AsStream());

using var stream = response.ContentBytes.AsStream();
var newUpload = Utils.DeserializeXml<InitiateMultipartUploadResult>(stream);
return newUpload.UploadId;
}

Expand Down Expand Up @@ -1670,13 +1668,16 @@ await ExecuteTaskAsync(NoErrorHandlers, requestMessageBuilder, cancellationToken
object copyResult = null;

if (type == typeof(CopyObjectResult))
copyResult =
(CopyObjectResult)new XmlSerializer(typeof(CopyObjectResult)).Deserialize(
response.ContentBytes.AsStream());
{
using var stream = response.ContentBytes.AsStream();
copyResult = Utils.DeserializeXml<CopyObjectResult>(stream);
}

if (type == typeof(CopyPartResult))
copyResult =
(CopyPartResult)new XmlSerializer(typeof(CopyPartResult)).Deserialize(response.ContentBytes.AsStream());
{
using var stream = response.ContentBytes.AsStream();
copyResult = Utils.DeserializeXml<CopyPartResult>(stream);
}

return copyResult;
}
Expand Down
11 changes: 6 additions & 5 deletions Minio/Credentials/AssumeRoleBaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

using System.Net;
using System.Text;
using System.Xml.Serialization;
using CommunityToolkit.HighPerformance;
using Minio.DataModel;

Expand Down Expand Up @@ -132,10 +131,12 @@ public override async Task<AccessCredentials> GetCredentialsAsync()

internal virtual AccessCredentials ParseResponse(HttpResponseMessage response)
{
if (string.IsNullOrEmpty(Convert.ToString(response.Content)) || !HttpStatusCode.OK.Equals(response.StatusCode))
throw new ArgumentNullException("Unable to generate credentials. Response error.");
return (AccessCredentials)new XmlSerializer(typeof(AccessCredentials)).Deserialize(Encoding.UTF8
.GetBytes(Convert.ToString(response.Content)).AsMemory().AsStream());
var content = Convert.ToString(response.Content);
if (string.IsNullOrEmpty(content) || !HttpStatusCode.OK.Equals(response.StatusCode))
throw new ArgumentNullException(nameof(response), "Unable to generate credentials. Response error.");

using var stream = Encoding.UTF8.GetBytes(content).AsMemory().AsStream();
return Utils.DeserializeXml<AccessCredentials>(stream);
}

public override AccessCredentials GetCredentials()
Expand Down
7 changes: 4 additions & 3 deletions Minio/Credentials/AssumeRoleProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ public override async Task<AccessCredentials> GetCredentialsAsync()

AssumeRoleResponse assumeRoleResp = null;
if (responseResult.Response.IsSuccessStatusCode)
assumeRoleResp =
(AssumeRoleResponse)new XmlSerializer(typeof(AssumeRoleResponse)).Deserialize(
Encoding.UTF8.GetBytes(responseResult.Content).AsMemory().AsStream());
{
using var stream = Encoding.UTF8.GetBytes(responseResult.Content).AsMemory().AsStream();
assumeRoleResp = Utils.DeserializeXml<AssumeRoleResponse>(stream);
}

if (credentials == null &&
assumeRoleResp?.arr != null)
Expand Down
4 changes: 2 additions & 2 deletions Minio/Credentials/CertificateIdentityProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ public override async Task<AccessCredentials> GetCredentialsAsync()
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
using var stream = Encoding.UTF8.GetBytes(content).AsMemory().AsStream();
certResponse =
(CertificateResponse)new XmlSerializer(typeof(CertificateResponse))
.Deserialize(Encoding.UTF8.GetBytes(content).AsMemory().AsStream());
Utils.DeserializeXml<CertificateResponse>(stream);
}

if (Credentials == null && certResponse?.Cr != null)
Expand Down
10 changes: 5 additions & 5 deletions Minio/Credentials/WebIdentityClientGrantsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

using System.Net;
using System.Text;
using System.Xml.Serialization;
using CommunityToolkit.HighPerformance;
using Minio.DataModel;

Expand Down Expand Up @@ -62,12 +61,13 @@ internal override AccessCredentials ParseResponse(HttpResponseMessage response)
// Stream receiveStream = response.Content.ReadAsStreamAsync();
// StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
// txtBlock.Text = readStream.ReadToEnd();
if (string.IsNullOrWhiteSpace(Convert.ToString(response.Content)) ||
var content = Convert.ToString(response.Content);
if (string.IsNullOrWhiteSpace(content) ||
!HttpStatusCode.OK.Equals(response.StatusCode))
throw new ArgumentNullException("Unable to get credentials. Response error.");
throw new ArgumentNullException(nameof(response), "Unable to get credentials. Response error.");

return (AccessCredentials)new XmlSerializer(typeof(AccessCredentials)).Deserialize(Encoding.UTF8
.GetBytes(Convert.ToString(response.Content)).AsMemory().AsStream());
using var stream = Encoding.UTF8.GetBytes(content).AsMemory().AsStream();
return Utils.DeserializeXml<AccessCredentials>(stream);
}

protected void Validate()
Expand Down
4 changes: 2 additions & 2 deletions Minio/Credentials/WebIdentityProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ internal override AccessCredentials ParseResponse(HttpResponseMessage response)
{
Validate();
var credentials = base.ParseResponse(response);
return (AccessCredentials)new XmlSerializer(typeof(AccessCredentials)).Deserialize(Encoding.UTF8
.GetBytes(Convert.ToString(response.Content)).AsMemory().AsStream());
using var stream = Encoding.UTF8.GetBytes(Convert.ToString(response.Content)).AsMemory().AsStream();
return Utils.DeserializeXml<AccessCredentials>(stream);
}
}
Loading

0 comments on commit 6a45116

Please sign in to comment.