-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from OmniSharp/fix/uri-serialization
fix/uri-serialization
- Loading branch information
Showing
8 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/Protocol/Serialization/Converters/AbsoluteUriConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
// #see https://github.com/NuGet/NuGet.Server | ||
using System; | ||
using Newtonsoft.Json; | ||
|
||
namespace OmniSharp.Extensions.LanguageServer.Protocol.Serialization.Converters | ||
{ | ||
/// <summary> | ||
/// This is necessary because Newtonsoft.Json creates <see cref="Uri"/> instances with | ||
/// <see cref="UriKind.RelativeOrAbsolute"/> which treats UNC paths as relative. NuGet.Core uses | ||
/// <see cref="UriKind.Absolute"/> which treats UNC paths as absolute. For more details, see: | ||
/// https://github.com/JamesNK/Newtonsoft.Json/issues/2128 | ||
/// </summary> | ||
class AbsoluteUriConverter : JsonConverter<Uri> | ||
{ | ||
public override Uri ReadJson(JsonReader reader, Type objectType, Uri existingValue, bool hasExistingValue, JsonSerializer serializer) | ||
{ | ||
if (reader.TokenType == JsonToken.Null) | ||
{ | ||
return null; | ||
} | ||
else if (reader.TokenType == JsonToken.String) | ||
{ | ||
var uri = new Uri((string)reader.Value, UriKind.RelativeOrAbsolute); | ||
if (!uri.IsAbsoluteUri) | ||
{ | ||
throw new JsonSerializationException($"The Uri must be absolute. Given: {reader.Value}"); | ||
} | ||
return uri; | ||
} | ||
|
||
throw new JsonSerializationException("The JSON value must be a string."); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, Uri value, JsonSerializer serializer) | ||
{ | ||
if (value == null) | ||
{ | ||
writer.WriteNull(); | ||
return; | ||
} | ||
|
||
if (!(value is Uri uriValue)) | ||
{ | ||
throw new JsonSerializationException("The value must be a URI."); | ||
} | ||
|
||
if (!uriValue.IsAbsoluteUri) | ||
{ | ||
throw new JsonSerializationException("The URI value must be an absolute Uri. Relative URI instances are not allowed."); | ||
} | ||
|
||
writer.WriteValue(uriValue.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters