Let's say I have the following input model:
public class InputModel
{
[Required]
public string? Name { get; set; }
[Required]
public DateTime? Birthday { get; set; }
}
When the birthday field is not provided then I get the following appropriate response:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-818caf3d757ae345a735fd0f4a523ecb-e9f90641c111814c-00",
"errors": {
"Birthday": [
"The Birthday field is required."
]
}
}
But if I provide an invalid date string, then the following is returned:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-715471c843155940a6f0cae580cd1b69-247e6dbfe3442446-00",
"errors": {
"model": [
"The model field is required."
],
"$.birthday": [
"The JSON value could not be converted to System.Nullable`1[System.DateTime]. Path: $.birthday | LineNumber: 13 | BytePositionInLine: 37."
]
}
}
These two response models are not consistent which makes it difficult for the client to reason about the validation errors.
How can I validate the DateTime string before it gets handled by the converter so that I can return a response model similar to the first? Something like this:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-818caf3d757ae345a735fd0f4a523ecb-e9f90641c111814c-00",
"errors": {
"Birthday": [
"The Birthday field is badly formed."
]
}
}
How can I validate the DateTime string before it gets handled by the
converter so that I can return a response model similar to the first?
You can create a custom BadRequest method that inherits ValidationProblemDetails to return the error message you want.
First, add the following code in your startup.cs ConfigureServices method:
services.AddMvc()
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = context =>
{
var problems = new CustomBadRequest(context);
return new BadRequestObjectResult(problems);
};
});
Here is the custom Bad Request method:
public class CustomBadRequest : ValidationProblemDetails
{
public CustomBadRequest(ActionContext context)
{
Title = "Invalid arguments to the API";
Detail = "The inputs supplied to the API are invalid";
Status = 400;
ConstructErrorMessages(context);
Type = context.HttpContext.TraceIdentifier;
}
private void ConstructErrorMessages(ActionContext context)
{
foreach (var keyModelStatePair in context.ModelState)
{
var key = keyModelStatePair.Key.Replace("$.", "");
var errors = keyModelStatePair.Value.Errors;
if (errors != null && errors.Count > 0)
{
if (errors.Count == 1)
{
var errorMessage = GetErrorMessage(key, errors[0]);
Errors.Add(key, new[] { errorMessage });
}
else
{
var errorMessages = new string[errors.Count];
for (var i = 0; i < errors.Count; i++)
{
errorMessages[i] = GetErrorMessage(key,errors[i]);
}
Errors.Add(key, errorMessages);
}
}
}
}
string GetErrorMessage(string key, ModelError error)
{
if (error.ErrorMessage != $"The {key} field is required.")
{
return $"The {key} field is badly formed.";
}
return error.ErrorMessage;
}
}
After the above settings, when your InputModel is illegal, it will automatically enter the CustomBadRequest method and return the corresponding error message through judgment.
Here is the test result through postman:
Try defining the datatype of the datetime field like as follows:
[Required]
[DataType(DataType.DateTime)]
public DateTime? Birthday { get; set; }
Related
When calling an HTTP get method, the query parameters are not correctly filled in.
Let me explain with the code.
[HttpGet]
public async Task<IActionResult> GetTasks(Guid propertyId, [FromQuery] TaskFilterParams filterParams, [FromQuery] SortingParams sortingParams, [FromQuery] PagingParams pagingParams)
{
// Do some stuff, not important
}
So we call this endpoint with the following url: someurl.com/api/v1/properties/[some-guid]/tasks?&roomClass=Single&category=Cleaning
As you can see, we only have provided three values, a GUID, a type of room and the category of the task. The sorting and paging parameters are empty.
The problem is that the 'category' parameter is not filled in. It is always null, while the other parameters are filled in correctly. No matter what we try (url encoding, re-arranging the order of the parameters, ...).
The real catch is however, if we restart the service, it sometimes works for a while. But every so often it fails and we need to restart evertything again.
Has anyone encountered the same issue? Or, even better, a solution?
PS: The code is running in a docker container in AKS (FROM mcr.microsoft.com/dotnet/core/aspnet:3.1).
[DBG] Get tasks parameters: PropertyId: [some-guid], TaskFilterParams:{"Category": null, "RoomClass": {"Value": "Single", "Operator": "Equal", "$type": "FilterParam`1"}, "$type": "TaskFilterParams"}
public class TaskFilterParams
{
public FilterParam<IssueCategory> Category { get; set; }
public FilterParam<RoomClass> RoomClass { get; set; }
public FilterParam<Guid?> Room { get; set; }
}
public abstract class FilterParam
{
public FilterOperator Operator { get; set; }
}
[TypeDescriptionProvider(typeof(FilterParamTypeDescriptionProvider))]
[JsonObject]
public class FilterParam<T> : FilterParam
{
public T Value { get; set; }
public static bool TryParse(string s, out FilterParam<T> result)
{
result = default(FilterParam<T>);
var filterOperators = Enum.GetValues(typeof(FilterOperator)).Cast<FilterOperator>();
foreach (var filterOperatorString in filterOperators.Select<FilterOperator, string>(x => x.ToFilterOperatorString()).OrderByDescending(x => x.Length))
{
if (s.StartsWith(filterOperatorString))
{
if (s.TryParse(filterOperatorString, out result))
{
return true;
}
return false;
}
}
return false;
}
}
public enum IssueCategory
{
Cleaning = 0,
Inspection = 1,
Maintenance = 2
}
public enum RoomClass
{
Single = 0,
Double = 1,
Family = 2
}
UPDATE:
We have added more logging. It seems the data is present in the query string but the model is not binding correctly.
Log.Debug("Query: {#query}", HttpContext.Request.Query);
Log.Debug(
"Get tasks parameters: PropertyId:{#propertyId}, TaskFilterParams:{#filterParams}, SortingParams:{#sortingParams}, PagingParams:{#pagingParams}",
propertyId, filterParams, sortingParams, pagingParams);
[16:51:35 DBG] Query: [{"Key": "category", "Value": ["Cleaning"], "$type": "KeyValuePair`2"}, {"Key": "roomClass", "Value": ["Single"], "$type": "KeyValuePair`2"}]
[16:51:35 DBG] Get tasks parameters: PropertyId:[some-guid], TaskFilterParams:{"Category": null, "RoomClass": {"Value": "Single", "Operator": "Equal", "$type": "FilterParam`1"}, "$type": "TaskFilterParams"}
I have a web api in .net core3.1 and I am using swagger.
One of my request data class is as below.
public class SNMPv1ReqData{
[JsonProperty("snmpV1Info")]
[MinLength(1)]
[MaxLength(2000)]
[Required]
public List<SNMPv1Info> SNMPv1InfoLst { get; set; }
}
public class SNMPv1Info{
[JsonProperty("host")]
[Required]
public string HostName { get; set; }
[JsonProperty("snmpV1Setting")]
public SNMPv1Setting SNMPv1Setting { get; set; }
[JsonProperty("oid")]
[MinLength(1)]
[MaxLength(1000)]
[Required]
public string[] OID { get; set; }
}
In swagger request is shown as below as it is mentioned in JsonProperty("PropertyName")
{
"snmpV1Info": [
{
"host": "string",
"snmpV1Setting": {
"retryCount": 0,
"timeout": 0,
"port": 0,
"communityName": "string"
},
"oid": [
"string"
]
}
]
}
But when I send request it is showing the below error.
{
"title": "One or more validation errors occurred.",
"status": 400,
"errors": {
"SNMPv1InfoLst": [
"The SNMPv1InfoLst field is required."
]
}
}
SNMPv1InfoLst is variable name but I want to use "snmpV1Info" in api request which is mentioned in JsonProperty("snmpV1Info") also showing "snmpV1Info" in swagger request.
Startup.cs
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1.0.0", new Microsoft.OpenApi.Models.OpenApiInfo
{
Title = "WebAPI",
Version = "v1.0",
Description = "Edge ASP.NET Core Web API",
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
services.AddSwaggerGenNewtonsoftSupport();
Swashbuckle.AspNetCore : 5.6.3
Swashbuckle.AspNetCore.Newtonsoft : 5.6.3
.Net Core3.1
I am not able to find the cause of this problem. Can anyone help me ?
I have a JSON response that I would like to parse using JSON.NET. I have done this with single values before but never when the response could contain an object that consist of an array as the errors property does below.
{
"code": "InvalidObject",
"message": "payment object is invalid",
"errors": [
{
"code": "AccountingApi",
"message": "Paid amount cannot be greater than the amount of the invoice.",
"resource": "payment",
"field": "amount"
},
{
"code": "AccountingApi",
"message": "Payment has not been verified",
"resource": "payment",
"field": "verification"
}
]
}
I would like to extract the error messages into a List. How do I specify that I want to grab the message property in the errors collection?
List<string> errorMessages = parsedJson["errors"].ToList<string>();
You could use
class Error
{
public string code { get; set; }
public string message { get; set; }
public string resource { get; set; }
public string field { get; set; }
}
class Some
{
public string code { get; set; }
public string message { get; set; }
public List<Error> errors { get; set; }
}
Then (Probably you'll send your json string as param )
List<string> parse()
{
var s = new StringBuilder();
s.Append("{");
s.Append(" \"code\": \"InvalidObject\",");
s.Append("\"message\": \"payment object is invalid\",");
s.Append("\"errors\": [");
s.Append("{");
s.Append("\"code\": \"AccountingApi\",");
s.Append("\"message\": \"Paid amount cannot be greater than the amount of the invoice.\",");
s.Append("\"resource\": \"payment\",");
s.Append("\"field\": \"amount\"");
s.Append("},");
s.Append("{");
s.Append("\"code\": \"AccountingApi\",");
s.Append("\"message\": \"Payment has not been verified\",");
s.Append("\"resource\": \"payment\",");
s.Append("\"field\": \"verification\" ");
s.Append("}");
s.Append("]");
s.Append("}");
var json = s.ToString();
var obj = JsonConvert.DeserializeObject<Some>(json);
return obj.errors.Select(x => x.message).ToList();
}
I have a model (myModel) that has the following data annotation on myProperty
[Required(ErrorMessage = "myProperty is required.")]
In the api controller we are validating the model as follows:
if (!ModelState.IsValid)
{
var errorResponse = new HttpRequestMessage()
.CreateErrorResponse(HttpStatusCode.BadReques, ModelState);
throw new HttpResponseException(errorResponse);
}
What I get back in postman is:
{
"Message": "The request is invalid.",
"ModelState": {
"myModel": [
"An error has occurred."
]
}
}
I would like to get back the error message from the data annotation.
You can get the error messages from the model state validation from the ModelState.Errors property, it's a collection of ModelError.
MSDN documentation
For Example your model should like this
[Required]
[DataType(DataType.EmailAddress)]
[EmailAddress]
public string EmailAddress { get; set; }
And your in your action method
if (!ModelState.IsValid) {
var error = ModelState.Where(e => e.Value.Errors.Count > 0).Select(e => new { Name = e.Key, Message = e.Value.Errors.First().ErrorMessage }).ToList();
return Request.CreateResponse(HttpStatusCode.BadRequest, new Dictionary<string, object>()
{
{ "ErrorList", error }
});
}
Following is the Model class that I use for the application
public class APIVesselFilter
{
[Required(ErrorMessage = "Vessel is required")]
public Guid VesselID { get; set; }
public Guid? AnchorageID { get; set; }
}
Following is the Validation Filter that will check if the ModelState is Valid and if not valid I will send the error message.
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
List<string> errorList = new List<string>();
foreach (var err in actionContext.ModelState.Values)
{
foreach (var er in err.Errors)
{
errorList.Add(er.ErrorMessage);
}
}
actionContext.Response = actionContext.Request.CreateResponse(APIResponse.SendResponse(RequestStatus.GetValidationFailedMessage(), actionContext.ModelState));
}
}
}
Here, Following is the response that I get using the above Model State. Over here the error message is not in a user understandable way and so I have added the ErrorMessage in Require attribute of Vessel and I loop through the errors in ModelState. But my error message is always empty (I checked this using a debugger). What am I missing here so that the error message will be bound directly to the ModelState?
{
"Status": {
"StatusCode": 620,
"StatusMessage": "Validation Failed"
},
"Data": {
"filter": {
"_errors": [
{
"<Exception>k__BackingField": {
"ClassName": "Newtonsoft.Json.JsonSerializationException",
"Message": "Required property 'VesselID' not found in JSON. Path '', line 1, position 56.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EndObject(Object newObject, JsonReader reader, JsonObjectContract contract, Int32 initialDepth, Dictionary`2 propertiesPresence)",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": "8\nEndObject\nNewtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed\nNewtonsoft.Json.Serialization.JsonSerializerInternalReader\nVoid EndObject(System.Object, Newtonsoft.Json.JsonReader, Newtonsoft.Json.Serialization.JsonObjectContract, Int32, System.Collections.Generic.Dictionary`2[Newtonsoft.Json.Serialization.JsonProperty,Newtonsoft.Json.Serialization.JsonSerializerInternalReader+PropertyPresence])",
"HResult": -2146233088,
"Source": "Newtonsoft.Json",
"WatsonBuckets": null
},
"<ErrorMessage>k__BackingField": ""
}
],
"<Value>k__BackingField": null
}
}
}
Try decorating your model property with a DisplayAttribute:
public class APIVesselFilter
{
[Required]
[Display(Name="Vessel is required")]
public Guid VesselID { get; set; }
public Guid? AnchorageID { get; set; }
}
I know this is the way to customise your messages when using the Html.ValidationSummary and a quick test showed this comes up when inspecting the ModelState in an action.
using CreateErrorResponse instead of CreateResponse might solve your problem.
I faced a smiliar problem and was fixed with that.
In your case this will be
actionContext.Response = actionContext.Request.CreateErrorResponse(APIResponse.SendResponse(RequestStatus.GetValidationFailedMessage(), actionContext.ModelState));