Repository navigation
Fix SelectionExpressionBuilder failing on types with only private constructors - #9666
Closed
PHILLIPS71 wants to merge 3 commits into
Closed
PHILLIPS71 wants to merge 3 commits into
PHILLIPS71 wants to merge 3 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates HotChocolate’s projection selector generation to avoid attempting to construct result types that EF Core can materialize (notably DDD-style aggregates with non-public constructors), and adds regression tests around Selection.AsSelector<T>() behavior.
Changes:
- Adjust
SelectionExpressionBuilder.ShouldReuseExistingInstanceto treat types with private parameterless constructors as reusable (so the selector can fall back to identity). - Add an EF integration test to ensure
selection.AsSelector<T>()doesn’t throw for an aggregate with only private constructors. - Add a unit-style test asserting POCOs with a public parameterless ctor produce a
MemberInitExpression.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/HotChocolate/Data/test/Data.EntityFramework.Tests/IntegrationTests.cs |
Adds regression coverage for DDD aggregate/private ctor materialization and asserts MemberInitExpression for a POCO selector. |
src/HotChocolate/Core/src/Execution.Projections/SelectionExpressionBuilder.cs |
Broadens the “reuse existing instance” heuristic to include non-public parameterless ctors. |
Comments suppressed due to low confidence (1)
src/HotChocolate/Core/src/Execution.Projections/SelectionExpressionBuilder.cs:638
ShouldReuseExistingInstancestill requires a non-public ctor with parameters. That means a common EF/DDD pattern ofprivateparameterless ctor for materialization +publicbusiness ctor with parameters will still not hit the reuse path and can still throwNo writable properties or suitable constructor foundwhen properties have private setters and the public ctor doesn’t match the projected members. Consider broadening the second constructor check (e.g., consider public parameterized ctors too) or moving the reuse fallback to the point where member-init/ctor-matching has already failed, so these types don’t throw during selector construction.
private static bool ShouldReuseExistingInstance(Type type)
=> type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Any(c => c.GetParameters().Length == 0)
&& type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)
.Any(t =>
t.GetParameters().Length > 0
&& !IsRecordCopyConstructor(t, type));
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
# Conflicts: # src/HotChocolate/Data/test/Data.EntityFramework.Tests/IntegrationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SelectionExpressionBuilder.ShouldReuseExistingInstancehas a condition that checks for a public parameterless constructor viaType.GetConstructor(Type.EmptyTypes). This returnsnullfor types that only expose a private parameterless constructor, which is a common pattern in DDD aggregates that use a private ORM constructor alongside a business constructor that requires parameters.When the check fails, the builder falls through to the member-init and constructor-matching paths, both of which require public constructors or public writable properties, so it throws:
InvalidOperationException: No writable properties or suitable constructor found for typeEF Core supports this pattern natively (private parameterless ctor for materialisation, private setters for properties), so HotChocolate should not attempt to construct the type itself.