Skip to content

Commit

Permalink
Fix prefix check.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Oct 2, 2024
1 parent db99455 commit b96bc52
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
1 change: 0 additions & 1 deletion .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ jobs:
uses: docker/[email protected]
with:
load: true
build-args: "SQUIDEX__RUNTIME__VERSION=7.0.0-dev-${{ env.BUILD_NUMBER }}"
cache-from: type=gha
cache-to: type=gha,mode=max
tags: squidex-local
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ await collection.Aggregate()
PipelineDefinitionBuilder.For<MongoContentEntity>()
.Match(LookupMatch)
.Project(
BuildProjection2<MongoContentEntity>(q.Fields)),
BuildProjection<MongoContentEntity>(q.Fields)),
x => x.Joined)
.Project<IdOnly>(
Builders<IdOnly>.Projection.Include(x => x.Joined))
Expand Down Expand Up @@ -165,15 +165,15 @@ public static Task<List<StatusOnly>> FindStatusAsync(this IMongoCollection<Mongo

public static IFindFluent<T, T> SelectFields<T>(this IFindFluent<T, T> find, IEnumerable<string>? fields)
{
return find.Project<T>(BuildProjection2<T>(fields));
return find.Project<T>(BuildProjection<T>(fields));
}

public static IAggregateFluent<T> SelectFields<T>(this IAggregateFluent<T> find, IEnumerable<string>? fields)
{
return find.Project<T>(BuildProjection2<T>(fields));
return find.Project<T>(BuildProjection<T>(fields));
}

public static ProjectionDefinition<T, T> BuildProjection2<T>(IEnumerable<string>? fields)
public static ProjectionDefinition<T, T> BuildProjection<T>(IEnumerable<string>? fields)
{
var projector = Builders<T>.Projection;
var projections = new List<ProjectionDefinition<T>>();
Expand Down Expand Up @@ -210,7 +210,7 @@ static IEnumerable<string> GetDataFields(IEnumerable<string> fields)
foreach (var field in allFields)
{
// If there is at least one field that is a prefix of the current field, we cannot add that.
if (addedFields.Exists(x => field.StartsWith(x, StringComparison.Ordinal)))
if (addedFields.Exists(x => IsPrefix(field, x)))
{
continue;
}
Expand All @@ -227,5 +227,15 @@ static IEnumerable<string> GetDataFields(IEnumerable<string> fields)
}

return projector.Combine(projections);

static bool IsPrefix(string field, string prefix)
{
if (!field.StartsWith(prefix, StringComparison.Ordinal))
{
return false;
}

return field.Length == prefix.Length || field[prefix.Length] == '.';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,55 @@ public ExtensionsTests()
[Fact]
public void Should_build_projection_without_fields()
{
var projection = ExtensionSut.BuildProjection2<MongoContentEntity>(null);
var projection = ExtensionSut.BuildProjection<MongoContentEntity>(null);

AssertProjection(projection, "{ 'dd' : 0 }");
}

[Fact]
public void Should_build_projection_with_data_prefix()
{
var projection = ExtensionSut.BuildProjection2<MongoContentEntity>(["data.myField"]);
var projection = ExtensionSut.BuildProjection<MongoContentEntity>(["data.myField"]);

AssertProjection(projection, "{ '_ai' : 1, '_id' : 1, '_si' : 1, 'ai' : 1, 'cb' : 1, 'ct' : 1, 'dl' : 1, 'do.myField' : 1, 'id' : 1, 'is' : 1, 'mb' : 1, 'mt' : 1, 'ns' : 1, 'rf' : 1, 'sa' : 1, 'si' : 1, 'sj' : 1, 'ss' : 1, 'ts' : 1, 'vs' : 1 }");
}

[Fact]
public void Should_build_projection_without_data_prefix()
{
var projection = ExtensionSut.BuildProjection2<MongoContentEntity>(["myField"]);
var projection = ExtensionSut.BuildProjection<MongoContentEntity>(["myField"]);

AssertProjection(projection, "{ '_ai' : 1, '_id' : 1, '_si' : 1, 'ai' : 1, 'cb' : 1, 'ct' : 1, 'dl' : 1, 'do.myField' : 1, 'id' : 1, 'is' : 1, 'mb' : 1, 'mt' : 1, 'ns' : 1, 'rf' : 1, 'sa' : 1, 'si' : 1, 'sj' : 1, 'ss' : 1, 'ts' : 1, 'vs' : 1 }");
}

[Fact]
public void Should_build_projection_without_included_field()
{
var projection = ExtensionSut.BuildProjection2<MongoContentEntity>(["myField.special", "myField"]);
var projection = ExtensionSut.BuildProjection<MongoContentEntity>(["myField.special", "myField"]);

AssertProjection(projection, "{ '_ai' : 1, '_id' : 1, '_si' : 1, 'ai' : 1, 'cb' : 1, 'ct' : 1, 'dl' : 1, 'do.myField' : 1, 'id' : 1, 'is' : 1, 'mb' : 1, 'mt' : 1, 'ns' : 1, 'rf' : 1, 'sa' : 1, 'si' : 1, 'sj' : 1, 'ss' : 1, 'ts' : 1, 'vs' : 1 }");
}

[Fact]
public void Should_build_projection_with_prefix_field()
{
var projection = ExtensionSut.BuildProjection<MongoContentEntity>(["myField", "myField2"]);

AssertProjection(projection, "{ '_ai' : 1, '_id' : 1, '_si' : 1, 'ai' : 1, 'cb' : 1, 'ct' : 1, 'dl' : 1, 'do.myField' : 1, 'do.myField2' : 1, 'id' : 1, 'is' : 1, 'mb' : 1, 'mt' : 1, 'ns' : 1, 'rf' : 1, 'sa' : 1, 'si' : 1, 'sj' : 1, 'ss' : 1, 'ts' : 1, 'vs' : 1 }");
}

[Fact]
public void Should_build_projection_with_status_data_field()
{
var projection = ExtensionSut.BuildProjection2<MongoContentEntity>(["data.Status"]);
var projection = ExtensionSut.BuildProjection<MongoContentEntity>(["data.Status"]);

AssertProjection(projection, "{ '_ai' : 1, '_id' : 1, '_si' : 1, 'ai' : 1, 'cb' : 1, 'ct' : 1, 'dl' : 1, 'do.Status' : 1, 'id' : 1, 'is' : 1, 'mb' : 1, 'mt' : 1, 'ns' : 1, 'rf' : 1, 'sa' : 1, 'si' : 1, 'sj' : 1, 'ss' : 1, 'ts' : 1, 'vs' : 1 }");
}

[Fact]
public void Should_build_projection_with_meta_status_field()
{
var projection = ExtensionSut.BuildProjection2<MongoContentEntity>(["status"]);
var projection = ExtensionSut.BuildProjection<MongoContentEntity>(["status"]);

AssertProjection(projection, "{ '_ai' : 1, '_id' : 1, '_si' : 1, 'ai' : 1, 'cb' : 1, 'ct' : 1, 'dl' : 1, 'do.status' : 1, 'id' : 1, 'is' : 1, 'mb' : 1, 'mt' : 1, 'ns' : 1, 'rf' : 1, 'sa' : 1, 'si' : 1, 'sj' : 1, 'ss' : 1, 'ts' : 1, 'vs' : 1 }");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}

&-disabled {
color: $color-border-dark;
color: $color-text-decent;
font-size: $font-smallest;
font-weight: normal;
}
Expand Down

0 comments on commit b96bc52

Please sign in to comment.