Skip to content

Repro for issue 1224: Validate #1225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
{
}
1 change: 1 addition & 0 deletions src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace JsonApiDotNetCoreExample.Data;
public sealed class AppDbContext : DbContext
{
public DbSet<TodoItem> TodoItems => Set<TodoItem>();
public DbSet<PostIt> PostIts => Set<PostIt>();

public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
Expand Down
29 changes: 29 additions & 0 deletions src/Examples/JsonApiDotNetCoreExample/Models/PostIt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.ComponentModel.DataAnnotations;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Resources.Annotations;

namespace JsonApiDotNetCoreExample.Models;

[Resource]
public class PostIt : Identifiable<int>
{
[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; }
}
17 changes: 17 additions & 0 deletions src/Examples/JsonApiDotNetCoreExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,5 +99,21 @@ static async Task CreateDatabaseAsync(IServiceProvider serviceProvider)
await using AsyncServiceScope scope = serviceProvider.CreateAsyncScope();

var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();

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();
}
22 changes: 19 additions & 3 deletions src/JsonApiDotNetCore/Errors/UnsuccessfulActionResultException.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ErrorObject> 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))
Expand Down