Skip to content

Commit ac5dc79

Browse files
Fix deserialization with missing XML namespace (#1327)
* Fix deserialization with missing XML namespace * Adjust formatting --------- Co-authored-by: ebozduman <ersan.bozduman@gmail.com>
1 parent 4c89ea8 commit ac5dc79

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Minio.Tests/UtilsTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Xml.Serialization;
1919
using Microsoft.VisualStudio.TestTools.UnitTesting;
2020
using Minio.DataModel;
21+
using Minio.DataModel.Result;
2122
using Minio.Exceptions;
2223
using Minio.Helper;
2324

@@ -235,4 +236,23 @@ public void TestisValidEndpoint()
235236
Assert.IsTrue(RequestUtil.IsValidEndpoint("A.domain.com"));
236237
Assert.IsTrue(RequestUtil.IsValidEndpoint("A.domain1.com"));
237238
}
239+
240+
[TestMethod]
241+
public void TestXmlResultWithoutNamespace()
242+
{
243+
var xml =
244+
"""
245+
<?xml version="1.0" encoding="UTF-8"?>
246+
<CopyObjectResult>
247+
<LastModified>
248+
2022-10-29T15:34:41.626Z
249+
</LastModified>
250+
<ETag>
251+
&#34;ead3fcd881dee32547f1b6ca1fc29463&#34;
252+
</ETag>
253+
</CopyObjectResult>
254+
""";
255+
var result = Utils.DeserializeXml<CopyObjectResult>(xml);
256+
Assert.IsNotNull(result);
257+
}
238258
}

Minio/Helper/AmazonAwsS3XmlReader.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@ public AmazonAwsS3XmlReader(Stream stream) : base(stream)
2424
{
2525
}
2626

27+
public AmazonAwsS3XmlReader(TextReader textReader) : base(textReader)
28+
{
29+
}
30+
2731
public override string NamespaceURI => "http://s3.amazonaws.com/doc/2006-03-01/";
2832
}

Minio/Helper/Utils.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,13 +1050,29 @@ public static string SerializeToXml<T>(T anyobject) where T : class
10501050
using var reader = new StreamReader(stream);
10511051
var xmlContent = reader.ReadToEnd();
10521052

1053-
return DeserializeXml<T>(xmlContent); // Call the string overload
1053+
return DeserializeNonS3Xml<T>(xmlContent); // Fallback to generic XML deserialization
10541054
}
10551055

10561056
public static T DeserializeXml<T>(string xml) where T : class, new()
10571057
{
10581058
if (string.IsNullOrEmpty(xml)) return default;
10591059

1060+
var ns = GetNamespace<T>();
1061+
if (!string.IsNullOrWhiteSpace(ns) && string.Equals(ns, "http://s3.amazonaws.com/doc/2006-03-01/",
1062+
StringComparison.OrdinalIgnoreCase))
1063+
{
1064+
using var stringReader = new StringReader(xml);
1065+
using var amazonAwsS3XmlReader = new AmazonAwsS3XmlReader(stringReader);
1066+
return (T)new XmlSerializer(typeof(T)).Deserialize(amazonAwsS3XmlReader);
1067+
}
1068+
1069+
return DeserializeNonS3Xml<T>(xml); // Fallback to generic XML deserialization
1070+
}
1071+
1072+
private static T DeserializeNonS3Xml<T>(string xml) where T : class, new()
1073+
{
1074+
if (string.IsNullOrEmpty(xml)) return default;
1075+
10601076
var settings = new XmlReaderSettings
10611077
{
10621078
// Disable DTD processing

0 commit comments

Comments
 (0)