json-api-dotnet/JsonApiDotNetCore

issue using asp net validation state x json-api-dot-net-core

Alex-Doms opened this issue · 8 comments

SUMMARY

Hello, i have an issue using asp net validation state x json-api-dot-net-core.
My custom errors throws from my Validate function, but the json reponse show a generic error.
Do i miss something in configuration?

DETAILS

My model :

public class MyClass: Identifiable<string>, IValidatableObject
    {
        [Attr]
        public List<int> MyProp{ get; set; } = new List<int>();

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            foreach (var d in this.MyProp)
            {
                if (d!=0)
                    res.Add(new ValidationResult($"MyCustomMessage"));
            }
            return res;
        }
}

Configuration programs.cs :

    builder.Services.AddJsonApi(
        options =>
        {
            options.UseRelativeLinks = true;
            options.MaximumIncludeDepth = 5;
            options.IncludeTotalResourceCount = true;
            options.DefaultPageSize = new PageSize(200);
            options.MaximumPageSize = new PageSize(500);
            options.MaximumPageNumber = null;
            options.ValidateModelState = true;
            options.IncludeRequestBodyInErrors = true;
            options.AllowUnknownQueryStringParameters = true;
            options.ResourceLinks = JsonApiDotNetCore.Resources.Annotations.LinkTypes.All ;
            options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
            options.SerializerOptions.Converters.Add(new DateConverter());
            options.RelationshipLinks = JsonApiDotNetCore.Resources.Annotations.LinkTypes.All;
            options.SerializerOptions.PropertyNameCaseInsensitive = false;
            options.SerializerOptions.WriteIndented = true;
            options.SerializerOptions.IncludeFields = true;
            options.SerializerOptions.ReadCommentHandling = System.Text.Json.JsonCommentHandling.Skip;
            options.DefaultAttrCapabilities = JsonApiDotNetCore.Resources.Annotations.AttrCapabilities.All;
            options.IncludeExceptionStackTraceInErrors = false;
            
        },
        dis =>
        {
            dis.AddCurrentAssembly();
            dis.AddAssembly(Assembly.GetAssembly(typeof(ActeBilan)));
        }
);

Response :

{
    "links": {
        "self": "/WS_ApiGalaxie/v2/myClasses/"
    },
    "errors": [
        {
            "id": "c4b2e7db-5bf7-4a19-8c89-e185b2a33e17",
            "links": {
                "about": "https://tools.ietf.org/html/rfc7231#section-6.5.1"
            },
            "status": "400",
            "title": "One or more validation errors occurred."
        }
    ]
}

VERSIONS USED

  • JsonApiDotNetCore version: 5.1.0
  • ASP.NET Core version: .net core 6
  • Entity Framework Core version: none
  • Database provider: none

Implementations of IValidatableObject.Validate act at the class level, so it cannot be determined which property failed validation. Furthermore, because of JSON:API partial post/patch, the property may not have been sent at all by the client.

See https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/master/test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/DateMustBeInThePastAttribute.cs for an example to make this work correctly.

@Alex-Doms Does this answer your question? Can this issue be closed now?

Hello Bart, well no, i've converted my validation test with an ValidationAttribute (I prefer this form), add it on the property on my model and I removed IValidatableObject implementation. My ValidationResult error message is well fired in debug but i still got the same generic error message in response.

I got same generic error with [MaxLength(4)] on a string, i think i have something wrong in my configuration.

in UnsuccessfulActionResultException.cs,
Does the detail property of the object problemDetails should not be null?
My custom error is in the prop Error in problemDetails, so JSonAPi don't list it.
image

Sounds like a case of https://www.jsonapi.net/usage/common-pitfalls.html#do-not-use-apicontroller-on-jsonapi-controllers. Since JsonApiDotNetCore v5.1.2, a warning is logged when using that.

If that's not the source of the problem, please provide a minimal repro project so I can analyze what's going on.

Ok, that's it, i'm using apicontroller, i upgrade to v5.1.2 and it's works! Thanks for your help!

Glad to help :)