Skip to content
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So for now the asymmetry between IsKnown and IsNotKnown is not perfect right.

For any expression node, after this PR there are three possible states from the helper's perspective:

State Map has entry? Serializer type IsKnown(out s) IsNotKnown()
Truly known yes real serializer true false
Sentinel yes Unknowable/IgnoreSubtree false false
Not registered no false true

states 2 and 3 are indistinguishable from IsKnown's return value alone (both return false), but IsNotKnown distinguishes states 2 and 3. So IsKnown and IsNotKnown are not logical inverses. And your mention of having an explicit Exists/Contains should be help with this right? I would advocate to have that now rather than later.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes. This is why I suggesting to have another wider PR to introduce explicit Contains method to SerializerMap which will check if node has assigned ANY serializer, and IsKnown method should return true only for cases when there is a meaningful serializer registered.

Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,10 @@ nodeSerializer is IBsonArraySerializer arraySerializer &&
return false;
}

private bool IsKnown(Expression node) => _nodeSerializers.IsKnown(node);

private bool IsKnown(Expression node, out IBsonSerializer nodeSerializer) => _nodeSerializers.IsKnown(node, out nodeSerializer);
private bool IsKnown(Expression node, out IBsonSerializer nodeSerializer)
{
return _nodeSerializers.IsKnown(node, out nodeSerializer) && nodeSerializer is not IUnknowableSerializer and not IIgnoreSubtreeSerializer;
}
Comment thread
sanych-sun marked this conversation as resolved.

private bool IsNotKnown(Expression node) => _nodeSerializers.IsNotKnown(node);
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ static IBsonSerializer GetTargetSerializer(UnaryExpression node, Type sourceType
if (IsConvertToNullableType(targetType, out var valueType))
{
var valueSerializer = valueType == targetType ? sourceSerializer : GetTargetSerializer(node, sourceType, valueType, sourceSerializer);
return valueSerializer != null ? GetConvertToNullableTypeSerializer(node, sourceType, targetType, valueSerializer) : null;
return valueSerializer is null or IUnknowableSerializer
? valueSerializer
: GetConvertToNullableTypeSerializer(node, sourceType, targetType, valueSerializer);
}

// from here on we know there are no longer any Nullable<T> types involved
Expand Down Expand Up @@ -145,7 +147,7 @@ static IBsonSerializer GetTargetSerializer(UnaryExpression node, Type sourceType
return GetNumericConversionSerializer(node, sourceType, targetType, sourceSerializer);
}

return UnknowableSerializer.Create(node.Type);
return UnknowableSerializer.Create(targetType);
}

static IBsonSerializer GetConvertFromBsonValueSerializer(UnaryExpression expression, Type targetType)
Expand Down Expand Up @@ -210,7 +212,9 @@ static IBsonSerializer GetConvertFromNullableTypeSerializer(UnaryExpression expr
if (targetType.IsNullable(out var targetValueType))
{
var targetValueSerializer = GetTargetSerializer(expression, sourceValueType, targetValueType, sourceValueSerializer);
return NullableSerializer.Create(targetValueSerializer);
return targetValueSerializer is IUnknowableSerializer
? targetValueSerializer
: NullableSerializer.Create(targetValueSerializer);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void StartPass()

public override Expression Visit(Expression node)
{
if (IsKnown(node, out var nodeSerializer))
if (_nodeSerializers.IsKnown(node, out var nodeSerializer))
{
if (nodeSerializer is IIgnoreSubtreeSerializer or IUnknowableSerializer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using System.Linq.Expressions;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
using MongoDB.Driver.Linq.Linq3Implementation.Serializers;

Expand Down Expand Up @@ -96,12 +95,7 @@ public bool HasResultSerializer(Expression node)

public bool IsNotKnown(Expression node)
{
return !IsKnown(node);
}

public bool IsKnown(Expression node)
{
return _map.ContainsKey(node);
return !IsKnown(node, out _);
}

public bool IsKnown(Expression node, out IBsonSerializer serializer)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Driver.TestHelpers;
using FluentAssertions;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver.Linq;
using Xunit;

namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira;

public class CSharp5929Tests : LinqIntegrationTest<CSharp5929Tests.ClassFixture>
{
public CSharp5929Tests(ClassFixture fixture)
: base(fixture)
{
}

[Fact]
public void Test1()
{
var collection = Fixture.Collection;

var queryable = collection.AsQueryable()
.Select(x => x.Property[0] );

var exception = Record.Exception(() => queryable.ToList());
exception.Should().BeOfType<ExpressionNotSupportedException>();
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Before the changes it used to throw the InvalidOperationException:

System.InvalidOperationException
MongoDB.Driver.Linq.Linq3Implementation.Serializers.UnknowableSerializer`1[[System.Int64[], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] must implement IBsonArraySerializer to be used with LINQ.

}

[BsonSerializer(typeof(CSerializer))]
public class C
{
public long[] Property { get; set; }
}

public class CSerializer : IBsonDocumentSerializer, IBsonSerializer<C>
{
public Type ValueType => typeof(C);
public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) => throw new NotImplementedException();
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, C value) => throw new NotImplementedException();

C IBsonSerializer<C>.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) => throw new NotImplementedException();
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value) => throw new NotImplementedException();

public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
{
serializationInfo = null;
return false;
}
}

public sealed class ClassFixture : MongoCollectionFixture<C>
{
protected override IEnumerable<C> InitialData => null;
}
}