Skip to content

Commit

Permalink
Backport "Allow empty filter expressions to be visited #5214" to 12 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored Aug 16, 2022
1 parent cfdc549 commit 6f0486c
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public override bool TryCombineOperations(
{
if (operations.Count == 0)
{
throw ThrowHelper.Filtering_QueryableCombinator_QueueEmpty(this);
combined = default!;
return false;
}

combined = operations.Dequeue();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"data": {
"root": [
{
"bar": true
},
{
"bar": false
}
]
}
}
54 changes: 54 additions & 0 deletions src/HotChocolate/Data/test/Data.Filters.InMemory.Tests/asd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Threading.Tasks;
using HotChocolate.Execution;
using Snapshooter;
using Snapshooter.Xunit;
using Xunit;

namespace HotChocolate.Data.Filters;

public class QueryableFilterCombinatorTests
{
private static readonly Foo[] _fooEntities = { new() { Bar = true }, new() { Bar = false } };

private readonly SchemaCache _cache = new();

[Fact]
public async Task Create_Empty_Expression()
{
// arrange
var tester = _cache.CreateSchema<Foo, FooFilterInput>(_fooEntities);

// act
// assert
var res1 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery("{ root(where: { }){ bar }}")
.Create());

res1.ToJson().MatchSnapshot(new SnapshotNameExtension("true"));
}

public class Foo
{
public int Id { get; set; }

public bool Bar { get; set; }
}

public class FooNullable
{
public int Id { get; set; }

public bool? Bar { get; set; }
}

public class FooFilterInput
: FilterInputType<Foo>
{
}

public class FooNullableFilterInput
: FilterInputType<FooNullable>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ namespace HotChocolate.Data.MongoDb
{
public abstract class MongoDbFilterDefinition : FilterDefinition<BsonDocument>
{
private static readonly MongoDbFilterDefinition _empty = new MongoDbEmptyFilterDefinition();

/// <summary>
/// Gets an empty filter. An empty filter matches everything.
/// </summary>
public static new MongoDbFilterDefinition Empty => MongoDbFilterDefinition._empty;

public abstract BsonDocument Render(
IBsonSerializer documentSerializer,
IBsonSerializerRegistry serializerRegistry);
Expand Down Expand Up @@ -52,5 +59,15 @@ public override BsonDocument Render(
return Render(documentSerializer, serializerRegistry);
}
}

internal sealed class MongoDbEmptyFilterDefinition : MongoDbFilterDefinition
{
public override BsonDocument Render(
IBsonSerializer documentSerializer,
IBsonSerializerRegistry serializerRegistry)
{
return new BsonDocument();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver;

namespace HotChocolate.Data.MongoDb.Filters
{
Expand All @@ -18,7 +16,8 @@ public static bool TryCreateQuery(

if (scope.Level.Peek().Count == 0)
{
return false;
query = MongoDbFilterDefinition.Empty;
return true;
}

query = new AndFilterDefinition(scope.Level.Peek().ToArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public override bool TryCombineOperations(
{
if (operations.Count == 0)
{
throw ThrowHelper.Filtering_MongoDbCombinator_QueueEmpty(this);
combined = default!;
return false;
}

combined = combinator switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,18 @@ async ValueTask ExecuteAsync(

Visitor.Visit(filter, visitorContext);

if (!visitorContext.TryCreateQuery(out MongoDbFilterDefinition? whereQuery) ||
visitorContext.Errors.Count > 0)
if (visitorContext.Errors.Count > 0)
{
context.Result = Array.Empty<TEntityType>();
foreach (IError error in visitorContext.Errors)
{
context.ReportError(error.WithPath(context.Path));
}
}
else
else if (visitorContext.TryCreateQuery(out MongoDbFilterDefinition? whereQuery))
{
context.LocalContextData =
context.LocalContextData.SetItem(
nameof(FilterDefinition<TEntityType>),
whereQuery);
context.LocalContextData = context.LocalContextData
.SetItem(nameof(FilterDefinition<TEntityType>), whereQuery);

await next(context).ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

<ItemGroup>
<PackageReference Include="Squadron.Mongo" Version="0.5.0" />
<PackageReference Include="MongoDB.Driver" Version="2.14.0"/>
<PackageReference Include="MongoDB.Driver" Version="2.14.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using HotChocolate.Data.Filters;
using HotChocolate.Execution;
using MongoDB.Bson.Serialization.Attributes;
using Squadron;
using Xunit;
using System;
using System.Threading.Tasks;

namespace HotChocolate.Data.MongoDb.Filters;

public class MongoDbFilterCombinatorTests
: SchemaCache
, IClassFixture<MongoResource>
{
private static readonly Foo[] _fooEntities = { new() { Bar = true }, new() { Bar = false } };

public MongoDbFilterCombinatorTests(MongoResource resource)
{
Init(resource);
}

[Fact]
public async Task Create_Empty_Expression()
{
// arrange
var tester = CreateSchema<Foo, FooFilterInput>(_fooEntities);

// act
// assert
var res1 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery("{ root(where: { }){ bar }}")
.Create());

res1.MatchDocumentSnapshot("res1");
}

public class Foo
{
[BsonId]
public Guid Id { get; set; } = Guid.NewGuid();

public bool Bar { get; set; }
}

public class FooFilterInput
: FilterInputType<Foo>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"data": {
"root": [
{
"bar": true
},
{
"bar": false
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
find({ })
6 changes: 5 additions & 1 deletion src/HotChocolate/Neo4J/src/Data/Execution/Neo4JExecutable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ public Neo4JExecutable<T> WithLimit(int limit)
/// <inheritdoc />
public INeo4JExecutable WithFiltering(CompoundCondition filters)
{
_filters = filters;
if (!filters.IsEmpty)
{
_filters = filters;
}

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public static bool TryCreateQuery(

if (scope.Level.Peek().Count == 0)
{
return false;
query = new CompoundCondition(null);
return true;
}

var conditions = new CompoundCondition(Operator.And);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public override bool TryCombineOperations(
{
if (operations.Count == 0)
{
throw ThrowHelper.Filtering_Neo4JFilterCombinator_QueueEmpty(this);
combined = default!;
return false;
}

combined = combinator switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,15 @@ async ValueTask ExecuteAsync(

Visitor.Visit(filter, visitorContext);

if (!visitorContext.TryCreateQuery(out CompoundCondition whereQuery) ||
visitorContext.Errors.Count > 0)
if (visitorContext.Errors.Count > 0)
{
context.Result = Array.Empty<TEntityType>();
foreach (IError error in visitorContext.Errors)
{
context.ReportError(error.WithPath(context.Path));
}
}
else
else if (visitorContext.TryCreateQuery(out CompoundCondition whereQuery))
{
context.LocalContextData =
context.LocalContextData.SetItem("Filter", whereQuery);
Expand Down
2 changes: 2 additions & 0 deletions src/HotChocolate/Neo4J/src/Data/Language/CompoundCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public CompoundCondition(Operator? op)
_conditions = new List<Condition>();
}

public bool IsEmpty => _conditions.Count == 0;

public override ClauseKind Kind => ClauseKind.CompoundCondition;

public new void And(Condition condition) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Threading.Tasks;
using HotChocolate.Data.Filters;
using HotChocolate.Data.Neo4J.Sorting;
using HotChocolate.Execution;
using Xunit;

namespace HotChocolate.Data.Neo4J.Filtering;

public class Neo4JFilterCombinatorTests
: IClassFixture<Neo4JFixture>
{
private readonly Neo4JFixture _fixture;

public Neo4JFilterCombinatorTests(Neo4JFixture fixture)
{
_fixture = fixture;
}

private const string _fooEntitiesCypher =
@"CREATE (:FooBool {Bar: true}), (:FooBool {Bar: false})";

[Fact]
public async Task Create_Empty_Expression()
{
// arrange
var tester =
await _fixture.GetOrCreateSchema<FooBool, FooBoolFilterType>(_fooEntitiesCypher);

// act
// assert
IExecutionResult res1 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery("{ root(where: { }){ bar }}")
.Create());

res1.MatchDocumentSnapshot("ASC");
}

public class FooBool
{
public bool Bar { get; set; }
}

public class FooBoolFilterType : FilterInputType<FooBool>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"data": {
"root": [
{
"bar": true
},
{
"bar": false
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MATCH (fooBool:FooBool) RETURN fooBool {.Bar}

0 comments on commit 6f0486c

Please sign in to comment.