New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistency when combining file:
URIs
#85229
Comments
Tagging subscribers to this area: @dotnet/area-system-io Issue DetailsDescriptionIt seems that the This occurs in my JsonSchema.Net library when loading schemas from files. When I do this, I need to set the base URI for the schema as the file path, so I've been doing this: var text = File.ReadAllText(fileName);
var schema = FromText(text, options);
schema.BaseUri = new Uri(Path.GetFullPath(fileName)); This creates a Reproduction Stepsvar filePath = "C:\\Folder\\Issue435_schema.json";
var withoutProtocol = new Uri(filePath);
var withProtocol = new Uri($"file:///{filePath}");
var fragment = new Uri("#/$defs/DerivedType", UriKind.RelativeOrAbsolute);
var withoutProtocolResult = new Uri(withoutProtocol, fragment);
var fileUriResult = new Uri(withProtocol, fragment);
Console.WriteLine("File path: {0}", filePath);
Console.WriteLine();
Console.WriteLine("Without protocol: {0}", withoutProtocol);
Console.WriteLine("With protocol: {0}", withProtocol);
Console.WriteLine();
Console.WriteLine("Combined, Without: {0}", withoutProtocolResult);
Console.WriteLine("Combined, With: {0}", fileUriResult); Expected behavior
Actual behavior
Regression?Unknown Known WorkaroundsFor now, I can update my URI creation to explicitly include the ConfigurationMy library is .Net Standard. I've tested this running .Net Core 3.1, .Net 6, and .Net 7. I'm running Windows 11 x64. It seems to work fine in my online evaluator (Blazor) at https://json-everything.net/json-schema, so I'm moderately certain it's contained to Windows, but I haven't tested anywhere else. Other informationDiscovered with help from users in gregsdennis/json-everything#436.
|
Tagging subscribers to this area: @dotnet/ncl Issue DetailsDescriptionIt seems that the This occurs in my JsonSchema.Net library when loading schemas from files. When I do this, I need to set the base URI for the schema as the file path, so I've been doing this: var text = File.ReadAllText(fileName);
var schema = FromText(text, options);
schema.BaseUri = new Uri(Path.GetFullPath(fileName)); This creates a Reproduction Stepsvar filePath = "C:\\Folder\\Issue435_schema.json";
var withoutProtocol = new Uri(filePath);
var withProtocol = new Uri($"file:///{filePath}");
var fragment = new Uri("#/$defs/DerivedType", UriKind.RelativeOrAbsolute);
var withoutProtocolResult = new Uri(withoutProtocol, fragment);
var fileUriResult = new Uri(withProtocol, fragment);
Console.WriteLine("File path: {0}", filePath);
Console.WriteLine();
Console.WriteLine("Without protocol: {0}", withoutProtocol);
Console.WriteLine("With protocol: {0}", withProtocol);
Console.WriteLine();
Console.WriteLine("Combined, Without: {0}", withoutProtocolResult);
Console.WriteLine("Combined, With: {0}", fileUriResult); Expected behavior
Actual behavior
Regression?Unknown Known WorkaroundsFor now, I can update my URI creation to explicitly include the ConfigurationMy library is .Net Standard. I've tested this running .Net Core 3.1, .Net 6, and .Net 7. I'm running Windows 11 x64. It seems to work fine in my online evaluator (Blazor) at https://json-everything.net/json-schema, so I'm moderately certain it's contained to Windows, but I haven't tested anywhere else. Other informationDiscovered with help from users in gregsdennis/json-everything#436.
|
@karelz This has been unaddressed for over a month. Can I get some direction or feedback on this, please? |
@gregsdennis I fear this may be by design - when you pass file path without That said, Uri is extremely tricky and was source of many security issues and breaking changes over time. It was designed with some unfortunate default behaviors which keep biting us :( ... Even if we agree it is undesired behavior, we might still triage it as Won't Fix. Given the low impact (first report so far) and tricky area + load on the team at the moment, there is nearly no chance it will be addressed in 8.0. (just setting expectations) |
This does not seems like regression. and Uri has historically quirks for compatibility reasons. It does not seems critical for 8.0, moving to future, |
Thanks for the comments. I'm not really worried about it being a regression. I'm more just confused by the behavior. If this isn't something that's going to be "fixed" (whatever that may mean), I think I'd be satisfied with an explanation as to why it works this way. |
Just pinging back since it's been 6 weeks or so. @MihaZupan can you provide insight into what's going on here? Am I doing something I shouldn't be? |
@MihaZupan just pinging again. |
@MihaZupan ping (bringing it to the top of your notifications ;)) |
Sorry about the late reply. As you've discovered, Uri treats file path inputs differently depending on whether they are explicitly specifying the Effectively, inputs that specified the scheme are treated as "real Uris", and will behave similarly to what you might expect for an In your example, the input with the scheme specified is considered as a real Uri, and is as such allowed to contain a fragment. new Uri("file:///C:/Folder/Issue435_schema.json#/$defs/DerivedType").Fragment // #/$defs/DerivedType
new Uri("C:/Folder/Issue435_schema.json#/$defs/DerivedType").Fragment // <empty> You can try swapping out When it then comes to combining the two inputs, we'll check if the runtime/src/libraries/System.Private.Uri/src/System/UriExt.cs Lines 711 to 717 in 7d399f6
For the implicit file path, the new Uri(new Uri("file:///C:/Folder/Issue435_schema.json"), "a/$defs/DerivedType").AbsoluteUri // file:///C:/Folder/a/$defs/DerivedType
new Uri(new Uri("http://foo/Folder/Issue435_schema.json"), "a/$defs/DerivedType").AbsoluteUri // http://foo/Folder/a/$defs/DerivedType That is, TL;DR I think the behavior observed here is "by design", and we'd recommend you use explicit file paths whenever possible. |
Closing as By Design per above explanation. |
Description
It seems that the
Uri(baseUri, newUri)
constructor behaves inconsistently when the base is afile:
URI depending on how that URI was created, specifically whether thefile:///
protocol is prefixed onto the string.This occurs in my JsonSchema.Net library when loading schemas from files. When I do this, I need to set the base URI for the schema as the file path, so I've been doing this:
This creates a
Uri
object that reveals the correct string, but it doesn't combine with JSON Pointer fragments correctly. Instead of appending a fragment, it replaces the file name with the#
from the pointer fragment.Reproduction Steps
Expected behavior
Actual behavior
Regression?
Unknown
Known Workarounds
For now, I can update my URI creation to explicitly include the
file:
protocol, but I don't think I should have to. If theUri
class is smart enough to recognize that a file path has been passed to it (which it is because it internally prepends thefile:
protocol), then these two cases should behave the same.Configuration
My library is .Net Standard. I've tested this running .Net Core 3.1, .Net 6, and .Net 7.
I'm running Windows 11 x64. It seems to work fine in my online evaluator (Blazor) at https://json-everything.net/json-schema, so I'm moderately certain it's contained to Windows, but I haven't tested anywhere else.
Other information
Discovered with help from users in gregsdennis/json-everything#436.
The text was updated successfully, but these errors were encountered: