Skip to content

ValidationContext does not have a ServiceProvider on atomic operations #1250

Closed
@Tommy228

Description

@Tommy228

DESCRIPTION

This bug assumes I have a model implementing IValidatableObject and injecting a service for the validation. A use-case for this is injecting ITargetedFields to check if a field needs to be validated or not on a PATCH request (#1204).

If I try to create or update this model through an atomic operations, then I cannot inject any service because ServiceProvider is null.

STEPS TO REPRODUCE

Create the following object

[UsedImplicitly(ImplicitUseTargetFlags.Members)]
[Resource]
public sealed class Person : Identifiable<int>, IValidatableObject
{
    [Attr]
    public string Name { get; set; } = null!;

    [HasMany]
    public ICollection<Book> Books { get; set; } = new List<Book>();

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        IJsonApiRequest service = validationContext.GetRequiredService<IJsonApiRequest>();

        // do something with the service

        return Enumerable.Empty<ValidationResult>();
    }
}

Try to update it with an atomic operation

{
  "atomic:operations": [
    {
      "op": "add",
      "data": {
        "type": "people",
        "attributes": {
          "name": ""
        }
      }
    }
  ]
}

EXPECTED BEHAVIOR

The object is created.

ACTUAL BEHAVIOR

The creation fails with the following error

{
    "links": {
        "self": "/api/operations"
    },
    "errors": [
        {
            "id": "af29550f-225c-4386-87b9-523633e3db3e",
            "status": "500",
            "title": "An unhandled error occurred while processing this request.",
            "detail": "No service for type 'JsonApiDotNetCore.Middleware.IJsonApiRequest' has been registered."
        }
    ]
}

VERSIONS USED

  • JsonApiDotNetCore version: 5.1.1
  • ASP.NET Core version: .NET 7
  • Entity Framework Core version: EFCore 6
  • Database provider: SQL Server

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions