Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI Custom Model binding of complex abstract object

This is a tough one. I have an issue with binding a model from JSON. I am attempting to resolve polymorphic-ally the record supplied with the type of record that it will resolve to (I want to be able to add many record types in the future). I have attempted to use the following example to resolve my model when calling the endpoint however this example only works for MVC and not Web API applications.

I have attempted to write it using IModelBinder and BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext). However I can't find the equivalent of ModelMetadataProviders in the System.Web.Http namespace.

Appreciate any help anyone can give.

I have a Web API 2 application which has the following object structure.

public abstract class ResourceRecord
{
    public abstract string Type { get; }
}

public class ARecord : ResourceRecord
{
    public override string Type
    {
        get { return "A"; }
    }

    public string AVal { get; set; }

}

public class BRecord : ResourceRecord
{
    public override string Type
    {
        get { return "B"; }
    }

    public string BVal { get; set; }
}

public class RecordCollection
{
    public string Id { get; set; }

    public string Name { get; set; }

    public List<ResourceRecord> Records { get; }

    public RecordCollection()
    {
        Records = new List<ResourceRecord>();
    }
}

JSON Structure

{
  "Id": "1",
  "Name": "myName",
  "Records": [
    {
      "Type": "A",
      "AValue": "AVal"
    },
    {
      "Type": "B",
      "BValue": "BVal"
    }
  ]
}
like image 610
garyamorris Avatar asked May 17 '16 16:05

garyamorris


People also ask

What is custom model binding in MVC?

In the MVC pattern, Model binding maps the HTTP request data to the parameters of a Controllers action method. The parameter can be of a simple type like integers, strings, double etc. or they may be complex types. MVC then binds the request data to the action parameter by using the parameter name.


2 Answers

After some research I discovered that metadata providers don't exist within WebAPI and in order to bind to complex abstract objects you have to write your own.

I started by writing a new model binding method, with the use of a custom type name JSon serializer and finally I updated my endpoint to use the custom binder. It's worth noting the following will only work with requests in the body, you will have to write something else for requests in the header. I would suggest a read of chapter 16 of Adam Freeman's Expert ASP.NET Web API 2 for MVC Developers and complex object binding.

I was able to serialize my object from the body of the request using the following code.

WebAPI configuration

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Services.Insert(typeof(ModelBinderProvider), 0,
            new SimpleModelBinderProvider(typeof(RecordCollection), new JsonBodyModelBinder<RecordCollection>()));
    }
}

Custom model binder

public class JsonBodyModelBinder<T> : IModelBinder
{
    public bool BindModel(HttpActionContext actionContext,
        ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType != typeof(T))
        {
            return false;
        }

        try
        {
            var json = ExtractRequestJson(actionContext);

            bindingContext.Model = DeserializeObjectFromJson(json);

            return true;
        }
        catch (JsonException exception)
        {
            bindingContext.ModelState.AddModelError("JsonDeserializationException", exception);

            return false;
        }


        return false;
    }

    private static T DeserializeObjectFromJson(string json)
    {
        var binder = new TypeNameSerializationBinder("");

        var obj = JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.Auto,
            Binder = binder
        });
        return obj;
    }

    private static string ExtractRequestJson(HttpActionContext actionContext)
    {
        var content = actionContext.Request.Content;
        string json = content.ReadAsStringAsync().Result;
        return json;
    }
}

Custom Serialization binding

public class TypeNameSerializationBinder : SerializationBinder
{
    public string TypeFormat { get; private set; }

    public TypeNameSerializationBinder(string typeFormat)
    {
        TypeFormat = typeFormat;
    }

    public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
    {
        assemblyName = null;
        typeName = serializedType.Name;
    }

    public override Type BindToType(string assemblyName, string typeName)
    {
        string resolvedTypeName = string.Format(TypeFormat, typeName);

        return Type.GetType(resolvedTypeName, true);
    }
}

End point definition

    [HttpPost]
    public void Post([ModelBinder(BinderType = typeof(JsonBodyModelBinder<RecordCollection>))]RecordCollection recordCollection)
    {
    }
like image 112
garyamorris Avatar answered Nov 15 '22 15:11

garyamorris


The TypeNameSerializationBinder class is not necessary anymore as well as the WebApiConfig configuration.

First, you need to create enum for record type:

public enum ResourceRecordTypeEnum
{
    a,
    b
}

Then, change your "Type" field in ResourceRecord to be the enum we just created:

public abstract class ResourceRecord
{
    public abstract ResourceRecordTypeEnum Type { get; }
}

Now you should create these 2 classes:

Model Binder

public class ResourceRecordModelBinder<T> : IModelBinder
{
    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType != typeof(T))
            return false;

        try
        {
            var json = ExtractRequestJson(actionContext);
            bindingContext.Model = DeserializeObjectFromJson(json);
            return true;
        }
        catch (JsonException exception)
        {
            bindingContext.ModelState.AddModelError("JsonDeserializationException", exception);
            return false;
        }
    }

    private static T DeserializeObjectFromJson(string json)
    {
        // This is the main part of the conversion
        var obj = JsonConvert.DeserializeObject<T>(json, new ResourceRecordConverter());
        return obj;
    }

    private string ExtractRequestJson(HttpActionContext actionContext)
    {
        var content = actionContext.Request.Content;
        string json = content.ReadAsStringAsync().Result;
        return json;
    }
}

Converter class

public class ResourceRecordConverter : CustomCreationConverter<ResourceRecord>
{
    private ResourceRecordTypeEnum _currentObjectType;

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var jobj = JObject.ReadFrom(reader);
        // jobj is the serialized json of the reuquest
        // It pulls from each record the "type" field as it is in requested json,
        // in order to identify which object to create in "Create" method
        _currentObjectType = jobj["type"].ToObject<ResourceRecordTypeEnum>();
        return base.ReadJson(jobj.CreateReader(), objectType, existingValue, serializer);
    }

    public override ResourceRecord Create(Type objectType)
    {
        switch (_currentObjectType)
        {
            case ResourceRecordTypeEnum.a:
                return new ARecord();
            case ResourceRecordTypeEnum.b:
                return new BRecord();
            default:
                throw new NotImplementedException();
        }
    }
}

Controller

[HttpPost]
public void Post([ModelBinder(BinderType = typeof(ResourceRecordModelBinder<RecordCollection>))] RecordCollection recordCollection)
{ 
}
like image 22
MasterPiece Avatar answered Nov 15 '22 14:11

MasterPiece