I implemented custom IExceptionToErrorInfoConverter for ASP.NET Boilerplate to convert custom exceptions in Web API.
Problem is that ASP.NET Boilerplate has a strict interface, that must return the ErrorInfo type:
ErrorInfo Convert(Exception exception);
The problem is that the ErrorInfo structure does not fit my requirements so I would like to have my own error DTO.
Anyone has an idea how to circumvent ASP.NET Boilerplate's exception conversion?
You can try one trick. When ASP.NET Boilerplate creates a JSON response, it might serialize the error with all available properties via reflection and wrap it into MvcAjaxResponse object with other stuff.
You can try to create your own class, derived from ErrorInfo and replace it at IExceptionToErrorInfoConverter implementation:
[Serializable]
public class MyErrorInfo : ErrorInfo
{
public string MyProperty1 { get; set; }
public int MyProperty2 { get; set; }
}
public class MyExceptionToErrorInfoConverter : IExceptionToErrorInfoConverter
{
public IExceptionToErrorInfoConverter Next { set { } }
public ErrorInfo Convert(Exception exception)
{
return new MyErrorInfo{ MyProperty1 = "test", MyProperty2 = 1};
}
}
This is my complementary information based on Slava Utesinov's answer.
Indeed internally ASP.NET Boilerplate as one would suspect uses object when dealing with serializing DTOs, therefore assumption was solid.
Some sample code from ASP.NET Boilerplate's source:
public static string ToJsonString(this object obj, bool camelCase = false, bool indented = false)
{ ... }
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{ ... }
So after success I stepped up the problem to try and hide the original ErrorInfo member. Now, knowing that ASP.NET Boilerplate uses JSON.NET I found the feature of Conditional Property Serialization. By convention implementing bool ShouldSerialize[member name]() we can instruct the serializer to ignore a property.
So I ended up with following proof of concept code:
public class ErrorInfoEx : ErrorInfo
{
public new string Details { get; set; }
public bool ShouldSerializeDetails() { return false; }
public ErrorInfoEx(int code, string message) : base(code, message) { }
public string MyField { get; set; }
}
Pleas note, for some reason you must replace base class implementation to ignore base class members.
That resulted in the following JSON, as you can see there is no 'details' property but 'myField' is present.
{
"success":false,
"result":null,
"error":
{
"myField":"123",
"code":420,
"message":"Validation failed",
"validationErrors":
[{
"message":"'Order Number' should not be empty.",
"members":["OrderNumber"]
}]
},
"unAuthorizedRequest":false
}
Related
My solution has a WebAPI project (.net core 3.1, Microsoft.AspNetCore.Mvc) and a (.Net Standard 2.1) class library that defines the data structures.
My Controller takes a post with a single parameter that deserializes mostly correctly
public class apiRequest
{
public RequestData TheData { get; set; }
public Options Options { get; set; }
public apiRequest() { }
}
The RequestData and child objects are defined i a .Net Standard 2.1 class library and added via a nuget package
public class RequestData : IRequestData
{
public int Datum{ get; set; }
...
public List<ComplexItem> ComplexItems { get; set; }
...
}
public class ComplexItem: ItemBase, IComplexItem
{
public ComplexItem() : base() { }
public ComplexItem(Pricing defaultPricing) : base(defaultPricing) { }
[JsonConstructor]
public ComplexItem(Pricing defaultPricing, Pricing selectedPricing) : base(defaultPricing, selectedPricing) { }
}
The problem I am running into is with the defaultPricing is always null when it gets to the controller
public class ItemBase : IItemBase
{
public ItemBase () { }
public ItemBase (Pricing defaultPricing)
{
DefaultPricing = defaultPricing;
}
[JsonConstructor]
public ItemBase (Pricing defaultPricing, Pricing selectedPricing)
{
DefaultPricing = defaultPricing;
SelectedPricing = selectedPricing;
}
#region Pricing
[JsonProperty]
protected Pricing DefaultPricing { get; set; }
public Pricing SelectedPricing { get; set; }
[JsonIgnore]
protected Pricing CurrentPricing
{
get { return SelectedPricing ?? DefaultPricing; }
set { SelectedPricing = value; }
}
[JsonIgnore]
public decimal Cost { get => CurrentPricing?.Cost ?? 0; }
[JsonIgnore]
public decimal Price { get => CurrentPricing?.Price ?? 0; }
#endregion
}
I've tried using [DataContract] and [DataMember] attributes, JsonObject, JsonConstructor, JsonProperty attributes and [Serializable] attribute. (Is there a current best practice on what to use?)
If I read the Json from a file and use Newtonsoft.Json.JsonConvert.DeserializeObject it deserializes correctly with the Json attributes added, but still null in the controller.
It also deserializes in the API properly if I make it public, so it doesn't seem like a problem in the Pricing class itself
After posting I found this Question about making Newtonsoft the default and using MikeBeaton's accepted solution there with Microsoft.AspNetCore.Mvc.NewtonsoftJson package worked so I'll put this as one potential answer for anyone else with this issue. Would still like to know if there is a more correct solution available.
System.Text.Json Serializes Public Properties
As the documentation implies (emphasis mine):
By default, all (read: only) public properties are serialized. You can specify properties to exclude.
I would guess that this was the design chosen because serializing an object is allowing that object to cross barriers of scope and the public scope is the only one that can reliably be assumed.
If you think about it, it makes sense. Lets say, you define a protected property and serialize the object. Then a client picks it up and deserializates that text representation into a public property. What you have designed to be an implementation detail of/to derived types is now accessible outside the scope defined by the modifier.
Apart from simply pointing you to your own answer where Newtonsoft allows this protected property to be serialized, I would suggest you look more intently at your design and why those properties are protected in the first place. It makes sense within the context of your API implementation, but the client can't (shouldn't) be assumed to follow your same inheritance structure (or support inheritance at all). It seems like you might want to define a true DTO to act as the "shape" of your API response and find the right place to transition from your internal types using protected scope to control access and the DTO that can cross the border of the API.
I receive some jsons that are supposed to represent a legal object of some class.
I wish to validate this is indeed so. So I deserialized the strings to see if this succeeds. This is very time consuming as the strings gets very large in some cases and there are many of them. I am therefore looking for a different approach.
I thought of creating a regExp from the class's definition and check that the jsons I receive are compatible. Is there a way to generate a regExp from a C# class?
Any other suggestions will help as well.
Thanks
Use JSON schema validator by newtonsoft , more details here
public class JsonSchemaController : ApiController
{
[HttpPost]
[Route("api/jsonschema/validate")]
public ValidateResponse Valiate(ValidateRequest request)
{
// load schema
JSchema schema = JSchema.Parse(request.Schema);
JToken json = JToken.Parse(request.Json);
// validate json
IList<ValidationError> errors;
bool valid = json.IsValid(schema, out errors);
// return error messages and line info to the browser
return new ValidateResponse
{
Valid = valid,
Errors = errors
};
}
}
public class ValidateRequest
{
public string Json { get; set; }
public string Schema { get; set; }
}
public class ValidateResponse
{
public bool Valid { get; set; }
public IList<ValidationError> Errors { get; set; }
}
I am writing an application where I do need to handle some scenarios beforehand in my controller class like certain property must have been provided otherwise status code would be BadRequest. Here is my class lookalike.
public class MyClass
{
[Required]
public IEnumerable<NewObject> NewObjects { get; set; }
}
public class NewObject : INewObject
{
public NewObject(string typeName, IEnumerable<Property> properties)
{
TypeName = typeName;
Properties = properties;
}
[JsonProperty(Required = Required.Always)]
public string TypeName { get; }
public IEnumerable<IProperty> Properties { get; }
}
public interface IProperty
{
string Name { get; }
object Value { get; }
}
Now though I have marked TypeName as required property and if I do not pass that in json content while sending request from swagger, json deserialization doesn't fail. I tried to search but I got an answer that setting Required to Always should work.
Below is the Json Content I am passing through swagger:
{
"NewObjects": [
{
"Properties": [
{
"Name": "string",
"Value": ''
}
]
}
]
}
I wrote below piece of code too by looking at one of the solution:
var config = new HttpConfiguration();
var jsonFormatter = config.Formatters.JsonFormatter;
jsonFormatter.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Error;
config.MapHttpAttributeRoutes();
Still it's not working:
Note: I am using Newtonsoft.Json version 11.0.1
This seems to be swagger issue because when I serialize input C# object and when again deserialize it, I am getting proper error.
For example in my controller class if I say:
var input2 = JsonConvert.DeserializeObject<MyClass>(JsonConvert.SerializeObject(input))
Then input2 throws an exception.
You can take a look at FluentValidation. If I am not mistaken it is designed to validate data in jsons forms specifically.
using FluentValidation;
public CertainActionValidator()
{
RuleFor(x => x.PropertyName).NotEmpty()
}
You can add plenty of additional conditions in there.
I have created a custom retrieve entity response class for crm2011 so as to serialize the class. The entity response class is derived from OrganizationRequest class. Its as shown below:
public partial class RetrieveEntityRequest : OrganizationRequest
{
public RetrieveEntityRequest()
{
}
private System.Guid metadataIdField;
public System.Guid MetadataId
{
get
{
return this.metadataIdField;
}
set
{
this.metadataIdField = value;
}
}
public EntityFilters EntityFilters { get; set; }
public string LogicalName { get; set; }
public bool RetrieveAsIfPublished { get; set; }
}
Now when i run the code shown below
using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
{
try
{
serviceProxy.EnableProxyTypes();
request = new CrmUtilities.RetrieveEntityRequest();
request.LogicalName=entityName;
request.EntityFilters = EntityFilters.Entity;
request.RequestName = requestName;
//Execute Request
retrieveEntityResponse = (CrmUtilities.RetrieveEntityResponse)serviceProxy.Execute(request);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
It says that MetadataId which is a required field is missing.The exception thrown is OrganizationServiceFault was caught //Required field 'MetadataId' is missing.
How do i create a metadataId for this custom object in this case?
Check out the MSDN documentation for OrganizationRequest. One of the properties is Parameters, which is a collection of all the data needed for a request to work.
Your getter and setter should set (or retrieve) the value from that collection. You can't just create a private field and expect it to work. ;)
For the record - all the other request classes available in CRM SDK follow the same pattern - they derive from OrganizationRequest and the extra properties are just shortcuts to manipulate the required Parameters.
Just guessing based on the exception your getting, because I don't know crm2011. But the exception is saying that the field is missing, what you have is a property. Whilst the difference may seem trivial there is a big difference when using reflection.
What you might need to do is:
public Guid MetadataId;
And remove your property.
I have a simple JavaScript string and object:
var name = "Scarlett Johansson";
var args = { arg1: "foo", arg2: "bar" };
And I want to pass them via $.ajax to a Web API controller:
public string Get([FromUri]TestClass input) {
// would like Args model-bound
}
And my TestClass is:
public class TestClass
{
public string Name { get; set; }
public Dictionary<string, string> Args { get; set; }
}
The Name property is bound as expected, but I haven't found a way to bind Args. I've tried JSON.stringify(args), $.param(args), using a List<KeyValuePair<string,string>> on TestClass instead of Dictionary, nothing has worked.
I was hoping I could achieve this via model binding instead of manually de-serializing the JSON. Is this possible?
Clarification: the number of keys/values would vary in "args" from call to call, hence my need for a Dictionary.
the default model binding wont work like that, it attempts to bind to public properties on objects. in this example, you would need a class containing like :
public class ArgClass
{
public string Arg1 { get; set; }
public string Arg2 { get; set; }
}
public class TestClass
{
public string Name { get; set; }
public List<ArgClass> Args { get; set; }
}
the alternative, which seems like you would want to do, is write a custom model binder, or a quick google search turns up this DefaultDictionaryBinder someone seems to have implemented already
https://github.com/loune/MVCStuff/blob/master/Extensions/DefaultDictionaryBinder.cs
Update: just realized you are using web api, which is i guess slightly different. Here's a blog post explaining how the binding works for web api: http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx
Let's extend your method with implementation (to see the result of what we've passed) like this:
public HttpResponseMessage Get([FromUri]TestClass input)
{
return Request.CreateResponse<TestClass>(HttpStatusCode.OK, input);
}
And if we would like to see this:
{
"Name":"MyName",
"Args":
{
"FirstKey":"FirstValue",
"SecondKey":"SecondValue"
}
}
Other words the testClass.Name == "MyName" and testClass.Args["FirstKey"] == "FirstValue"... we can call the API like this:
api/MyService/?name=MyName&args[0].key=FirstKey&args[0].value=FirstValue&args[1].key=SecondKey&args[1].value=SecondValue
The params on separated lines, just for clarity (URI will be without line breaks!):
api/MyService/
?name=MyName
&args[0].key=FirstKey
&args[0].value=FirstValue
&args[1].key=SecondKey
&args[1].value=SecondValue