Skip to content

[release/11.0-preview7]: Move the abstract Validatable*Info from Microsoft.Extensions.Validation to be source-generated - #67975

Merged
wtgodbe merged 1 commit into
release/11.0-preview7from
copilot/backport-67956-to-release-11-0-preview7
Jul 23, 2026
Merged

wtgodbe merged 1 commit into
release/11.0-preview7from
copilot/backport-67956-to-release-11-0-preview7

Conversation

Copilot AI commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Backport PR #67956 to release/11.0-preview7

Backport the validation refactor from main so release/11.0-preview7 carries the same source-generated Validatable*Info model and related API shape.

Description

  • Validation model

    • Move the abstract ValidatableTypeInfo, ValidatablePropertyInfo, and ValidatableParameterInfo implementations out of the shared runtime assembly and into generated code/templates.
    • Remove now-obsolete runtime helper types that are replaced by generated equivalents.
  • Validation API updates

    • Replace the old validation error context shape with ValidationError.
    • Simplify error payloads to a single ErrorMessage per entry and keep path-based grouping at the call site.
    • Ensure IValidatableObject receives the validated instance as ValidationContext.ObjectInstance.
  • Generator and integration updates

    • Add generator emitters/templates for the moved info types and display name handling.
    • Stop emitting [NotNullWhen] on generated implementations to avoid attribute collisions in consumer compilations.
    • Update Blazor form validation and endpoint validation integration to the new generated/runtime contract.
  • Release branch alignment

    • Bring the affected Validation, Components, and Http test projects/snapshots/project references forward with the backport so the branch remains internally consistent.
public IEnumerable<ValidationResult> Validate(ValidationContext context)
{
    var model = (MyModel)context.ObjectInstance;
    // ObjectInstance is now the validated instance, not a throwaway placeholder.
    yield break;
}

Customer Impact

Keeps the preview7 branch aligned with the validation stack already merged to main, including the generated-type model and the corrected IValidatableObject behavior. This avoids divergence in API shape, generated output, and consuming test assets across branches.

Regression?

  • Yes
  • No

[If yes, specify the version the behavior has regressed from]

Risk

  • High
  • Medium
  • Low

Broad refactor touching generated code, runtime validation flow, and multiple dependent test/assets projects; low conceptual risk, but wide surface area.

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

When servicing release/2.3

  • Make necessary changes in eng/PatchConfig.props

…tion to be source-generated (#67956)

* Refactor validation

* Step 2

* Refactor

* Move to SG

* Introduce

* Rename

* Snapshots, rename

* Generated

* Namespace

* Reduce comments

* Avoid usings. Use global::

* Stable

* Remove unused usings

* Update failing snapshot tests to remove BOM and pragma

Co-authored-by: Youssef1313 <[email protected]>

* Initial plan: fix ValidationEndpointFilterFactory test failures

Co-authored-by: Youssef1313 <[email protected]>

* Fix ValidationEndpointFilterFactory test failures by restoring runtime resolver in AddValidation

Co-authored-by: Youssef1313 <[email protected]>

* Revert "Fix ValidationEndpointFilterFactory test failures by restoring runtime resolver in AddValidation"

This reverts commit 1a52a50.

Users are expected to have the source generator available; restoring the source generator reference to the test project instead.

Co-authored-by: Youssef1313 <[email protected]>

* Simplify ValidationError to a single ErrorMessage and make it a class

Address API feedback on #67960: ValidationError carried a plural Errors
list that the generator always populated with a single message, while
AddValidationError already groups errors by Path. Collapse the redundant
axis to a scalar string ErrorMessage and rely on the per-path list.

Also make ValidationError a sealed class instead of a readonly struct so
default(ValidationError) no longer bypasses the required members and hands
back null Name/Path/ErrorMessage. It is only materialized on validation
failure, so the extra allocation is not a concern.

Update the generator templates, the Blazor EditContext integration, the
minimal API endpoint filter, and all affected tests and snapshots.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 762782cd-5621-4641-8bea-ecc5675c0dbf

* Pass the validated instance to IValidatableObject and drop NotNullWhen from generated resolvers

Address PR review feedback and fix the Http.Extensions.Tests CI build break.

- ValidationContext for type-level validation now uses `value ?? _throwawayObjectInstance`
  instead of always passing the throwaway placeholder, so IValidatableObject.Validate (sync
  and async) receives the model being validated as ObjectInstance. Previously user code that
  cast ObjectInstance back to the model threw. Adds a regression test covering both paths.

- The generated file-local resolver/type-info implementations no longer emit [NotNullWhen] on
  their out parameters. The public interfaces (IValidatableInfoResolver, IValidatableTypeInfo)
  keep the attribute, so callers are unaffected, but the emitted code no longer references
  System.Diagnostics.CodeAnalysis.NotNullWhenAttribute. That reference was ambiguous (CS0433)
  in compilations where another source generator (e.g. the RequestDelegateGenerator) contributes
  an internal polyfill of the same attribute via InternalsVisibleTo, which broke the build of
  Microsoft.AspNetCore.Http.Extensions.Tests. CS8767 on the implementations is suppressed in the
  generated file.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 762782cd-5621-4641-8bea-ecc5675c0dbf

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Ondrej Roztocil <[email protected]>
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 762782cd-5621-4641-8bea-ecc5675c0dbf
@Youssef1313 Youssef1313 changed the title Backport PR #67956 to release/11.0-preview7 [release/11.0-preview7]: Move the abstract Validatable*Info from Microsoft.Extensions.Validation to be source-generated Jul 23, 2026
@Youssef1313
Youssef1313 marked this pull request as ready for review July 23, 2026 11:03
Copilot AI review requested due to automatic review settings July 23, 2026 11:03
@Youssef1313
Youssef1313 requested review from a team, BrennanConroy and halter73 as code owners July 23, 2026 11:03

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.

Copilot wasn't able to review this pull request because it exceeds the maximum number of lines (20,000). Try reducing the number of changed lines and requesting a review from Copilot again.

@wtgodbe
wtgodbe merged commit 60de750 into release/11.0-preview7 Jul 23, 2026
25 checks passed
@wtgodbe
wtgodbe deleted the copilot/backport-67956-to-release-11-0-preview7 branch July 23, 2026 15:26
@wtgodbe wtgodbe added the tell-mode Indicates a PR which is being merged during tell-mode label Jul 23, 2026
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 24, 2026
@Youssef1313 Youssef1313 added the feature-validation Issues related to model validation in minimal and controller-based APIs label Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature-validation Issues related to model validation in minimal and controller-based APIs tell-mode Indicates a PR which is being merged during tell-mode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants