From 64101931072163893547789444195b90559367f2 Mon Sep 17 00:00:00 2001 From: gmarz Date: Tue, 6 Jan 2015 16:55:27 -0500 Subject: [PATCH 1/2] Fix exception when deserializing relative uris By removing the custom UriJsonConverter which was put in place to serialize uris to strings by default, however Json.NET already does this out of the box. Closes #629 --- src/Nest/Nest.csproj | 3 +- .../Resolvers/Converters/UriJsonConverter.cs | 67 ------------------- src/Nest/Resolvers/ElasticContractResolver.cs | 3 - .../Nest.Tests.Unit/Nest.Tests.Unit.csproj | 3 +- .../Reproduce/Reproduce629Tests.cs | 58 ++++++++++++++++ 5 files changed, 61 insertions(+), 73 deletions(-) delete mode 100644 src/Nest/Resolvers/Converters/UriJsonConverter.cs create mode 100644 src/Tests/Nest.Tests.Unit/Reproduce/Reproduce629Tests.cs diff --git a/src/Nest/Nest.csproj b/src/Nest/Nest.csproj index 0e4ebc7c7f1..aa1bfe5f5a4 100644 --- a/src/Nest/Nest.csproj +++ b/src/Nest/Nest.csproj @@ -997,7 +997,6 @@ - @@ -1096,4 +1095,4 @@ --> - + \ No newline at end of file diff --git a/src/Nest/Resolvers/Converters/UriJsonConverter.cs b/src/Nest/Resolvers/Converters/UriJsonConverter.cs deleted file mode 100644 index c85e345c6b5..00000000000 --- a/src/Nest/Resolvers/Converters/UriJsonConverter.cs +++ /dev/null @@ -1,67 +0,0 @@ -namespace Nest -{ - 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); - } - - /// - /// 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; - - 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; - } - - 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."); - } - } -} 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/"); + } + } +} + From 9756b01adf3ce2471ac3edd68e6a6ac4ec6a1452 Mon Sep 17 00:00:00 2001 From: gmarz Date: Thu, 8 Jan 2015 09:43:50 -0500 Subject: [PATCH 2/2] Put back UriJsonConverter and marked obsolete for BWC purposes --- src/Nest/Nest.csproj | 1 + .../Resolvers/Converters/UriJsonConverter.cs | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/Nest/Resolvers/Converters/UriJsonConverter.cs diff --git a/src/Nest/Nest.csproj b/src/Nest/Nest.csproj index aa1bfe5f5a4..5df67ac4c46 100644 --- a/src/Nest/Nest.csproj +++ b/src/Nest/Nest.csproj @@ -997,6 +997,7 @@ + diff --git a/src/Nest/Resolvers/Converters/UriJsonConverter.cs b/src/Nest/Resolvers/Converters/UriJsonConverter.cs new file mode 100644 index 00000000000..4ca45af1cdb --- /dev/null +++ b/src/Nest/Resolvers/Converters/UriJsonConverter.cs @@ -0,0 +1,68 @@ +using System; +using Newtonsoft.Json; + +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); + + 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."); + } + + /// + /// 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; + } + + throw new InvalidOperationException("Unhandled case for UriConverter. Check to see if this converter has been applied to the wrong serialization type."); + } + } +}