Skip to content

Skip records with no validatable members in the validations generator - #68831

Open
Arul1998 wants to merge 2 commits into
dotnet:mainfrom
Arul1998:fix/validation-gen-record-no-attributes
Open

Skip records with no validatable members in the validations generator#68831
Arul1998 wants to merge 2 commits into
dotnet:mainfrom
Arul1998:fix/validation-gen-record-no-attributes

Conversation

@Arul1998

Copy link
Copy Markdown
  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making.

Skip records with no validatable members in the validations generator

Description

The record primary-constructor path in ValidationsGenerator.TypesParser.ExtractValidatableMembers added members unconditionally, unlike the regular property path, which skips members that have no validation attributes and whose type is not itself validatable.

As a result, any record with a primary constructor was emitted as a validatable type, and every property of that record type became a validatable member of its containing types. This inflated generated code, added per-request traversal for members that can never produce an error, consumed the MaxDepth budget for nested record graphs, and made records behave differently from equivalently-shaped classes (e.g. Holder.PlainRec was a validatable member while Holder.PlainCls of identical shape was not).

This change applies the same gate to the record path: it captures the previously-discarded TryExtractValidatableType result and skips the member when neither the parameter nor the corresponding property has a validation attribute and the property type is not itself validatable. Both the parameter and the property are checked because attributes on record primary-constructor parameters bind to the parameter rather than the property by default — matching how the runtime already resolves them.

Added a test (DoesNotEmit_ForRecordWithNoValidatableMembers) asserting that plain records/classes and a mixed record are not emitted or pulled in as validatable members, while a record with [Required] on a primary-constructor parameter still is.

Fixes #68805

The record primary-constructor path in ExtractValidatableMembers added
members unconditionally, unlike the property path which skips members that
have no validation attributes and whose type is not itself validatable. As
a result any record with a primary constructor was emitted as a validatable
type, which also made every property of that record type a validatable
member of its containing types.

This inflated generated code, added per-request traversal for members that
can never produce an error, consumed the MaxDepth budget for nested record
graphs, and made records behave differently from equivalently-shaped classes.

Apply the same gate to the record path, capturing the previously-discarded
TryExtractValidatableType result and checking the parameter as well as the
corresponding property. Attributes on record primary-constructor parameters
bind to the parameter rather than the property by default, so both are
checked, matching how the runtime already resolves them.

Fixes dotnet#68805
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Aug 26, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @Arul1998. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validation source generator emits ValidatableTypeInfo for records with no validation attributes

1 participant