Skip to content

Commit

Permalink
Updated XML helper to support storing and parsing of Guid (#8717)
Browse files Browse the repository at this point in the history
* Updated XML helper to support storing and parsing of Guid

* Adding Guid and Guid? assertions to XmlHelperTests

* Restoring XmlHelper

* Adding Guid and Guid? assertions to XmlHelperTests

---------

Co-authored-by: Benedek Farkas <benedek.farkas@lombiq.com>
  • Loading branch information
RichieOrtiz and BenedekFarkas committed Feb 7, 2024
1 parent 97648ed commit 90b104e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
60 changes: 50 additions & 10 deletions src/Orchard.Tests/ContentManagement/XmlHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace Orchard.Tests.ContentManagement {
[TestFixture]
public class XmlHelperTests {
private const string _testGuidString = "98f3dc0a-01c3-4975-bd52-1b4f5a678d73";

[Test]
public void AddEl() {
var el = new XElement("data");
Expand Down Expand Up @@ -84,6 +86,14 @@ public void DateTimeToAttribute() {
Assert.That(el.Attribute("foo").Value, Is.EqualTo("1970-05-21T13:55:21.934Z"));
}

[Test]
public void GuidToAttribute() {
var el = new XElement("data");
el.Attr("guid", new Guid(_testGuidString));

Assert.That(el.Attribute("guid").Value, Is.EqualTo(_testGuidString));
}

[Test]
public void DoubleFloatDecimalToAttribute() {
var el = new XElement("data");
Expand Down Expand Up @@ -150,6 +160,14 @@ public void DoubleFloatDecimalToElement() {
Assert.That(el.Element("decimal").Value, Is.EqualTo("12.458"));
}

[Test]
public void GuidToElement() {
var el = new XElement("data");
el.El("guid", new Guid(_testGuidString));

Assert.That(el.Element("guid").Value, Is.EqualTo(_testGuidString));
}

[Test]
public void ReadElement() {
var el = XElement.Parse("<data><foo>bar</foo></data>");
Expand All @@ -168,12 +186,14 @@ public void SerializeObject() {
ADouble = 12.345D,
AFloat = 23.456F,
ADecimal = 34.567M,
AGuid = new Guid(_testGuidString),
ANullableInt = 42,
ANullableBoolean = true,
ANullableDate = new DateTime(1970, 5, 21, 13, 55, 21, 934, DateTimeKind.Utc),
ANullableDouble = 12.345D,
ANullableFloat = 23.456F,
ANullableDecimal = 34.567M
ANullableDecimal = 34.567M,
ANullableGuid = new Guid(_testGuidString)
};
var el = new XElement("data");
el.With(target)
Expand All @@ -184,12 +204,14 @@ public void SerializeObject() {
.ToAttr(t => t.ADouble)
.ToAttr(t => t.AFloat)
.ToAttr(t => t.ADecimal)
.ToAttr(t => t.AGuid)
.ToAttr(t => t.ANullableInt)
.ToAttr(t => t.ANullableBoolean)
.ToAttr(t => t.ANullableDate)
.ToAttr(t => t.ANullableDouble)
.ToAttr(t => t.ANullableFloat)
.ToAttr(t => t.ANullableDecimal);
.ToAttr(t => t.ANullableDecimal)
.ToAttr(t => t.ANullableGuid);


Assert.That(el.Attr("AString"), Is.EqualTo("foo"));
Expand All @@ -199,12 +221,14 @@ public void SerializeObject() {
Assert.That(el.Attr("ADouble"), Is.EqualTo("12.345"));
Assert.That(el.Attr("AFloat"), Is.EqualTo("23.456"));
Assert.That(el.Attr("ADecimal"), Is.EqualTo("34.567"));
Assert.That(el.Attr("AGuid"), Is.EqualTo(_testGuidString));
Assert.That(el.Attr("ANullableInt"), Is.EqualTo("42"));
Assert.That(el.Attr("ANullableBoolean"), Is.EqualTo("true"));
Assert.That(el.Attr("ANullableDate"), Is.EqualTo("1970-05-21T13:55:21.934Z"));
Assert.That(el.Attr("ANullableDouble"), Is.EqualTo("12.345"));
Assert.That(el.Attr("ANullableFloat"), Is.EqualTo("23.456"));
Assert.That(el.Attr("ANullableDecimal"), Is.EqualTo("34.567"));
Assert.That(el.Attr("ANullableGuid"), Is.EqualTo(_testGuidString));
}

[Test]
Expand All @@ -214,10 +238,10 @@ public void DeSerializeObject() {
XElement.Parse(
"<data AString=\"foo\" AnInt=\"42\" ABoolean=\"true\" " +
"ADate=\"1970-05-21T13:55:21.934Z\" ADouble=\"12.345\" " +
"AFloat=\"23.456\" ADecimal=\"34.567\" " +
$"AFloat=\"23.456\" ADecimal=\"34.567\" AGuid=\"{_testGuidString}\" " +
"ANullableInt=\"42\" ANullableBoolean=\"true\" " +
"ANullableDate=\"1970-05-21T13:55:21.934Z\" ANullableDouble=\"12.345\" " +
"ANullableFloat=\"23.456\" ANullableDecimal=\"34.567\"/>");
$"ANullableFloat=\"23.456\" ANullableDecimal=\"34.567\" ANullableGuid=\"{_testGuidString}\"/>");
el.With(target)
.FromAttr(t => t.AString)
.FromAttr(t => t.AnInt)
Expand All @@ -226,12 +250,14 @@ public void DeSerializeObject() {
.FromAttr(t => t.ADouble)
.FromAttr(t => t.AFloat)
.FromAttr(t => t.ADecimal)
.FromAttr(t => t.AGuid)
.FromAttr(t => t.ANullableInt)
.FromAttr(t => t.ANullableBoolean)
.FromAttr(t => t.ANullableDate)
.FromAttr(t => t.ANullableDouble)
.FromAttr(t => t.ANullableFloat)
.FromAttr(t => t.ANullableDecimal);
.FromAttr(t => t.ANullableDecimal)
.FromAttr(t => t.ANullableGuid);

Assert.That(target.AString, Is.EqualTo("foo"));
Assert.That(target.AnInt, Is.EqualTo(42));
Expand All @@ -240,12 +266,14 @@ public void DeSerializeObject() {
Assert.That(target.ADouble, Is.EqualTo(12.345D));
Assert.That(target.AFloat, Is.EqualTo(23.456F));
Assert.That(target.ADecimal, Is.EqualTo(34.567M));
Assert.That(target.AGuid, Is.EqualTo(new Guid(_testGuidString)));
Assert.That(target.ANullableInt, Is.EqualTo(42));
Assert.That(target.ANullableBoolean, Is.True);
Assert.That(target.ANullableDate, Is.EqualTo(new DateTime(1970, 5, 21, 13, 55, 21, 934, DateTimeKind.Utc)));
Assert.That(target.ANullableDouble, Is.EqualTo(12.345D));
Assert.That(target.ANullableFloat, Is.EqualTo(23.456F));
Assert.That(target.ANullableDecimal, Is.EqualTo(34.567M));
Assert.That(target.ANullableGuid, Is.EqualTo(new Guid(_testGuidString)));
}

[Test]
Expand All @@ -258,12 +286,14 @@ public void DeSerializeFromMissingAttributeLeavesValueIntact() {
ADouble = 12.345D,
AFloat = 23.456F,
ADecimal = 34.567M,
AGuid = new Guid(_testGuidString),
ANullableInt = 42,
ANullableBoolean = true,
ANullableDate = new DateTime(1970, 5, 21, 13, 55, 21, 934, DateTimeKind.Utc),
ANullableDouble = 12.345D,
ANullableFloat = 23.456F,
ANullableDecimal = 34.567M
ANullableDecimal = 34.567M,
ANullableGuid = new Guid(_testGuidString)
};
var el = new XElement("data");
el.With(target)
Expand All @@ -274,12 +304,14 @@ public void DeSerializeFromMissingAttributeLeavesValueIntact() {
.FromAttr(t => t.ADouble)
.FromAttr(t => t.AFloat)
.FromAttr(t => t.ADecimal)
.FromAttr(t => t.AGuid)
.FromAttr(t => t.ANullableInt)
.FromAttr(t => t.ANullableBoolean)
.FromAttr(t => t.ANullableDate)
.FromAttr(t => t.ANullableDouble)
.FromAttr(t => t.ANullableFloat)
.FromAttr(t => t.ANullableDecimal);
.FromAttr(t => t.ANullableDecimal)
.FromAttr(t => t.ANullableGuid);

Assert.That(target.AString, Is.EqualTo("foo"));
Assert.That(target.AnInt, Is.EqualTo(42));
Expand All @@ -288,12 +320,14 @@ public void DeSerializeFromMissingAttributeLeavesValueIntact() {
Assert.That(target.ADouble, Is.EqualTo(12.345D));
Assert.That(target.AFloat, Is.EqualTo(23.456F));
Assert.That(target.ADecimal, Is.EqualTo(34.567M));
Assert.That(target.AGuid, Is.EqualTo(new Guid(_testGuidString)));
Assert.That(target.ANullableInt, Is.EqualTo(42));
Assert.That(target.ANullableBoolean, Is.True);
Assert.That(target.ANullableDate, Is.EqualTo(new DateTime(1970, 5, 21, 13, 55, 21, 934, DateTimeKind.Utc)));
Assert.That(target.ANullableDouble, Is.EqualTo(12.345D));
Assert.That(target.ANullableFloat, Is.EqualTo(23.456F));
Assert.That(target.ANullableDecimal, Is.EqualTo(34.567M));
Assert.That(target.ANullableGuid, Is.EqualTo(new Guid(_testGuidString)));
}

[Test]
Expand Down Expand Up @@ -339,7 +373,8 @@ public void NullSerializes() {
.ToAttr(t => t.ANullableDate)
.ToAttr(t => t.ANullableDouble)
.ToAttr(t => t.ANullableFloat)
.ToAttr(t => t.ANullableDecimal);
.ToAttr(t => t.ANullableDecimal)
.ToAttr(t => t.ANullableGuid);

Assert.That(el.Attr("AString"), Is.EqualTo(""));
Assert.That(el.Attr("ANullableInt"), Is.EqualTo("null"));
Expand All @@ -348,6 +383,7 @@ public void NullSerializes() {
Assert.That(el.Attr("ANullableDouble"), Is.EqualTo("null"));
Assert.That(el.Attr("ANullableFloat"), Is.EqualTo("null"));
Assert.That(el.Attr("ANullableDecimal"), Is.EqualTo("null"));
Assert.That(el.Attr("ANullableGuid"), Is.EqualTo("null"));
}

[Test]
Expand All @@ -357,15 +393,16 @@ public void DeSerializeNull() {
XElement.Parse(
"<data AString=\"null\" ANullableInt=\"null\" ANullableBoolean=\"null\" " +
"ANullableDate=\"null\" ANullableDouble=\"null\" " +
"ANullableFloat=\"null\" ANullableDecimal=\"null\"/>");
"ANullableFloat=\"null\" ANullableDecimal=\"null\" ANullableGuid=\"null\"/>");
el.With(target)
.FromAttr(t => t.AString)
.FromAttr(t => t.ANullableInt)
.FromAttr(t => t.ANullableBoolean)
.FromAttr(t => t.ANullableDate)
.FromAttr(t => t.ANullableDouble)
.FromAttr(t => t.ANullableFloat)
.FromAttr(t => t.ANullableDecimal);
.FromAttr(t => t.ANullableDecimal)
.FromAttr(t => t.ANullableGuid);

Assert.That(target.AString, Is.EqualTo("null"));
Assert.That(target.ANullableInt, Is.Null);
Expand All @@ -374,6 +411,7 @@ public void DeSerializeNull() {
Assert.That(target.ANullableDouble, Is.Null);
Assert.That(target.ANullableFloat, Is.Null);
Assert.That(target.ANullableDecimal, Is.Null);
Assert.That(target.ANullableGuid, Is.Null);
}

private class Target {
Expand All @@ -384,12 +422,14 @@ private class Target {
public double ADouble { get; set; }
public float AFloat { get; set; }
public decimal ADecimal { get; set; }
public Guid AGuid { get; set; }
public int? ANullableInt { get; set; }
public bool? ANullableBoolean { get; set; }
public DateTime? ANullableDate { get; set; }
public double? ANullableDouble { get; set; }
public float? ANullableFloat { get; set; }
public decimal? ANullableDecimal { get; set; }
public Guid? ANullableGuid { get; set; }
}
}
}
12 changes: 10 additions & 2 deletions src/Orchard/ContentManagement/XmlHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
Expand Down Expand Up @@ -157,7 +157,7 @@ public static string ToString<T>(T value) {
return Convert.ToString(value);
}
if ((!type.IsValueType || Nullable.GetUnderlyingType(type) != null) &&
value == null &&
value == null &&
type != typeof(string)) {

return "null";
Expand Down Expand Up @@ -214,6 +214,10 @@ public static string ToString<T>(T value) {
return decimalValue.ToString(CultureInfo.InvariantCulture);
}

if (type == typeof(Guid) || type == typeof(Guid?)) {
return value == null ? "null" : value.ToString();
}

var underlyingType = Nullable.GetUnderlyingType(type) ?? type;

if (underlyingType.IsEnum) {
Expand Down Expand Up @@ -277,6 +281,10 @@ public static T Parse<T>(string value) {
return (T)(object)decimal.Parse(value, CultureInfo.InvariantCulture);
}

if (type == typeof(Guid) || type == typeof(Guid?)) {
return (T)(object)Guid.Parse(value);
}

var underlyingType = Nullable.GetUnderlyingType(type) ?? type;

if (underlyingType.IsEnum) {
Expand Down

0 comments on commit 90b104e

Please sign in to comment.