Skip to content

Commit 1d67992

Browse files
Workaround Unicode characters in URIs .NET bug
1 parent 09fc4f3 commit 1d67992

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/Protocol/Serialization/Converters/AbsoluteUriConverter.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,21 @@ public override void WriteJson(JsonWriter writer, Uri value, JsonSerializer seri
5151
throw new JsonSerializationException("The URI value must be an absolute Uri. Relative URI instances are not allowed.");
5252
}
5353

54-
writer.WriteValue(uriValue.ToString());
54+
if (uriValue.IsFile)
55+
{
56+
// Regular file paths
57+
if (uriValue.HostNameType == UriHostNameType.Basic)
58+
{
59+
writer.WriteValue($"{uriValue.Scheme}://{uriValue.PathAndQuery}");
60+
return;
61+
}
62+
63+
// UNC file paths
64+
writer.WriteValue($"{uriValue.Scheme}://{uriValue.Host}{uriValue.PathAndQuery}");
65+
return;
66+
}
67+
68+
writer.WriteValue(uriValue.AbsoluteUri);
5569
}
5670
}
5771
}

test/Lsp.Tests/Models/ApplyWorkspaceEditParamsTests.cs

+31
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,37 @@ public void SimpleTest(string expected)
4343
deresult.Should().BeEquivalentTo(model);
4444
}
4545

46+
[Theory, JsonFixture]
47+
public void NonStandardCharactersTest(string expected)
48+
{
49+
var model = new ApplyWorkspaceEditParams()
50+
{
51+
Edit = new WorkspaceEdit()
52+
{
53+
Changes = new Dictionary<Uri, IEnumerable<TextEdit>>() {
54+
{
55+
new Uri("/abc/123/Mörkö.cs"), new [] {
56+
new TextEdit() {
57+
NewText = "new text",
58+
Range = new Range(new Position(1, 1), new Position(2,2))
59+
},
60+
new TextEdit() {
61+
NewText = "new text2",
62+
Range = new Range(new Position(3, 3), new Position(4,4))
63+
}
64+
}
65+
}
66+
}
67+
}
68+
};
69+
var result = Fixture.SerializeObject(model);
70+
71+
result.Should().Be(expected);
72+
73+
var deresult = new Serializer(ClientVersion.Lsp3).DeserializeObject<ApplyWorkspaceEditParams>(expected);
74+
deresult.Should().BeEquivalentTo(model);
75+
}
76+
4677
[Theory, JsonFixture]
4778
public void DocumentChangesTest(string expected)
4879
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"edit": {
3+
"changes": {
4+
"file:///abc/123/M%C3%B6rk%C3%B6.cs": [
5+
{
6+
"range": {
7+
"start": {
8+
"line": 1,
9+
"character": 1
10+
},
11+
"end": {
12+
"line": 2,
13+
"character": 2
14+
}
15+
},
16+
"newText": "new text"
17+
},
18+
{
19+
"range": {
20+
"start": {
21+
"line": 3,
22+
"character": 3
23+
},
24+
"end": {
25+
"line": 4,
26+
"character": 4
27+
}
28+
},
29+
"newText": "new text2"
30+
}
31+
]
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)