question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.Uri does not round trip properly when original string is UNC path

See original GitHub issue

Consider 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:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
TylerLeonhardtcommented, Aug 19, 2019

Looks like the root issue is with .NET then… because:

[Uri]::new("/Users/tyler/").IsAbsoluteUri

# gives you

true

# and 

[Uri]::new("/Users/tyler/", [System.UriKind]::RelativeOrAbsolute).IsAbsoluteUri

#gives you

false
0reactions
TylerLeonhardtcommented, Aug 19, 2019

I’ve opened an issue on corefx to discuss this further https://github.com/dotnet/corefx/issues/40428

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Invalid URI with UNC path
This seems like a valid UNC path correct? I'm not sure where the error is here, and even more so I find it...
Read more >
Why is “IsUnc” of “[System.URI]” empty for UNC-complaint ...
Uri] cannot convert the provided String to a valid URI (as in your first and third example), the property 'IsUnc' will never return...
Read more >
Uri.IsUnc Property (System)
Gets a value that indicates whether the specified Uri is a universal naming convention (UNC) path.
Read more >
Cisco Preferred Architecture for Webex Calling
This guide is a design and deployment overview of the Cisco Webex Calling solution.
Read more >
Friends of the Family - Oct 2000 - Page 60 - Google Books Result
Network of family child-care homes: Number of homes that are financially supported by the company. ✓ Number of homes in network is not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found