Skip to content

Query: property with converter which handles nulls, can/will give wrong results when compared to null #29416

@maumar

Description

@maumar

example scenario: Json_predicate_on_nullableenumwithconverterthathandlesnulls2

property has the following converter:

                b.Property(x => x.TestNullableEnumWithConverterThatHandlesNulls).HasConversion(
                    new ValueConverter<JsonEnum?, string>(
                        x => x == null
                            ? "Null"
                            : x == JsonEnum.One
                                ? "One"
                                : x == JsonEnum.Two
                                    ? "Two"
                                    : x == JsonEnum.Three
                                        ? "Three"
                                        : "INVALID",
                        x => x == "One"
                            ? JsonEnum.One
                            : x == "Two"
                                ? JsonEnum.Two
                                : x == "Three"
                                    ? JsonEnum.Three
                                    : null,
                        convertsNulls: true));
            });

when the clr value is null, the column stores it as "Null" string. However, when we compare the property to null, we generate the following sql:

SELECT (...)
FROM [JsonEntitiesAllTypes] AS [j]
WHERE JSON_VALUE([j].[Reference],'$.TestNullableEnumWithConverterThatHandlesNulls') IS NOT NULL

we should instead pass the null value thru the converter also so instead we should generate something like this:

SELECT (...)
FROM [JsonEntitiesAllTypes] AS [j]
WHERE JSON_VALUE([j].[Reference],'$.TestNullableEnumWithConverterThatHandlesNulls') <> 'Null'

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions