Skip to content

Poor error response when using ModelState Validation with [ApiController] #1224

Closed
@Alex-Doms

Description

@Alex-Doms

SUMMARY

Hello, i try to add some data annotations on my resources. On my first tests, the ValidateModelState seems fired well when an attribute is not correct, but in the response, i don't have any details.

STEPS TO REPRODUCE

Resource :

 public class PostIt : Identifiable<int>
    {
        
        [Attr]
        [Required]
        public bool Actif { get; set; }

        [HasOne]
        public DPIDentaire DPIDentaire { get; set; }
        [Attr,Required]
        public DateTime Date { get; set; }

        [Attr]
        [MaxLength(2, ErrorMessage ="Error")] 
        public string Message { get; set; }

        [Attr]
        [Required]
        public string RefOperateur { get; set; }


    }

Request :

curl --location --request PATCH 'http://localhost/v1/post-its/835405' \
--header 'Content-Type: application/vnd.api+json' \
--header 'Authorization: Bearer token' \
--data-raw '{
    "data": {
        "type": "postIts",
        "id": "835405",
        "attributes": {
            "actif": false,
            "message": "HELLO POST IT",
            "refOperateur": "1"
        }
}
}'

Response :

{
    "links": {
        "self": "http://localhost/WS_ApiGalaxie/v1/post-its/835405"
    },
    "errors": [
        {
            "id": "1af709c0-ccfa-45ec-8f75-7fc7470e7fc4",
            "links": {
                "about": "https://tools.ietf.org/html/rfc7231#section-6.5.1"
            },
            "status": "400",
            "title": "One or more validation errors occurred.",
            "meta": {
                "stackTrace": [
                    "JsonApiDotNetCore.Errors.UnsuccessfulActionResultException: Exception of type 'JsonApiDotNetCore.Errors.UnsuccessfulActionResultException' was thrown.",
                    "   at string JsonApiDotNetCore.Serialization.Response.JsonApiWriter.GetResponseBody(object model, HttpContext httpContext) in C:/projects/jsonapidotnetcore/src/JsonApiDotNetCore/Serialization/Response/JsonApiWriter.cs:line 84"
                ]
            }
        }
    ]
}

I should have the detail where my "message" attribute is listed with my custom error.
Where did I miss something?

VERSIONS USED

  • JsonApiDotNetCore version: 5.1.0
  • ASP.NET Core version: 6
  • Entity Framework Core version: no (custom)
  • Database provider: oracle

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions