Skip to content
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

CSHARP-5356: Support OfType and is with scalar discriminators when using class mapped serializers. #1559

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
CSHARP-5356: Added support for wrapped arrays as stage inputs to OfTy…
…pe and Where.
  • Loading branch information
rstam committed Nov 29, 2024
commit 0488d2f90a0c4e6cdd45f99de46614f30a3f25f2
Original file line number Diff line number Diff line change
@@ -42,9 +42,17 @@ public static AggregationExpression Translate(TranslationContext context, Method
{
var sourceExpression = arguments[0];
var sourceTranslation = ExpressionToAggregationExpressionTranslator.TranslateEnumerable(context, sourceExpression);
var itemSerializer = ArraySerializerHelper.GetItemSerializer(sourceTranslation.Serializer);
NestedAsQueryableHelper.EnsureQueryableMethodHasNestedAsQueryableSource(expression, sourceTranslation);

var sourceAst = sourceTranslation.Ast;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block of code is to support wrapped arrays as input to the stage.

var sourceSerializer = sourceTranslation.Serializer;
if (sourceSerializer is IWrappedValueSerializer wrappedValueSerializer)
{
sourceAst = AstExpression.GetField(sourceAst, wrappedValueSerializer.FieldName);
sourceSerializer = wrappedValueSerializer.ValueSerializer;
}
var itemSerializer = ArraySerializerHelper.GetItemSerializer(sourceSerializer);

var nominalType = itemSerializer.ValueType;
var nominalTypeSerializer = itemSerializer;
var actualType = method.GetGenericArguments().Single();
@@ -53,7 +61,7 @@ public static AggregationExpression Translate(TranslationContext context, Method
AstExpression ast;
if (nominalType == actualType)
{
ast = sourceTranslation.Ast;
ast = sourceAst;
}
else
{
@@ -69,7 +77,7 @@ public static AggregationExpression Translate(TranslationContext context, Method
};

ast = AstExpression.Filter(
input: sourceTranslation.Ast,
input: sourceAst,
cond: ofTypeExpression,
@as: "item");
}
Original file line number Diff line number Diff line change
@@ -40,9 +40,17 @@ public static AggregationExpression Translate(TranslationContext context, Method
{
var sourceExpression = arguments[0];
var sourceTranslation = ExpressionToAggregationExpressionTranslator.TranslateEnumerable(context, sourceExpression);
var itemSerializer = ArraySerializerHelper.GetItemSerializer(sourceTranslation.Serializer);
NestedAsQueryableHelper.EnsureQueryableMethodHasNestedAsQueryableSource(expression, sourceTranslation);

var sourceAst = sourceTranslation.Ast;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block of code is to support wrapped arrays as input to the stage.

var sourceSerializer = sourceTranslation.Serializer;
if (sourceSerializer is IWrappedValueSerializer wrappedValueSerializer)
{
sourceAst = AstExpression.GetField(sourceAst, wrappedValueSerializer.FieldName);
sourceSerializer = wrappedValueSerializer.ValueSerializer;
}
var itemSerializer = ArraySerializerHelper.GetItemSerializer(sourceSerializer);

var predicateLambda = ExpressionHelper.UnquoteLambdaIfQueryableMethod(method, arguments[1]);
var predicateParameter = predicateLambda.Parameters[0];
var predicateSymbol = context.CreateSymbol(predicateParameter, itemSerializer);
@@ -57,7 +65,7 @@ public static AggregationExpression Translate(TranslationContext context, Method
}

var ast = AstExpression.Filter(
sourceTranslation.Ast,
sourceAst,
predicateTranslation.Ast,
@as: predicateSymbol.Var.Name,
limitTranslation?.Ast);
Original file line number Diff line number Diff line change
@@ -34,7 +34,6 @@ private class B : A
{
}

[BsonDiscriminator(RootClass = true)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RootClass = true in the middle of the class hierarchy makes no sense.

This must have been an oversight.

[BsonKnownTypes(typeof(E))]
private class C : A
{
@@ -77,7 +76,7 @@ public void TestDeserializeEAsA()
{
var document = new BsonDocument
{
{ "_t", new BsonArray { "C", "E" } },
{ "_t", "E" },
{ "P", "x" }
};

@@ -86,7 +85,7 @@ public void TestDeserializeEAsA()
Assert.IsType<E>(rehydrated);

var json = rehydrated.ToJson<A>();
var expected = "{ '_t' : ['C', 'E'], 'P' : 'x' }".Replace("'", "\"");
var expected = "{ '_t' : 'E', 'P' : 'x' }".Replace("'", "\"");
Assert.Equal(expected, json);
Assert.True(bson.SequenceEqual(rehydrated.ToBson<A>()));
}
Loading