Skip to content

Commit 7eea69d

Browse files
committed
Suppress trimming warnings, refactor
1 parent 7cc7699 commit 7eea69d

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/Components/Forms/src/EditContextDataAnnotationsExtensions.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,17 @@ private void OnValidationRequested(object? sender, ValidationRequestedEventArgs
120120
{
121121
var validationContext = new ValidationContext(_editContext.Model, _serviceProvider, items: null);
122122

123-
if (TryValidateTypeInfo(validationContext))
123+
if (!TryValidateTypeInfo(validationContext))
124124
{
125-
_editContext.NotifyValidationStateChanged();
126-
return;
125+
ValidateWithDefaultValidator(validationContext);
127126
}
128127

128+
_editContext.NotifyValidationStateChanged();
129+
}
130+
131+
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Model types are expected to be defined in assemblies that do not get trimmed.")]
132+
private void ValidateWithDefaultValidator(ValidationContext validationContext)
133+
{
129134
var validationResults = new List<ValidationResult>();
130135
Validator.TryValidateObject(_editContext.Model, validationContext, validationResults, true);
131136

@@ -150,8 +155,6 @@ private void OnValidationRequested(object? sender, ValidationRequestedEventArgs
150155
_messages.Add(new FieldIdentifier(_editContext.Model, fieldName: string.Empty), validationResult.ErrorMessage!);
151156
}
152157
}
153-
154-
_editContext.NotifyValidationStateChanged();
155158
}
156159

157160
#pragma warning disable ASP0029 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
@@ -199,9 +202,10 @@ private bool TryValidateTypeInfo(ValidationContext validationContext)
199202
#pragma warning restore ASP0029 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
200203

201204
// TODO(OR): Replace this with a more robust implementation or a different approach. Ideally, collect references during the validation process itself.
205+
[UnconditionalSuppressMessage("Trimming", "IL2075", Justification = "Model types are expected to be defined in assemblies that do not get trimmed.")]
202206
private static object GetFieldContainer(object obj, string[] dotSegments)
203207
{
204-
// The method does not check nullity and index bounds everywhere as the path is constructed internally and assumed to be correct.
208+
// The method does not check all possiblle null access and index bound errors as the path is constructed internally and assumed to be correct.
205209
object currentObject = obj;
206210

207211
for (int i = 0; i < dotSegments.Length; i++)
@@ -224,10 +228,14 @@ private static object GetFieldContainer(object obj, string[] dotSegments)
224228
string? indexStr = match.Groups[2].Success ? match.Groups[2].Value : null;
225229

226230
Type currentType = currentObject.GetType();
227-
PropertyInfo propertyInfo = currentType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
231+
PropertyInfo propertyInfo = currentType!.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance)!;
228232
object propertyValue = propertyInfo!.GetValue(currentObject)!;
229233

230-
if (indexStr != null) // Indexed access
234+
if (indexStr == null) // Simple property access
235+
{
236+
currentObject = propertyValue;
237+
}
238+
else // Indexed access
231239
{
232240
if (!int.TryParse(indexStr, out int index))
233241
{
@@ -251,10 +259,7 @@ private static object GetFieldContainer(object obj, string[] dotSegments)
251259
throw new ArgumentException($"Property '{propertyName}' is not an array, list, or enumerable. Cannot access by index in segment '{segment}'.");
252260
}
253261
}
254-
else // Simple property access
255-
{
256-
currentObject = propertyValue;
257-
}
262+
258263
}
259264
return currentObject!;
260265
}

src/Components/Samples/BlazorUnitedApp/Validation/AddressModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
14
using System.ComponentModel.DataAnnotations;
25

36
namespace BlazorUnitedApp.Validation;

src/Components/Samples/BlazorUnitedApp/Validation/OrderItemModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
14
using System.ComponentModel.DataAnnotations;
25

36
namespace BlazorUnitedApp.Validation;

0 commit comments

Comments
 (0)