Skip to content

Commit

Permalink
Correctly identify DateTimeOffset JSON properties as scalar (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
APErebus authored Jul 28, 2023
1 parent 156fdc2 commit 4924efc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion source/Nevermore.IntegrationTests/Model/Machine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Nevermore.IntegrationTests.Model
using System;

namespace Nevermore.IntegrationTests.Model
{
public class Machine
{
Expand All @@ -7,5 +9,6 @@ public class Machine

public string Description { get; set; }
public Endpoint Endpoint { get; set; }
public DateTimeOffset LastModified { get; set; }
}
}
27 changes: 27 additions & 0 deletions source/Nevermore.IntegrationTests/QueryableIntegrationFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,33 @@ public void WhereEqualJson()
customers.Select(m => m.Name).Should().BeEquivalentTo("Machine A");
}

[Test]
public void WhereEqualJsonDateTimeOffset()
{
using var t = Store.BeginTransaction();

var testLastModified = new DateTimeOffset(2023, 07, 27, 16, 04, 00, TimeSpan.Zero);
var testMachines = new[]
{
new Machine { Name = "Machine A", LastModified = DateTimeOffset.Now },
new Machine { Name = "Machine B", LastModified = testLastModified},
new Machine { Name = "Machine C", LastModified = DateTimeOffset.Now},
};

foreach (var c in testMachines)
{
t.Insert(c);
}

t.Commit();

var customers = t.Queryable<Machine>()
.Where(m => m.LastModified == testLastModified)
.ToList();

customers.Select(m => m.Name).Should().BeEquivalentTo("Machine B");
}

[Test]
public void WhereIsNullJsonValue()
{
Expand Down
12 changes: 12 additions & 0 deletions source/Nevermore/Advanced/Queryable/PropertyInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Nevermore.Advanced.Queryable
{
internal static class PropertyInfoExtensions
{
static readonly HashSet<Type> KnownScalarTypes = new()
{
typeof(DateTimeOffset)
};

public static bool Matches(this PropertyInfo propertyInfo, PropertyInfo other)
{
return propertyInfo.Name.Equals(other.Name) &&
Expand All @@ -13,6 +19,12 @@ public static bool Matches(this PropertyInfo propertyInfo, PropertyInfo other)

public static bool IsScalar(this PropertyInfo propertyInfo)
{
// there are some types that can be consider scalars but have a TypeCode of Object
if (KnownScalarTypes.Contains(propertyInfo.PropertyType))
{
return true;
}

return Type.GetTypeCode(propertyInfo.PropertyType) switch
{
TypeCode.Object => false,
Expand Down

0 comments on commit 4924efc

Please sign in to comment.