System.Uri does not round trip properly when original string is UNC path
See original GitHub issueConsider a path in this form:
//fileserver/Framework/NuGet/server.png
This is parsable by System.Uri
constructor. In fact, it is parseable with any of the UriKind
values. My understanding is that Uri
should round trip safely through JSON serialization and deserialization. If this is a bad assumption, perhaps this should be called out in the documentation (or… maybe I missed it).
With the following bit of code, you can see that the Uri
become relative after a round trip.
using System;
using Newtonsoft.Json;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var before = new Uri("//fileserver/Framework/NuGet/server.png", UriKind.Absolute);
Console.WriteLine("IsAbsoluteUri: " + before.IsAbsoluteUri);
var json = JsonConvert.SerializeObject(before);
Console.WriteLine("JSON: " + json);
var after = JsonConvert.DeserializeObject<Uri>(json);
Console.WriteLine("IsAbsoluteUri: " + after.IsAbsoluteUri);
}
}
}
Output:
IsAbsoluteUri: True
JSON: "//fileserver/Framework/NuGet/server.png"
IsAbsoluteUri: False
I think the crux of the issue is that a UNC path like this works on all forms of the Uri
contrustor:
new Uri(value);
new Uri(value, UriKind.Absolute);
new Uri(value, UriKind.RelativeOrAbsolute);
new Uri(value, UriKind.Relative);
… meaning this sort of string is kind of ambiguous given then following parse line: https://github.com/JamesNK/Newtonsoft.Json/blob/cdf10151d507d497a3f9a71d36d544b199f73435/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs#L478
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Looks like the root issue is with .NET then… because:
I’ve opened an issue on corefx to discuss this further https://github.com/dotnet/corefx/issues/40428