-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Fix Options Source Gen with Length attributes applied on properties of Interface type #93426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tarekgh
merged 2 commits into
dotnet:main
from
tarekgh:FixOptionsSourceGenWhenLengthAttributesAppliedonPropertiesOfInterfaceType
Oct 13, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
252 changes: 252 additions & 0 deletions
252
...on.Unit.Tests/Baselines/UsingInterfaceAsPropertyTypeForLengthAttributesTests.netcore.g.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
|
||
// <auto-generated/> | ||
#nullable enable | ||
#pragma warning disable CS1591 // Compensate for https://github.com/dotnet/roslyn/issues/54103 | ||
namespace Test | ||
{ | ||
partial class MyOptionsValidator | ||
{ | ||
/// <summary> | ||
/// Validates a specific named options instance (or all when <paramref name="name"/> is <see langword="null" />). | ||
/// </summary> | ||
/// <param name="name">The name of the options instance being validated.</param> | ||
/// <param name="options">The options instance.</param> | ||
/// <returns>Validation result.</returns> | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "42.42.42.42")] | ||
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode", | ||
Justification = "The created ValidationContext object is used in a way that never call reflection")] | ||
public global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, global::Test.MyOptions options) | ||
{ | ||
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null; | ||
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options); | ||
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>(); | ||
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1); | ||
|
||
context.MemberName = "P1"; | ||
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.P1" : $"{name}.P1"; | ||
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A1); | ||
if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.P1, context, validationResults, validationAttributes)) | ||
{ | ||
(builder ??= new()).AddResults(validationResults); | ||
} | ||
|
||
context.MemberName = "P2"; | ||
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.P2" : $"{name}.P2"; | ||
validationResults.Clear(); | ||
validationAttributes.Clear(); | ||
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A2); | ||
if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.P2, context, validationResults, validationAttributes)) | ||
{ | ||
(builder ??= new()).AddResults(validationResults); | ||
} | ||
|
||
context.MemberName = "P3"; | ||
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.P3" : $"{name}.P3"; | ||
validationResults.Clear(); | ||
validationAttributes.Clear(); | ||
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A3); | ||
if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.P3, context, validationResults, validationAttributes)) | ||
{ | ||
(builder ??= new()).AddResults(validationResults); | ||
} | ||
|
||
context.MemberName = "P4"; | ||
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.P4" : $"{name}.P4"; | ||
validationResults.Clear(); | ||
validationAttributes.Clear(); | ||
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A1); | ||
if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.P4, context, validationResults, validationAttributes)) | ||
{ | ||
(builder ??= new()).AddResults(validationResults); | ||
} | ||
|
||
context.MemberName = "P5"; | ||
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.P5" : $"{name}.P5"; | ||
validationResults.Clear(); | ||
validationAttributes.Clear(); | ||
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A2); | ||
if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.P5, context, validationResults, validationAttributes)) | ||
{ | ||
(builder ??= new()).AddResults(validationResults); | ||
} | ||
|
||
context.MemberName = "P6"; | ||
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.P6" : $"{name}.P6"; | ||
validationResults.Clear(); | ||
validationAttributes.Clear(); | ||
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A3); | ||
if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.P6, context, validationResults, validationAttributes)) | ||
{ | ||
(builder ??= new()).AddResults(validationResults); | ||
} | ||
|
||
return builder is null ? global::Microsoft.Extensions.Options.ValidateOptionsResult.Success : builder.Build(); | ||
} | ||
} | ||
} | ||
namespace __OptionValidationStaticInstances | ||
{ | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "42.42.42.42")] | ||
file static class __Attributes | ||
{ | ||
internal static readonly __OptionValidationGeneratedAttributes.__SourceGen__LengthAttribute A1 = new __OptionValidationGeneratedAttributes.__SourceGen__LengthAttribute( | ||
(int)10, | ||
(int)20); | ||
|
||
internal static readonly __OptionValidationGeneratedAttributes.__SourceGen__MinLengthAttribute A2 = new __OptionValidationGeneratedAttributes.__SourceGen__MinLengthAttribute( | ||
(int)4); | ||
|
||
internal static readonly __OptionValidationGeneratedAttributes.__SourceGen__MaxLengthAttribute A3 = new __OptionValidationGeneratedAttributes.__SourceGen__MaxLengthAttribute( | ||
(int)5); | ||
} | ||
} | ||
namespace __OptionValidationStaticInstances | ||
{ | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "42.42.42.42")] | ||
file static class __Validators | ||
{ | ||
} | ||
} | ||
namespace __OptionValidationGeneratedAttributes | ||
{ | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "42.42.42.42")] | ||
[global::System.AttributeUsage(global::System.AttributeTargets.Property | global::System.AttributeTargets.Field | global::System.AttributeTargets.Parameter, AllowMultiple = false)] | ||
file class __SourceGen__LengthAttribute : global::System.ComponentModel.DataAnnotations.ValidationAttribute | ||
{ | ||
private static string DefaultErrorMessageString => "The field {0} must be a string or collection type with a minimum length of '{1}' and maximum length of '{2}'."; | ||
public __SourceGen__LengthAttribute(int minimumLength, int maximumLength) : base(() => DefaultErrorMessageString) { MinimumLength = minimumLength; MaximumLength = maximumLength; } | ||
public int MinimumLength { get; } | ||
public int MaximumLength { get; } | ||
public override bool IsValid(object? value) | ||
{ | ||
if (MinimumLength < 0) | ||
{ | ||
throw new global::System.InvalidOperationException("LengthAttribute must have a MinimumLength value that is zero or greater."); | ||
} | ||
if (MaximumLength < MinimumLength) | ||
{ | ||
throw new global::System.InvalidOperationException("LengthAttribute must have a MaximumLength value that is greater than or equal to MinimumLength."); | ||
} | ||
if (value == null) | ||
{ | ||
return true; | ||
} | ||
|
||
int length; | ||
if (value is string stringValue) | ||
{ | ||
length = stringValue.Length; | ||
} | ||
else if (value is System.Collections.ICollection collectionValue) | ||
{ | ||
length = collectionValue.Count; | ||
} | ||
else if (value is global::System.Collections.Generic.IList<string>) | ||
{ | ||
length = ((global::System.Collections.Generic.IList<string>)value).Count; | ||
} | ||
else if (value is global::System.Collections.Generic.ICollection<string>) | ||
{ | ||
length = ((global::System.Collections.Generic.ICollection<string>)value).Count; | ||
} | ||
else | ||
{ | ||
throw new global::System.InvalidCastException($"The field of type {value.GetType()} must be a string, array, or ICollection type."); | ||
} | ||
|
||
return (uint)(length - MinimumLength) <= (uint)(MaximumLength - MinimumLength); | ||
} | ||
public override string FormatErrorMessage(string name) => string.Format(global::System.Globalization.CultureInfo.CurrentCulture, ErrorMessageString, name, MinimumLength, MaximumLength); | ||
} | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "42.42.42.42")] | ||
[global::System.AttributeUsage(global::System.AttributeTargets.Property | global::System.AttributeTargets.Field | global::System.AttributeTargets.Parameter, AllowMultiple = false)] | ||
file class __SourceGen__MaxLengthAttribute : global::System.ComponentModel.DataAnnotations.ValidationAttribute | ||
{ | ||
private const int MaxAllowableLength = -1; | ||
private static string DefaultErrorMessageString => "The field {0} must be a string or array type with a maximum length of '{1}'."; | ||
public __SourceGen__MaxLengthAttribute(int length) : base(() => DefaultErrorMessageString) { Length = length; } | ||
public __SourceGen__MaxLengthAttribute(): base(() => DefaultErrorMessageString) { Length = MaxAllowableLength; } | ||
public int Length { get; } | ||
public override string FormatErrorMessage(string name) => string.Format(global::System.Globalization.CultureInfo.CurrentCulture, ErrorMessageString, name, Length); | ||
public override bool IsValid(object? value) | ||
{ | ||
if (Length == 0 || Length < -1) | ||
{ | ||
throw new global::System.InvalidOperationException("MaxLengthAttribute must have a Length value that is greater than zero. Use MaxLength() without parameters to indicate that the string or array can have the maximum allowable length."); | ||
} | ||
if (value == null || MaxAllowableLength == Length) | ||
{ | ||
return true; | ||
} | ||
|
||
int length; | ||
if (value is string stringValue) | ||
{ | ||
length = stringValue.Length; | ||
} | ||
else if (value is System.Collections.ICollection collectionValue) | ||
{ | ||
length = collectionValue.Count; | ||
} | ||
else if (value is global::System.Collections.Generic.IList<string>) | ||
{ | ||
length = ((global::System.Collections.Generic.IList<string>)value).Count; | ||
} | ||
else if (value is global::System.Collections.Generic.ICollection<string>) | ||
{ | ||
length = ((global::System.Collections.Generic.ICollection<string>)value).Count; | ||
} | ||
else | ||
{ | ||
throw new global::System.InvalidCastException($"The field of type {value.GetType()} must be a string, array, or ICollection type."); | ||
} | ||
|
||
return length <= Length; | ||
} | ||
} | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "42.42.42.42")] | ||
[global::System.AttributeUsage(global::System.AttributeTargets.Property | global::System.AttributeTargets.Field | global::System.AttributeTargets.Parameter, AllowMultiple = false)] | ||
file class __SourceGen__MinLengthAttribute : global::System.ComponentModel.DataAnnotations.ValidationAttribute | ||
{ | ||
private static string DefaultErrorMessageString => "The field {0} must be a string or array type with a minimum length of '{1}'."; | ||
|
||
public __SourceGen__MinLengthAttribute(int length) : base(() => DefaultErrorMessageString) { Length = length; } | ||
public int Length { get; } | ||
public override bool IsValid(object? value) | ||
{ | ||
if (Length < -1) | ||
{ | ||
throw new global::System.InvalidOperationException("MinLengthAttribute must have a Length value that is zero or greater."); | ||
} | ||
if (value == null) | ||
{ | ||
return true; | ||
} | ||
|
||
int length; | ||
if (value is string stringValue) | ||
{ | ||
length = stringValue.Length; | ||
} | ||
else if (value is System.Collections.ICollection collectionValue) | ||
{ | ||
length = collectionValue.Count; | ||
} | ||
else if (value is global::System.Collections.Generic.IList<string>) | ||
{ | ||
length = ((global::System.Collections.Generic.IList<string>)value).Count; | ||
} | ||
else if (value is global::System.Collections.Generic.ICollection<string>) | ||
{ | ||
length = ((global::System.Collections.Generic.ICollection<string>)value).Count; | ||
} | ||
else | ||
{ | ||
throw new global::System.InvalidCastException($"The field of type {value.GetType()} must be a string, array, or ICollection type."); | ||
} | ||
|
||
return length >= Length; | ||
} | ||
public override string FormatErrorMessage(string name) => string.Format(global::System.Globalization.CultureInfo.CurrentCulture, ErrorMessageString, name, Length); | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.