No (helpful) exception, when Delta<T> payload is invalid #917
-
Hi, consider the following code: using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData;
using Microsoft.AspNetCore.OData.Deltas;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Results;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers()
.AddOData(options =>
{
options.AddRouteComponents("odata", GetModel());
options.EnableQueryFeatures();
});
var app = builder.Build();
app.MapControllers();
app.UseODataRouteDebug();
app.Run();
static IEdmModel GetModel()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Product>("Products");
return builder.GetEdmModel();
}
public class Product
{
public int Id { get; set; }
public string? Name { get; set; }
public DateTime LastUpdated { get; set; }
}
public class ProductsController : ODataController
{
public readonly static List<Product> Products = new()
{
new() {Id = 1, Name = "Product A", LastUpdated = DateTime.Today},
new() {Id = 2, Name = "Product B", LastUpdated = DateTime.Today},
new() {Id = 3, Name = "Product C", LastUpdated = DateTime.Today},
};
[EnableQuery]
public IEnumerable<Product> Get() => Products.AsQueryable();
public Product? Get(int key) => Products.FirstOrDefault(p => p.Id == key);
public ActionResult<Product> Patch(int key, Delta<Product> delta)
{
var original = Products.FirstOrDefault(p => p.Id == key);
if (original == null) return new NotFoundODataResult("not found");
delta.Patch(original);
return original;
}
} Now, the following patch request does work fine: {
"lastUpdated": "2023-05-12T00:00:00+02:00"
} But, if the {
"lastUpdated": "2023-05-12T00:00:00"
} How can I get the validation working correctly? Or is there someting I'm missing...? Related question: Why is Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I agree with you that we be giving a more detailed error message, or providing the controller method a way to get more details about the error. Let me explore this repro you've got. It seems like the model state could be used to see why the delta is null, I'll confirm that though. |
Beta Was this translation helpful? Give feedback.
I agree with you that we be giving a more detailed error message, or providing the controller method a way to get more details about the error. Let me explore this repro you've got. It seems like the model state could be used to see why the delta is null, I'll confirm that though.