Skip to content

Cast to the closed ValueConverter<,> in the precompiled shaper - #39236

Merged
AndriySvyryd merged 2 commits into
dotnet:mainfrom
Yusuf-Hussien:fix/39174-precompiled-query-converter-cast
Oct 15, 2026
Merged

AndriySvyryd merged 2 commits into
dotnet:mainfrom
Yusuf-Hussien:fix/39174-precompiled-query-converter-cast

Conversation

@Yusuf-Hussien

Copy link
Copy Markdown
Contributor

Fixes #39174

The relational shaper cast the type mapping's converter to the design-time converter class when materializing a property that requires a conversion. When the model is a compiled model, the converter is reconstructed as the base ValueConverter<TModel, TProvider> (the subclass is lost), so the generated cast to the subclass threw InvalidCastException at run time — under the JIT and in native AOT alike.

Cast to the closed ValueConverter<,> the converter implements (the typedConverterType already computed at each site) instead. This is what the compiled model reconstructs, and it remains valid for run-time models, where the converter instance is assignable to the closed generic.

Checklist

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow the format described in the template
  • Tests were added
  • No breaking changes

Tests

Added AdHocPrecompiledQueryRelationalTestBase.Projecting_entity_with_value_converter_subclass_does_not_cast_to_the_subclass. It uses a ValueConverter<TModel, TProvider> subclass with distinct model and provider types (Rating → int) and asserts that the generated interceptor no longer casts the type mapping's converter to the design-time subclass.

Verification against the SQLite provider:

  • Fails on main — the generated interceptor contains ((...PrecompiledContext39174.RatingConverter)(((IReadOnlyProperty)ratingProperty).GetTypeMapping().Converter)).ConvertFromProviderTyped(...), which throws InvalidCastException when the model is a compiled model.
  • Passes with this change.
  • AdHocPrecompiledQuerySqliteTest: 28/28.
  • PrecompiledQuerySqliteTest: 124 passed, 4 skipped.
  • CompiledModelSqliteTest: 16/16.

- The relational shaper cast the type mapping's converter to the design-time
  converter class when materializing a property that requires a conversion.
- A compiled model reconstructs the converter as the base
  ValueConverter<TModel, TProvider> (the subclass is lost), so the generated
  cast to the subclass threw InvalidCastException at run time, under the JIT
  and in native AOT alike.
- Cast to the closed ValueConverter<,> the converter implements
  (typedConverterType) instead, which is what the compiled model
  reconstructs and which remains valid for run-time models.

Fixes dotnet#39174
Copilot AI balanced review requested due to automatic review settings October 12, 2026 22:49
@Yusuf-Hussien
Yusuf-Hussien requested a review from a team as a code owner October 12, 2026 22:49

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Yusuf-Hussien

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

Copilot AI left a comment

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.

🟡 Changes recommended

The new virtual specification test lacks the SQL Server override required by its override guard.

1 open finding

🧠 Review effort: Balanced

AdHocPrecompiledQuerySqlServerTest.Check_all_tests_overridden fails if a base
test is not overridden, so add the SqlServer override with its SQL baseline.
Copilot AI balanced review requested due to automatic review settings October 14, 2026 05:34

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

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.

🔵 Needs a closer look

The separate JSON-null converter path still needs focused precompiled-query regression coverage.

0 open findings

1 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Medium severity Add compiled JSON nullable-property converter cast regression test

src/​EFCore.Relational/​Query/​RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs:3435

The new test covers a regular column, but this is a separate converter cast for a nullable property inside a JSON-mapped entity whose converter handles nulls. A subclass cast emitted here would still fail against the base converter reconstructed by a compiled model, while the added test would pass. Please add a precompiled JSON-property test with a ValueConverter<,> subclass using convertsNulls: true, a JSON null token, and an interceptor-code assertion that the subclass cast is absent.

🧠 Review effort: Lite

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Precompiled query throws InvalidCastException materializing a property whose value converter is a ValueConverter subclass

3 participants