Throw an error when an unknown property is found in an Input API request?
I am currently passing a rest request with a JSON string as a filter to the Web API like this:
http://localhost:13825/api/values/get?filter={"Name":"abcdef","Address":"Something"}
I have a class like
Public class Customer
{
public string Name{get;set;}
public string Address{get;set;}
}
When I use the following code to parse a JSON string for a class object, it works fine,
public string Get(Customer filter)
{
}
The problem is that when I pass the filter like below,
filter={"Name":"abcdef","Adess":"Something"}
my code is assigning to null the Address of a property of the Customer class, but I wanted to throw an error when a property from a JSON string was not found in any of the class properties.
We can use MissingMemberHandling.Error , but it will throw an error when we add properties with all the properties we have defined in the class. Here the case of the problem is different, I will not skip the address and name properties every time. I may or may not go through both.
Therefore I am unable to give the required property.
I need to throw an error when an unknown property is found in the JSON input string.
source to share
You can install JsonSerializerSettings.MissingMemberHandling
on MissingMemberHandling.Error
.
Gets or sets how missing elements (for example, JSON contains a non-member property) are handled during deserialization.
If you want to do it globally, for all controllers just add this to global.asax
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Error;
If you want to do this for a specific action / controller, you need to implement IControllerConfiguration
:
public class RejectUnrecognizedPropertiesAttribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
{
controllerSettings.Formatters.Remove(controllerSettings.Formatters.JsonFormatter);
var formatter = new JsonMediaTypeFormatter
{
SerializerSettings =
{
MissingMemberHandling = MissingMemberHandling.Error
}
};
controllerSettings.Formatters.Add(formatter);
}
}
And just apply the attribute [RejectUnrecognizedProperties]
to your action / controller.
source to share
In fact, you can simply use the Required annotation for the required property in your model. Like this:
Public class Customer
{
[Required]
public string Name{get;set;}
[Required]
public string Address{get;set;}
}
Then you can do validation in action:
if (ModelState.IsValid)
{
// Do something with the Customer.
return new HttpResponseMessage(HttpStatusCode.OK);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
source to share
With JSON.net, you can define properties like this:
public class Customer
{
[JsonProperty(Required = Required.Always)]
public string Name{ get; set; }
[JsonProperty(Required = Required.Always)]
public string Address{ get; set; }
}
and then deserialize it like this:
var CustomerInstance = (Customer)JsonConvert.DeserializeObject(JSONString, typeof(Customer));
This will throw an exception if the required data is not available.
source to share