Skip to content

[release/10.0] Fix JsonIgnore validation bypass for write-only conditions - #68762

Merged
wtgodbe merged 1 commit into
dotnet:release/10.0from
halter73:halter73-jsonignore-validation-backport
Sep 3, 2026
Merged

wtgodbe merged 1 commit into
dotnet:release/10.0from
halter73:halter73-jsonignore-validation-backport

Conversation

@halter73

@halter73 halter73 commented Aug 25, 2026 •

Copy link
Copy Markdown
Member

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/Always behavior and WhenReading, which affect deserialization. Conditions that only affect writing (WhenWritingDefault, WhenWritingNull, and WhenWriting) and Never must not suppress validation.

This backport inspects the attribute's Condition and only suppresses generated validation for default/Always and WhenReading. The source generator test and snapshot cover the relevant condition behavior, preserving exclusion for read-affecting conditions while validating Never and 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 as WhenWritingDefault or WhenWritingNull. 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 expected 400 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?

  • Yes
  • No

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

  • High
  • Medium
  • Low

The change is confined to an internal source-generator predicate and does not alter public APIs, runtime contracts, or packaging. Existing behavior for default/Always and WhenReading remains 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 (required)
  • Automated

Manual: built and ran the issue's Minimal API repro against this branch. Posting an overlong PropertyWithJsonIgnoreWhenWritingDefault returned 400 with the expected [MaxLength] error; the overlong non-ignored baseline also returned 400, and a valid write-only-ignored value returned 200.

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?

  • Yes
  • No
  • N/A

No packaging changes.

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]>
Copilot AI lite review requested due to automatic review settings August 25, 2026 02:17

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.

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 WhenWriting coverage.
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 // Arrange phase 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

@halter73 halter73 added the Servicing-consider Shiproom approval is required for the issue label Aug 25, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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.

@halter73
halter73 requested a review from BrennanConroy August 25, 2026 02:52
@halter73
halter73 requested a review from artl93 September 1, 2026 23:39

@artl93 artl93 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.

Customer reported. New feature in 10.0 did not work properly. Approved.

@Youssef1313 Youssef1313 added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc Servicing-approved Shiproom has approved the issue and removed Servicing-consider Shiproom approval is required for the issue labels Sep 2, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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.

@wtgodbe
wtgodbe merged commit d4fbd2a into dotnet:release/10.0 Sep 3, 2026
28 checks passed
@rbhanda rbhanda added this to the 10.0.13 milestone Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc Servicing-approved Shiproom has approved the issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants