Skip to content

Commit 6d74fbf

Browse files
committed
Introduce ability to map Uri fields
**Visual Studio**'s code analysis (rightly) complains that our view models are not of type `Uri`, despite ending in `Url`. Fair. Our `TopicMappingService`, however, doesn't support mapping to type `Uri`. So we've introduced that functionality—alongside some extensions of tests to ensure that they're working correctly.
1 parent 5df14d8 commit 6d74fbf

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

OnTopic.Tests/TopicMappingServiceTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,14 @@ public async Task MapNullablePropertiesWithInvalidValues() {
182182
topic.Attributes.SetValue("NullableInteger", "A");
183183
topic.Attributes.SetValue("NullableBoolean", "43");
184184
topic.Attributes.SetValue("NullableDateTime", "Hello World");
185+
topic.Attributes.SetValue("NullableUrl", "invalid://Web\\Path\\File!?@Query=String?");
185186

186187
var target = await mappingService.MapAsync<NullablePropertyTopicViewModel>(topic).ConfigureAwait(false);
187188

188189
Assert.IsNull(target.NullableInteger);
189190
Assert.IsNull(target.NullableBoolean);
190191
Assert.IsNull(target.NullableDateTime);
192+
Assert.IsNull(target.NullableUrl);
191193

192194
//The following should not be null since they map to non-nullable properties which will have default values
193195
Assert.AreEqual(topic.Title, target.Title);
@@ -214,6 +216,7 @@ public async Task MapNullablePropertiesWithValidValues() {
214216
topic.Attributes.SetValue("NullableDouble", "3.14159265359");
215217
topic.Attributes.SetValue("NullableBoolean", "tRuE");
216218
topic.Attributes.SetValue("NullableDateTime", "10/15/1976");
219+
topic.Attributes.SetValue("NullableUrl", "/Web/Path/File?Query=String");
217220

218221
topic.Attributes.SetValue("Title", "Hello World.");
219222
topic.Attributes.SetValue("IsHidden", "true");
@@ -226,6 +229,7 @@ public async Task MapNullablePropertiesWithValidValues() {
226229
Assert.AreEqual<double?>(3.14159265359, target.NullableDouble);
227230
Assert.AreEqual<bool?>(true, target.NullableBoolean);
228231
Assert.AreEqual<DateTime?>(new DateTime(1976, 10, 15), target.NullableDateTime);
232+
Assert.AreEqual<Uri?>(new Uri("/Web/Path/File?Query=String", UriKind.RelativeOrAbsolute), target.NullableUrl);
229233

230234
Assert.AreEqual<string?>(topic.Title, target.Title);
231235
Assert.AreEqual<bool?>(target.IsHidden, target.IsHidden);

OnTopic.Tests/ViewModels/NullablePropertyTopicViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ public class NullablePropertyTopicViewModel {
2828

2929
public DateTime? NullableDateTime { get; set; }
3030

31+
public Uri? NullableUrl { get; set; }
32+
3133
public String? Title { get; set; }
3234

3335
public bool? IsHidden { get; set; }
3436

3537
public DateTime? LastModified { get; set; }
3638

39+
3740
} //Class
3841
} //Namespace

OnTopic/Internal/Reflection/TypeMemberInfoCollection.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ static TypeMemberInfoCollection() {
4141
typeof(double?),
4242
typeof(string),
4343
typeof(DateTime),
44-
typeof(DateTime?)
44+
typeof(DateTime?),
45+
typeof(Uri)
4546
};
4647
}
4748

@@ -434,6 +435,11 @@ private static bool IsSettableType(Type sourceType, Type? targetType = null) {
434435
valueObject = date;
435436
}
436437
}
438+
else if (type.Equals(typeof(Uri))) {
439+
if (Uri.TryCreate(value, UriKind.RelativeOrAbsolute, out var uri)) {
440+
valueObject = uri;
441+
}
442+
}
437443

438444
return valueObject;
439445

0 commit comments

Comments
 (0)