You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
For implementation of PATCH method where I need to distinguish between property being null or not set, e.g. {"key": null} vs {}.
I have a following model (see below) used to define optional json properties similar to null-able, but it can take any type.
It works for deserialization in tests, but when used inside controller method, then an InvalidOperationException is thrown before my code even runs, because ValidationVisitor from framework iterates over public properties of my model and hits Value property of instance with IsSet==false, which leads to exception.
Is there a way to tell ValidationVisitor to ignore certain property or define a custom validation property?
[JsonConverter(typeof(OptionalJsonConverter))]publicstructOptional<T>{publicstaticOptional<T>Undefined=>default(Optional<T>);privateT_value;publicOptional(Tvalue){IsSet=true;_value=value;}publicboolIsSet{get;privateset;}publicTValue{get{if(IsSet){return_value;}thrownewInvalidOperationException("The value is undefined.");}set{IsSet=true;_value=value;}}}
Here is a stack-trace (minor parts of it is in Russian)
в WebApi.Optional`1.get_Value() в D:\WebApi\Optional.cs:строка 30
в Microsoft.Extensions.Internal.PropertyHelper.CallNullSafePropertyGetterByReference[TDeclaringType,TValue](ByRefFunc`2 getter, Object target)
в Microsoft.AspNet.Mvc.ModelBinding.Validation.DefaultComplexObjectValidationStrategy.Enumerator.MoveNext()
в Microsoft.AspNet.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy)
в Microsoft.AspNet.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType()
в Microsoft.AspNet.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model)
в Microsoft.AspNet.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy)
в Microsoft.AspNet.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType()
в Microsoft.AspNet.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model)
в Microsoft.AspNet.Mvc.ModelBinding.Validation.ValidationVisitor.Validate(ModelMetadata metadata, String key, Object model)
в Microsoft.AspNet.Mvc.ModelBinding.Validation.DefaultObjectValidator.Validate(IModelValidatorProvider validatorProvider, ModelStateDictionary modelState, ValidationStateDictionary validationState, String prefix, Object model)
в Microsoft.AspNet.Mvc.Controllers.DefaultControllerActionArgumentBinder.<BindModelAsync>d__6.MoveNext()
Hello,
For implementation of
PATCHmethod where I need to distinguish between property being null or not set, e.g.{"key": null}vs{}.I have a following model (see below) used to define optional json properties similar to null-able, but it can take any type.
It works for deserialization in tests, but when used inside controller method, then an
InvalidOperationExceptionis thrown before my code even runs, becauseValidationVisitorfrom framework iterates over public properties of my model and hits Value property of instance withIsSet==false, which leads to exception.Is there a way to tell
ValidationVisitorto ignore certain property or define a custom validation property?Here is a stack-trace (minor parts of it is in Russian)