Repository navigation
Conversation
Backport of dotnet#64729 to release/10.0. (cherry picked from commit 7ecd485) Co-authored-by: captainsafia <[email protected]> Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
Pull request overview
Backports condition-aware [JsonIgnore] handling so write-only conditions do not bypass generated inbound validation.
Changes:
- Filters validation suppression to read-affecting ignore conditions.
- Adds endpoint tests and generated snapshot coverage.
- Includes a nit to add explicit
WhenWritingcoverage.
Show a summary per file
| File | Summary |
|---|---|
src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.ComplexType.cs |
Adds condition-specific validation tests; nit noted for explicit WhenWriting coverage. |
src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.ValidatesPropertiesWithJsonIgnoreWhenWritingConditions#ValidatableInfoResolver.g.verified.cs |
Verifies generated validatable members. |
src/Validation/gen/Extensions/ITypeSymbolExtensions.cs |
Implements condition-aware [JsonIgnore] filtering. |
Review details
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Suppressed comments (1)
src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.ComplexType.cs:630
- The new test adds an
// Arrangephase comment, but the repository review guidelines prohibit Arrange/Act/Assert comments. Remove this marker; the test setup is clear without it.
// Arrange
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Lite
|
Hi @halter73. Please make sure you've updated the PR description to use the Shiproom Template. Also, make sure this PR is not marked as a draft and is ready-to-merge. To learn more about how to prepare a servicing PR click here. |
artl93
left a comment
There was a problem hiding this comment.
Customer reported. New feature in 10.0 did not work properly. Approved.
|
Hi @halter73. This PR was just approved to be included in the upcoming servicing release. Somebody from the @dotnet/aspnet-build team will get it merged when the branches are open. Until then, please make sure all the CI checks pass and the PR is reviewed. |
Backport of #64729 to release/10.0.
Fixes #64692.
Description
Generated validation currently treats every
[JsonIgnore]as unconditional and omits the property from inbound validation. That is only correct for the default/Alwaysbehavior andWhenReading, which affect deserialization. Conditions that only affect writing (WhenWritingDefault,WhenWritingNull, andWhenWriting) andNevermust not suppress validation.This backport inspects the attribute's
Conditionand only suppresses generated validation for default/AlwaysandWhenReading. The source generator test and snapshot cover the relevant condition behavior, preserving exclusion for read-affecting conditions while validatingNeverand write-only conditions. The production change and snapshot apply unchanged to release/10.0; the test was inserted alongside two release-only tests that were added later in the same file.Customer Impact
.NET 10 apps using generated validation can silently accept invalid inbound values when a property combines validation attributes with a write-only
[JsonIgnore]condition such asWhenWritingDefaultorWhenWritingNull. The endpoint handler can run with a value that violates annotations such as[MaxLength], allowing malformed data to reach application logic instead of producing the expected400 Bad Request.Customers can remove the write-only
[JsonIgnore]condition or add explicit validation in the endpoint/application, but removing the attribute changes response serialization and explicit validation duplicates the generated validation behavior. There is no transparent configuration workaround that preserves both behaviors.Regression?
The unconditional
[JsonIgnore]handling was introduced during .NET 10 development by #63075 and first appeared in .NET 10 RC1. This scenario never worked in a final release; the first stable release containing the behavior was .NET 10.0.0.Risk
The change is confined to an internal source-generator predicate and does not alter public APIs, runtime contracts, or packaging. Existing behavior for default/
AlwaysandWhenReadingremains unchanged. Risk is mitigated by generator snapshot and endpoint coverage across both suppressed and validated condition categories, plus a manual reproduction of the reported request.Verification
Manual: built and ran the issue's Minimal API repro against this branch. Posting an overlong
PropertyWithJsonIgnoreWhenWritingDefaultreturned400with the expected[MaxLength]error; the overlong non-ignored baseline also returned400, and a valid write-only-ignored value returned200.Automated:
$env:JAVA_HOME=$null; . .\activate.ps1; dotnet test .\src\Validation\test\Microsoft.Extensions.Validation.GeneratorTests\Microsoft.Extensions.Validation.GeneratorTests.csproj --filter "FullyQualifiedName~ValidatesPropertiesWithJsonIgnoreWhenWritingConditions" --verbosity minimal— passed 1/1, failed 0, skipped 0.Packaging changes reviewed?
No packaging changes.