Skip to content

ValidationContext does not have a ServiceProvider on atomic operations #1250

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
Tommy228 opened this issue Jan 30, 2023 · 1 comment · Fixed by #1251
Closed

ValidationContext does not have a ServiceProvider on atomic operations #1250

Tommy228 opened this issue Jan 30, 2023 · 1 comment · Fixed by #1251
Labels

Comments

@Tommy228
Copy link
Contributor

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
@Tommy228 Tommy228 added the bug label Jan 30, 2023
@Tommy228
Copy link
Contributor Author

Turns out the fix is quite simple, it just needs a HttpContext to be added to the validation object here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

1 participant