diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/PostItsController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/PostItsController.cs new file mode 100644 index 0000000000..4eac4c3f11 --- /dev/null +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/PostItsController.cs @@ -0,0 +1,11 @@ +using JsonApiDotNetCore.Controllers.Annotations; +using Microsoft.AspNetCore.Mvc; + +namespace JsonApiDotNetCoreExample.Controllers; + +[ApiController] +[DisableRoutingConvention] +[Route("api/v1/postIts")] +public partial class PostItsController +{ +} diff --git a/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs b/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs index 24378e3182..b126b4d541 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs @@ -10,6 +10,7 @@ namespace JsonApiDotNetCoreExample.Data; public sealed class AppDbContext : DbContext { public DbSet TodoItems => Set(); + public DbSet PostIts => Set(); public AppDbContext(DbContextOptions options) : base(options) diff --git a/src/Examples/JsonApiDotNetCoreExample/Models/PostIt.cs b/src/Examples/JsonApiDotNetCoreExample/Models/PostIt.cs new file mode 100644 index 0000000000..3d98df7854 --- /dev/null +++ b/src/Examples/JsonApiDotNetCoreExample/Models/PostIt.cs @@ -0,0 +1,29 @@ +using System.ComponentModel.DataAnnotations; +using JsonApiDotNetCore.Resources; +using JsonApiDotNetCore.Resources.Annotations; + +namespace JsonApiDotNetCoreExample.Models; + +[Resource] +public class PostIt : Identifiable +{ + [Attr] + [Required] + public bool Actif { get; set; } + + [Attr] + [Required] + public DateTime Date { get; set; } + + [Attr] + [MaxLength(2, ErrorMessage = "Error")] + public string Message { get; set; } + + [Attr] + [MaxLength(2)] + public string OtherMessage { get; set; } + + [Attr] + [Required] + public string RefOperateur { get; set; } +} diff --git a/src/Examples/JsonApiDotNetCoreExample/Program.cs b/src/Examples/JsonApiDotNetCoreExample/Program.cs index bda826d131..469b74e255 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Program.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Program.cs @@ -3,6 +3,7 @@ using JsonApiDotNetCore.Configuration; using JsonApiDotNetCore.Diagnostics; using JsonApiDotNetCoreExample.Data; +using JsonApiDotNetCoreExample.Models; using Microsoft.AspNetCore.Authentication; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -98,5 +99,21 @@ static async Task CreateDatabaseAsync(IServiceProvider serviceProvider) await using AsyncServiceScope scope = serviceProvider.CreateAsyncScope(); var dbContext = scope.ServiceProvider.GetRequiredService(); + + await dbContext.Database.EnsureDeletedAsync(); await dbContext.Database.EnsureCreatedAsync(); + + var postIt = new PostIt + { + Id = 835405, + Actif = true, + Date = DateTime.UtcNow, + Message = "ab", + OtherMessage = "cd", + RefOperateur = "some-ref" + }; + + dbContext.PostIts.Add(postIt); + + await dbContext.SaveChangesAsync(); } diff --git a/src/JsonApiDotNetCore/Errors/UnsuccessfulActionResultException.cs b/src/JsonApiDotNetCore/Errors/UnsuccessfulActionResultException.cs index e739ec9cbf..fbecfbecd6 100644 --- a/src/JsonApiDotNetCore/Errors/UnsuccessfulActionResultException.cs +++ b/src/JsonApiDotNetCore/Errors/UnsuccessfulActionResultException.cs @@ -1,6 +1,7 @@ using System.Net; using JetBrains.Annotations; using JsonApiDotNetCore.Serialization.Objects; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace JsonApiDotNetCore.Errors; @@ -20,20 +21,35 @@ public UnsuccessfulActionResultException(HttpStatusCode status) } public UnsuccessfulActionResultException(ProblemDetails problemDetails) - : base(ToError(problemDetails)) + : base(ToErrorObjects(problemDetails)) { } - private static ErrorObject ToError(ProblemDetails problemDetails) + private static IEnumerable ToErrorObjects(ProblemDetails problemDetails) { ArgumentGuard.NotNull(problemDetails); HttpStatusCode status = problemDetails.Status != null ? (HttpStatusCode)problemDetails.Status.Value : HttpStatusCode.InternalServerError; + if (problemDetails is HttpValidationProblemDetails validationProblemDetails && validationProblemDetails.Errors.Any()) + { + foreach (string errorMessage in validationProblemDetails.Errors.SelectMany(pair => pair.Value)) + { + yield return ToErrorObject(status, validationProblemDetails, errorMessage); + } + } + else + { + yield return ToErrorObject(status, problemDetails, problemDetails.Detail); + } + } + + private static ErrorObject ToErrorObject(HttpStatusCode status, ProblemDetails problemDetails, string detail) + { var error = new ErrorObject(status) { Title = problemDetails.Title, - Detail = problemDetails.Detail + Detail = detail }; if (!string.IsNullOrWhiteSpace(problemDetails.Instance))