Skip to content

Honor ErrorMessage in OptionsValidator generated Range attribute - #135927

Open
Laurianti wants to merge 1 commit into
dotnet:mainfrom
Laurianti:fix-options-range-errormessage
Open

Laurianti wants to merge 1 commit into
dotnet:mainfrom
Laurianti:fix-options-range-errormessage

Conversation

@Laurianti

Copy link
Copy Markdown

Contributes to #123137

The [OptionsValidator] source generator replaces [Range] with a synthesized __SourceGen__RangeAttribute. Its FormatErrorMessage always used the built-in message, so ErrorMessage, ErrorMessageResourceName and ErrorMessageResourceType were ignored.

[Range(-20, 55, ErrorMessage = "Temperature must be between -20 and 55.")]
public int Temperature { get; set; }

Before: Temperature: The field MyOptions.Temperature must be between -20 and 55.
After: Temperature: Temperature must be between -20 and 55.

The generated attribute now uses the built-in message only when none of those properties is set. Otherwise it uses ErrorMessageString, like RangeAttribute. The other synthesized attributes (MaxLength, MinLength, Length, Compare) already do this.

The nested member name reported in the issue (SubWeather.Name reported as Name) is not part of this change.

The new TestGeneratedRangeAttributeUsesErrorMessage covers a literal message, a format string, a resource message and the default message. The emitter baselines are updated.

The [OptionsValidator] source generator synthesizes its own Range
attribute. Its FormatErrorMessage always used the built-in message, so
ErrorMessage and ErrorMessageResourceName/ErrorMessageResourceType were
ignored. Use ErrorMessageString when any of them is set, as
RangeAttribute does.

Contributes to dotnet#123137
Copilot AI balanced review requested due to automatic review settings October 16, 2026 07:19
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 16, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-options
See info in area-owners.md if you want to be subscribed.

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.

🟢 Approval recommended

The narrow fix preserves default behavior, includes focused regression coverage, and has no identified blocking issues.

0 open findings

What changed in this PR

Fixes generated Options validators so [Range] honors custom and resource-based error messages while preserving default messages. Nested member naming remains out of scope.

Changes:

  • Uses inherited message resolution when customization is configured.
  • Adds regression coverage for literal, formatted, resource, and default messages.
  • Updates generated-code baselines across target frameworks and language versions.

Builds and tests were not run during this review.

Paths below are relative to src/libraries/Microsoft.Extensions.Options/.

File Description
tests/​SourceGenerationTests/​Baselines/​NetFX/​Validators.g.cs Updates .NET Framework baseline.
tests/​SourceGenerationTests/​Baselines/​NetCoreApp/​Validators.g.cs Updates .NET baseline.
tests/​SourceGeneration.Unit.Tests/​OptionsRuntimeTests.cs Adds message-customization regression coverage.
tests/​SourceGeneration.Unit.Tests/​Baselines/​OptionsExtendingSystemClassTest.netfx.g.cs Updates inheritance-test baseline.
tests/​SourceGeneration.Unit.Tests/​Baselines/​OptionsExtendingSystemClassTest.netcore.g.cs Updates inheritance-test baseline.
tests/​SourceGeneration.Unit.Tests/​Baselines/​GeneratedAttributesTest.netfx.lang11.g.cs Updates C# 11 Framework baseline.
tests/​SourceGeneration.Unit.Tests/​Baselines/​GeneratedAttributesTest.netfx.lang10.g.cs Updates C# 10 Framework baseline.
tests/​SourceGeneration.Unit.Tests/​Baselines/​GeneratedAttributesTest.netcore.lang11.g.cs Updates C# 11 .NET baseline.
tests/​SourceGeneration.Unit.Tests/​Baselines/​GeneratedAttributesTest.netcore.lang10.g.cs Updates C# 10 .NET baseline.
tests/​SourceGeneration.Unit.Tests/​Baselines/​EmitterWithCustomValidator.netfx.g.cs Updates custom-validator Framework baseline.
tests/​SourceGeneration.Unit.Tests/​Baselines/​EmitterWithCustomValidator.netcore.g.cs Updates custom-validator .NET baseline.
gen/​Emitter.cs Honors configured Range error messages.

🧠 Review effort: Balanced

This branch has not been deployed

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

Labels

area-Extensions-Options 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.

2 participants