diff --git a/src/Nest/Nest.csproj b/src/Nest/Nest.csproj index 0e4ebc7c7f1..5df67ac4c46 100644 --- a/src/Nest/Nest.csproj +++ b/src/Nest/Nest.csproj @@ -1096,4 +1096,4 @@ --> - + \ No newline at end of file diff --git a/src/Nest/Resolvers/Converters/UriJsonConverter.cs b/src/Nest/Resolvers/Converters/UriJsonConverter.cs index c85e345c6b5..4ca45af1cdb 100644 --- a/src/Nest/Resolvers/Converters/UriJsonConverter.cs +++ b/src/Nest/Resolvers/Converters/UriJsonConverter.cs @@ -1,67 +1,68 @@ -namespace Nest -{ - using System; - using Newtonsoft.Json; +using System; +using Newtonsoft.Json; - /// - /// Converter for converting Uri to String and vica versa - /// - /// - /// Code originated from http://stackoverflow.com/a/8087049/106909 - /// - public sealed class UriJsonConverter : JsonConverter - { - /// - /// Determines whether this instance can convert the specified object type. - /// - /// - /// - public override bool CanConvert(Type objectType) - { - return objectType == typeof(Uri); - } +namespace Nest +{ + /// + /// Converter for converting Uri to String and vica versa + /// + /// + /// Code originated from http://stackoverflow.com/a/8087049/106909 + /// + [Obsolete("Scheduled to be removed in 2.0")] + public sealed class UriJsonConverter : JsonConverter + { + /// + /// Determines whether this instance can convert the specified object type. + /// + /// + /// + public override bool CanConvert(Type objectType) + { + return objectType == typeof(Uri); + } - /// - /// Reads the JSON representation of the object. - /// - /// - /// - /// - /// - /// - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) - { - if (reader.TokenType == JsonToken.String) - return new Uri((string)reader.Value); + /// + /// Reads the JSON representation of the object. + /// + /// + /// + /// + /// + /// + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + if (reader.TokenType == JsonToken.String) + return new Uri((string)reader.Value); - if (reader.TokenType == JsonToken.Null) - return null; + if (reader.TokenType == JsonToken.Null) + return null; - throw new InvalidOperationException("Unhandled case for UriConverter. Check to see if this converter has been applied to the wrong serialization type."); - } + throw new InvalidOperationException("Unhandled case for UriConverter. Check to see if this converter has been applied to the wrong serialization type."); + } - /// - /// Writes the JSON representation of the object. - /// - /// - /// - /// - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) - { - if (null == value) - { - writer.WriteNull(); - return; - } + /// + /// Writes the JSON representation of the object. + /// + /// + /// + /// + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + if (null == value) + { + writer.WriteNull(); + return; + } - var uriValue = value as Uri; - if (uriValue != null) - { - writer.WriteValue(uriValue.OriginalString); - return; - } + var uriValue = value as Uri; + if (uriValue != null) + { + writer.WriteValue(uriValue.OriginalString); + return; + } - throw new InvalidOperationException("Unhandled case for UriConverter. Check to see if this converter has been applied to the wrong serialization type."); - } - } + throw new InvalidOperationException("Unhandled case for UriConverter. Check to see if this converter has been applied to the wrong serialization type."); + } + } } diff --git a/src/Nest/Resolvers/ElasticContractResolver.cs b/src/Nest/Resolvers/ElasticContractResolver.cs index 117d81b24f3..a921eef14f9 100644 --- a/src/Nest/Resolvers/ElasticContractResolver.cs +++ b/src/Nest/Resolvers/ElasticContractResolver.cs @@ -48,9 +48,6 @@ protected override JsonContract CreateContract(Type objectType) else if (objectType == typeof(Facet)) contract.Converter = new FacetConverter(); - else if (objectType == typeof(Uri)) - contract.Converter = new UriJsonConverter(); - else if (objectType == typeof(IAggregation)) contract.Converter = new AggregationConverter(); diff --git a/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj b/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj index d1522f3a1ec..4e062442fff 100644 --- a/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj +++ b/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj @@ -402,6 +402,7 @@ + @@ -1318,4 +1319,4 @@ --> - + \ No newline at end of file diff --git a/src/Tests/Nest.Tests.Unit/Reproduce/Reproduce629Tests.cs b/src/Tests/Nest.Tests.Unit/Reproduce/Reproduce629Tests.cs new file mode 100644 index 00000000000..c14cc35fe35 --- /dev/null +++ b/src/Tests/Nest.Tests.Unit/Reproduce/Reproduce629Tests.cs @@ -0,0 +1,58 @@ +using FluentAssertions; +using Newtonsoft.Json; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Nest.Tests.Unit.Reproduce +{ + [TestFixture] + public class Reproduce629Tests : BaseJsonTests + { + public class UriSerializationTest + { + public Uri MyUri { get; set; } + } + + [Test] + public void DeserializingRelativeUriThrowsException() + { + var json = @"{""MyUri"":""/relative""}"; + var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)); + var uriTest = _client.Serializer.Deserialize(stream); + uriTest.Should().NotBeNull(); + uriTest.MyUri.Should().NotBeNull(); + uriTest.MyUri.IsAbsoluteUri.Should().BeFalse(); + uriTest.MyUri.OriginalString.Should().Be("/relative"); + } + + [Test] + public void NestSerializesUriAsString() + { + var uriTest = new UriSerializationTest { MyUri = new Uri("http://localhost:9200") }; + + var serialized = Encoding.UTF8.GetString(_client.Serializer.Serialize(uriTest)); + serialized.JsonEquals(@"{ myUri: ""http://localhost:9200""}"); + + var deserialized = _client.Serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(serialized))); + deserialized.MyUri.AbsoluteUri.Should().Be("http://localhost:9200/"); + } + + [Test] + public void JsonNetSerializesUriAsString() + { + var uriTest = new UriSerializationTest { MyUri = new Uri("http://localhost:9200") }; + + var serialized = JsonConvert.SerializeObject(uriTest); + serialized.JsonEquals(@"{ MyUri: ""http://localhost:9200""}"); + + var deserialized = JsonConvert.DeserializeObject(serialized); + deserialized.MyUri.AbsoluteUri.Should().Be("http://localhost:9200/"); + } + } +} +