Skip to content

Fix/selectfacet derived and global namespace - #452

Merged
Tim-Maes merged 2 commits into
masterfrom
fix/selectfacet-derived-and-global-namespace
Oct 30, 2026
Merged

Tim-Maes merged 2 commits into
masterfrom
fix/selectfacet-derived-and-global-namespace

Conversation

@Tim-Maes

Copy link
Copy Markdown
Owner

1. SelectFacet / EF *FacetAsync with a derived source type (src/Facet.Extensions/FacetExtensions.cs)

SelectFacet<TSource, TTarget> hard-cast the declared projection to Expression<Func<TSource, TTarget>>. When TSource derives from the facet's source, for example an EF table-per-hierarchy subtype like db.Set<Admin>().ToFacetsAsync<Admin, UserDto>() with [Facet(typeof(User))], that cast threw InvalidCastException. Every EF *FacetAsync overload with explicit type arguments goes through this method.

It now reuses the non-generic overload's path: the cached declared projection, with the parameter adapted by a Convert when TSource is a subtype. EF translates that cast for TPH; the Sqlite tests run real SQL. For a declared source, the projection is used unchanged.

Bonus: this overload used to reflect on every query, and now uses the existing cache.
Multi-source facets looked up ProjectionFrom{TSource.Name}, which doesn't exist for a subtype. The lookup now uses the declared source the element type derives from. This also fixes the non-generic SelectFacet<TTarget>().

The error message, when there really is no projection, now names the source type.

2. [Facet] of a type in the global namespace (src/Facet/Generators/FacetGenerators/CodeGenerationHelpers.cs)

A type declared without a namespace has the fully qualified name global::User, which has no dot. GetSimpleTypeName therefore returned it whole, and the generated members read ProjectionFromglobal::User (CS0687). Any facet of such a type failed to compile, and top-level-statement and minimal API apps hit this first. The global:: prefix is now stripped before splitting; names of namespaced types are unchanged.

Tim-Maes and others added 2 commits October 30, 2026 11:02
SelectFacet<TSource, TTarget> hard-cast the facet's declared projection to
Expression<Func<TSource, TTarget>>, so querying a subtype of the declared
source threw InvalidCastException, e.g. an EF table-per-hierarchy
db.Set<Admin>().ToFacetsAsync<Admin, UserDto>(). Every EF *FacetAsync
overload with explicit type arguments goes through it. It now reuses the
non-generic overload's path: the cached declared projection, with the
parameter adapted by a cast when TSource derives from the declared source.

Multi-source facets looked up ProjectionFrom{TSource.Name}, which misses for
a subtype in both overloads; the lookup now uses the declared source the
element type derives from.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A type declared without a namespace has the fully qualified name
global::Name, which has no dot, so GetSimpleTypeName returned it whole.
Generated member names then read ProjectionFromglobal::User (CS0687), and
any [Facet] of such a type failed to compile - the first thing a
top-level-statement or minimal API app runs into. The global:: prefix is
now stripped first; namespaced names are unchanged.

Adds driver-based tests compiling global-namespace facets (single-source,
multi-source, facet in a namespace) and GenerateDtos, plus CHANGELOG
entries for this and the SelectFacet derived-type fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Pull requests that contains a bugfix Cleanup Clean up obsolete, redundant code. enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant