Skip to content

Linq expression parser removes required Convert nodes #2642

@nkreipke

Description

@nkreipke

Linq expression parser removes required Convert nodes since 5.3.
Issue is reproducible with this test case:

private readonly struct GuidWrapper
{
	public readonly Guid Id;

	public GuidWrapper(Guid id)
	{
		Id = id;
	}

	public static implicit operator Guid(GuidWrapper idWrapper)
	{
		return idWrapper.Id;
	}
}

[Test]
public void CanUseImplicitCastOperatorToGuid()
{
	var id = new GuidWrapper(new Guid("{01234567-abcd-abcd-abcd-0123456789ab}"));

	// Implicit cast operator is used here, expression for "id" is something like
	// Convert(value(...).id)
	var query = session.Query<Shipper>().Where(a => a.Reference == id);

	Assert.DoesNotThrow(() =>
	{
		var _ = query.ToList();
	});
}

This used to work in 5.2. In 5.3, we get the following errors depending on the DB driver:

SQL Server:

System.InvalidCastException: Failed to convert parameter value from a GuidWrapper to a Guid.
---> System.InvalidCastException: Object must implement IConvertible.

PostgreSQL:

System.InvalidCastException: Can't write CLR type NHibernate.Test.Linq.WhereTests+GuidWrapper with handler type UuidHandler

When creating the NamedParameter, the "Convert" expression is ignored and instead the parameter is assigned the value of GuidWrapper directly. Will try to provide a PR fixing this shortly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions