Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to #21006 - Support a default value for non-nullable properties #35746

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

maumar
Copy link
Contributor

@maumar maumar commented Mar 7, 2025

Only for scalar properties when projecting Json-mapped entity. Only need to change code for Cosmos - relational already works in the desired way after the change to streaming (properties that are not encountered maintain their default value) We still throw exception if JSON contains explicit null where non-nullable scalar is expected.

Fixes #21006

@maumar maumar force-pushed the fix21006 branch 8 times, most recently from 0ea33cb to d55392e Compare March 8, 2025 05:48
@maumar maumar marked this pull request as ready for review March 8, 2025 06:13
@maumar maumar requested a review from a team as a code owner March 8, 2025 06:13
Only for scalar properties when projecting Json-mapped entity.
Only need to change code for Cosmos - relational already works in the desired way after the change to streaming (properties that are not encountered maintain their default value)
We still throw exception if JSON contains explicit null where non-nullable scalar is expected.

Fixes #21006
@maumar
Copy link
Contributor Author

maumar commented Mar 8, 2025

This is now ready for review.

Tested (and working) scenarios:
project entity with missing required property
project entity with null-value required property
project missing required navigation
project null-value required navigation (works on Cosmos, throws validation error on relational)

tested top level and nested, as well as properties requiring converters vs no.

what doesn't work:
project missing required property directly
project null-value required property directly

to make it work users need to cast to nullable, otherwise we throw nullable object must have a value. This would be quite tricky to fix - when we project properties directly, we lose the IProperty information, so we can't easily reason with what is being projected (type doesn't provide any hints, it's always nullable - a requirement we check at the start of CreateGetValueExpression). It should be ok, given that workaround is very easy.

@maumar maumar requested a review from Copilot March 8, 2025 08:28
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

PR Overview

This PR fixes issue #21006 by ensuring that non-nullable scalar properties on JSON‐mapped entities get a proper default value when they are missing during projection. Key changes include new test scenarios for Cosmos, Sqlite, and SQL Server providers, as well as modifications to the Cosmos projection binding logic to correctly handle non-nullable scalars.

Reviewed Changes

File Description
test/EFCore.Cosmos.FunctionalTests/Query/AdHocCosmosTestHelpers.cs Introduces a helper to create entities in Cosmos with JSON payloads (note a spelling mistake in one of the error messages).
test/EFCore.Cosmos.FunctionalTests/Query/AdHocMiscellaneousQueryCosmosTest.cs Adds tests for projecting entities with missing scalar values and navigations.
test/EFCore.Sqlite.FunctionalTests/Query/AdHocJsonQuerySqliteTest.cs Adds insert scripts to cover scenarios with missing and null scalars/navigations.
test/EFCore.SqlServer.FunctionalTests/Query/AdHocJsonQuerySqlServerTestBase.cs Updates base test class to validate SQL output for various missing value cases.
test/EFCore.Relational.Specification.Tests/Query/AdHocJsonQueryRelationalTestBase.cs Revises model building and overrides tests for missing required values.
src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.CosmosProjectionBindingRemovingExpressionVisitorBase.cs Adjusts the expression visitor logic to account for non-nullable scalars.
test/EFCore.InMemory.FunctionalTests/InMemoryComplianceTest.cs Registers the new AdHocJsonQueryTestBase in the in-memory test suite.

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support a default value for non-nullable properties
1 participant