Note that there are some explanatory texts on larger screens.

plurals
  1. POExclude a type from model validation (example DbGeography) to avoid InsufficientExecutionStackException
    primarykey
    data
    text
    <p><strong>UPDATE:</strong> for the tl;dr version skip to the bottom</p> <hr> <p>I have a pretty simple subclass of JsonConverter that I'm using with Web API:</p> <pre><code>public class DbGeographyJsonConverter : JsonConverter { public override bool CanConvert(Type type) { return typeof(DbGeography).IsAssignableFrom(type); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var value = (string)reader.Value; if (value.StartsWith("POINT", StringComparison.OrdinalIgnoreCase)) { return DbGeography.PointFromText(value, DbGeography.DefaultCoordinateSystemId); } else if (value.StartsWith("POLYGON", StringComparison.OrdinalIgnoreCase)) { return DbGeography.FromText(value, DbGeography.DefaultCoordinateSystemId); } else //We don't want to support anything else right now. { throw new ArgumentException(); } } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { serializer.Serialize(writer, ((DbGeography)value).AsText()); } } </code></pre> <p>The problem is, after <code>ReadJson</code> returns the application never returns a bound object to the action method as it appears to be stuck in an infinite validation loop.</p> <p>Here's the top of the call stack when I pause execution:</p> <blockquote> <p>System.Web.Http.dll!System.Web.Http.Metadata.Providers.AssociatedMetadataProvider.GetMetadataForPropertiesImpl.AnonymousMethod__0() Line 40 C# System.Web.Http.dll!System.Web.Http.Metadata.ModelMetadata.Model.get() Line 85 C# System.Web.Http.dll!System.Web.Http.Validation.DefaultBodyModelValidator.ValidateNodeAndChildren(System.Web.Http.Metadata.ModelMetadata metadata, System.Web.Http.Validation.DefaultBodyModelValidator.ValidationContext validationContext, object container) Line 94 C# System.Web.Http.dll!System.Web.Http.Validation.DefaultBodyModelValidator.ValidateProperties(System.Web.Http.Metadata.ModelMetadata metadata, System.Web.Http.Validation.DefaultBodyModelValidator.ValidationContext validationContext) Line 156 C# System.Web.Http.dll!System.Web.Http.Validation.DefaultBodyModelValidator.ValidateNodeAndChildren(System.Web.Http.Metadata.ModelMetadata metadata, System.Web.Http.Validation.DefaultBodyModelValidator.ValidationContext validationContext, object container) Line 130 C# System.Web.Http.dll!System.Web.Http.Validation.DefaultBodyModelValidator.ValidateElements(System.Collections.IEnumerable model, System.Web.Http.Validation.DefaultBodyModelValidator.ValidationContext validationContext) Line 176 C#</p> </blockquote> <p>After that, the DefaultBodyModelValidator.Validation* pattern of calls repeats over and over and over again. Everytime I pause execution, it appears to be at about the same depth, so it doesn't appear to be getting recursively deeper.</p> <p>If I force the JsonConverter to return <code>null</code>, control returns to the API controller action method, I'm assuming because there's nothing to validate. </p> <p>I don't have the brain juices left to figure this one out. What am I doing wrong?</p> <hr> <p><strong>UPDATE:</strong> With brain juices somewhat replenished, I've stepped through most of the code and it appears that when validating the model the <code>DefaultBodyModelValidator</code> is drilling way down into the <code>SqlTypesAssembly</code> and getting stuck in a loop reading attributes somewhere. I don't really care to find out exactly where because I don't want the <code>DefaultBodyModelValidator</code> drilling into <code>DbGeography</code> type instances to start with.</p> <p>There's no reason for model validation to drill down into the <code>DbGeography</code> class. I need to figure out how to get the <code>MediaTypeFormatterCollection.IsTypeExcludedFromValidation</code> method to return true for <code>typeof(DbGeography)</code>, which will cause the <code>DefaultBodyModelValidator</code> to perform shallow validation on any <code>DbGeography</code> instances. So now the question at hand is- how do I exclude a type from model validation? The <code>ShouldValidateType</code> method of <code>DefaultBodyModelValidator</code> is marked virtual, but is there not a simple way to add an excluded type at startup?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload