Skip to content

Fix exception when deserializing relative uris #1184

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

Merged
merged 2 commits into from
Jan 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1096,4 +1096,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
119 changes: 60 additions & 59 deletions src/Nest/Resolvers/Converters/UriJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,67 +1,68 @@
namespace Nest
{
using System;
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;

/// <summary>
/// Converter for converting Uri to String and vica versa
/// </summary>
/// <remarks>
/// Code originated from http://stackoverflow.com/a/8087049/106909
/// </remarks>
public sealed class UriJsonConverter : JsonConverter
{
/// <summary>
/// Determines whether this instance can convert the specified object type.
/// </summary>
/// <param name="objectType"></param>
/// <returns></returns>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Uri);
}
namespace Nest
{
/// <summary>
/// Converter for converting Uri to String and vica versa
/// </summary>
/// <remarks>
/// Code originated from http://stackoverflow.com/a/8087049/106909
/// </remarks>
[Obsolete("Scheduled to be removed in 2.0")]
public sealed class UriJsonConverter : JsonConverter
{
/// <summary>
/// Determines whether this instance can convert the specified object type.
/// </summary>
/// <param name="objectType"></param>
/// <returns></returns>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Uri);
}

/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader"></param>
/// <param name="objectType"></param>
/// <param name="existingValue"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
return new Uri((string)reader.Value);
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader"></param>
/// <param name="objectType"></param>
/// <param name="existingValue"></param>
/// <param name="serializer"></param>
/// <returns></returns>
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.");
}

/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="serializer"></param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (null == value)
{
writer.WriteNull();
return;
}
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="serializer"></param>
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.");
}
}
}
3 changes: 0 additions & 3 deletions src/Nest/Resolvers/ElasticContractResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@
<Compile Include="QueryParsers\Visitor\VisitorDemoUseCase.cs" />
<Compile Include="QueryParsers\Visitor\VisitorTests.cs" />
<Compile Include="Reproduce\Reproduce1146Tests.cs" />
<Compile Include="Reproduce\Reproduce629Tests.cs" />
<Compile Include="Reproduce\Reproduce990Tests.cs" />
<Compile Include="Reproduce\Reproduce974Tests.cs" />
<Compile Include="Reproduce\Reproduce928Tests.cs" />
Expand Down Expand Up @@ -1318,4 +1319,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
58 changes: 58 additions & 0 deletions src/Tests/Nest.Tests.Unit/Reproduce/Reproduce629Tests.cs
Original file line number Diff line number Diff line change
@@ -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<UriSerializationTest>(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<UriSerializationTest>(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<UriSerializationTest>(serialized);
deserialized.MyUri.AbsoluteUri.Should().Be("http://localhost:9200/");
}
}
}