Skip to content
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

Property with getter throwing error - option to swallow exceptions #20

Open
luboshl opened this issue Jul 26, 2024 · 3 comments
Open

Property with getter throwing error - option to swallow exceptions #20

luboshl opened this issue Jul 26, 2024 · 3 comments

Comments

@luboshl
Copy link
Owner

luboshl commented Jul 26, 2024

Some types that are going to be validated can contain properties that throw an exception.

public class MyClass
{
    public string Name => throw new InvalidOperationException();
}

By default the behavior should be similar to validation in MVC.

  • If MVC swallows that exception then MiniValidatorPlus should swallow that too.
  • If MVC throws an exception then MiniValidatorPlus should throw exception too but will add option to swallow exceptions in property getters.
@luboshl
Copy link
Owner Author

luboshl commented Jul 26, 2024

@Kraviecc, I think this explains your issue we discussed. Am I right?

@luboshl
Copy link
Owner Author

luboshl commented Jul 26, 2024

Quick investigation - MVC throws the error from the property with getter that throws an error. So we should add an option to swallow exceptions in property getters.

   at ......................
   at Microsoft.Extensions.Internal.PropertyHelper.CallNullSafePropertyGetter[TDeclaringType,TValue](Func`2 getter, Object target)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationEntry.get_Model()
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType(IValidationStrategy defaultStrategy)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitImplementation(ModelMetadata& metadata, String& key, Object model)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ObjectModelValidator.Validate(ActionContext actionContext, ValidationStateDictionary validationState, String prefix, Object model, ModelMetadata metadata, Object container)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.EnforceBindRequiredAndValidate(ObjectModelValidator baseObjectValidator, ActionContext actionContext, ParameterDescriptor parameter, ModelMetadata metadata, ModelBindingContext modelBindingContext, ModelBindingResult modelBindingResult, Object container)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value, Object container)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext()

@Kraviecc
Copy link

@Kraviecc, I think this explains your issue we discussed. Am I right?

Thanks, @luboshl. My issue comes from the fact that in our project we used Castle.Components.Validator library (https://github.com/carcer/Castle.Components.Validator/tree/master/src/Castle.Components.Validator) and we didn't want to manually mark lots of properties that weren't supposed to be validated (also these get-only) during migration to the new MiniValidation library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants