Repository navigation
Conversation
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
|
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. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-extensions-options |
Contributor
There was a problem hiding this comment.
🟢 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 was referenced Oct 16, 2026
Closed
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contributes to #123137
The
[OptionsValidator]source generator replaces[Range]with a synthesized__SourceGen__RangeAttribute. ItsFormatErrorMessagealways used the built-in message, soErrorMessage,ErrorMessageResourceNameandErrorMessageResourceTypewere ignored.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, likeRangeAttribute. The other synthesized attributes (MaxLength,MinLength,Length,Compare) already do this.The nested member name reported in the issue (
SubWeather.Namereported asName) is not part of this change.The new
TestGeneratedRangeAttributeUsesErrorMessagecovers a literal message, a format string, a resource message and the default message. The emitter baselines are updated.