Repository: dotnet/efcore
File: test/EFCore.SqlServer.FunctionalTests/Query/Translations/JsonTranslationsSqlServerTest.cs (C#)

Code before:
        await AssertQuery(
            ss => ss.Set<JsonTranslationsEntity>()
                .Where(b => EF.Functions.JsonContains(b.JsonString, 8, "$.OptionalInt") == 1),
            ss => ss.Set<JsonTranslationsEntity>()
                .Where(b => JsonNode.Parse(b.JsonString ?? "{}")!.AsObject().ContainsKey("OptionalInt")
                    && JsonNode.Parse(b.JsonString ?? "{}")!.AsObject()["OptionalInt"] != null
                    && ((JsonValue)JsonNode.Parse(b.JsonString ?? "{}")!.AsObject()["OptionalInt"]!).GetValue<int?>() == 8));

        AssertSql(
            """
SELECT [j].[Id], [j].[JsonString], [j].[JsonComplexType], [j].[JsonOwnedType]
FROM [JsonEntities] AS [j]
WHERE JSON_CONTAINS([j].[JsonString], 8, N'$.OptionalInt') = 1
""");
    }

    [ConditionalFact, SqlServerCondition(SqlServerCondition.SupportsJsonType)]
    public async Task JsonContains_on_complex_property()
    {
        await AssertQuery(
            ss => ss.Set<JsonTranslationsEntity>()
                .Where(b => EF.Functions.JsonContains(b.JsonComplexType, 8, "$.OptionalInt") == 1),
            ss => ss.Set<JsonTranslationsEntity>()
                .Where(b => JsonNode.Parse(b.JsonString ?? "{}")!.AsObject().ContainsKey("OptionalInt")
                    && JsonNode.Parse(b.JsonString ?? "{}")!.AsObject()["OptionalInt"] != null
                    && ((JsonValue)JsonNode.Parse(b.JsonString ?? "{}")!.AsObject()["OptionalInt"]!).GetValue<int?>() == 8));

        AssertSql(
            """
SELECT [j].[Id], [j].[JsonString], [j].[JsonComplexType], [j].[JsonOwnedType]
FROM [JsonEntities] AS [j]
WHERE JSON_CONTAINS([j].[JsonComplexType], 8, N'$.OptionalInt') = 1
""");
    }

    [ConditionalFact, SqlServerCondition(SqlServerCondition.SupportsJsonType)]
    public async Task JsonContains_on_owned_entity()
    {
        await AssertQuery(
            ss => ss.Set<JsonTranslationsEntity>()
                .Where(b => EF.Functions.JsonContains(b.JsonOwnedType, 8, "$.OptionalInt") == 1),
            ss => ss.Set<JsonTranslationsEntity>()
                .Where(b => JsonNode.Parse(b.JsonString ?? "{}")!.AsObject().ContainsKey("OptionalInt")
                    && JsonNode.Parse(b.JsonString ?? "{}")!.AsObject()["OptionalInt"] != null
                    && ((JsonValue)JsonNode.Parse(b.JsonString ?? "{}")!.AsObject()["OptionalInt"]!).GetValue<int?>() == 8));

        AssertSql(
            """
SELECT [j].[Id], [j].[JsonString], [j].[JsonComplexType], [j].[JsonOwnedType]
FROM [JsonEntities] AS [j]
WHERE JSON_CONTAINS([j].[JsonOwnedType], 8, N'$.OptionalInt') = 1

Review comment (bug):
The expected query logic is incorrect - it's checking `b.JsonString` when it should be checking `b.JsonComplexType.OptionalInt`. Since the test data has identical JSON values across all three columns, the test passes but doesn't validate the correct behavior. The expected query should be:
```
.Where(b => b.JsonComplexType.OptionalInt == 8)
```

Code after:
        await AssertQuery(
            ss => ss.Set<JsonTranslationsEntity>()
                .Where(b => EF.Functions.JsonContains(b.JsonString, 8, "$.OptionalInt") == 1),
            ss => ss.Set<JsonTranslationsEntity>()
                .Where(b => JsonNode.Parse(b.JsonString ?? "{}")!.AsObject().ContainsKey("OptionalInt")
                    && JsonNode.Parse(b.JsonString ?? "{}")!.AsObject()["OptionalInt"] != null
                    && ((JsonValue)JsonNode.Parse(b.JsonString ?? "{}")!.AsObject()["OptionalInt"]!).GetValue<int?>() == 8));

        AssertSql(
            """
SELECT [j].[Id], [j].[JsonString], [j].[JsonComplexType], [j].[JsonOwnedType]
FROM [JsonEntities] AS [j]
WHERE JSON_CONTAINS([j].[JsonString], 8, N'$.OptionalInt') = 1
""");
    }

    [ConditionalFact, SqlServerCondition(SqlServerCondition.SupportsJsonType)]
    public async Task JsonContains_on_complex_property()
    {
        await AssertQuery(
            ss => ss.Set<JsonTranslationsEntity>()
                .Where(b => EF.Functions.JsonContains(b.JsonComplexType, 8, "$.OptionalInt") == 1),
            ss => ss.Set<JsonTranslationsEntity>()
                .Where(b => b.JsonComplexType.OptionalInt == 8));

        AssertSql(
            """
SELECT [j].[Id], [j].[JsonString], [j].[JsonComplexType], [j].[JsonOwnedType]
FROM [JsonEntities] AS [j]
WHERE JSON_CONTAINS([j].[JsonComplexType], 8, N'$.OptionalInt') = 1
""");
    }

    [ConditionalFact, SqlServerCondition(SqlServerCondition.SupportsJsonType)]
    public async Task JsonContains_on_owned_entity()
    {
        await AssertQuery(
            ss => ss.Set<JsonTranslationsEntity>()
                .Where(b => EF.Functions.JsonContains(b.JsonOwnedType, 8, "$.OptionalInt") == 1),
            ss => ss.Set<JsonTranslationsEntity>()
                .Where(b => b.JsonOwnedType.OptionalInt == 8));

        AssertSql(
            """
SELECT [j].[Id], [j].[JsonString], [j].[JsonComplexType], [j].[JsonOwnedType]
FROM [JsonEntities] AS [j]
WHERE JSON_CONTAINS([j].[JsonOwnedType], 8, N'$.OptionalInt') = 1
""");
    }
#pragma warning restore EF9106 // JsonContains is experimental