Skip to content

Null Reference in TypeMatchFilter Middleware with non-JADNC Controller #405

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
joshhubers opened this issue Sep 13, 2018 · 5 comments
Closed
Labels

Comments

@joshhubers
Copy link
Contributor

Description

I'm getting a

    System.NullReferenceException: Object reference not set to an instance of an object.
    at JsonApiDotNetCore.Middleware.TypeMatchFilter.OnActionExecuting(ActionExecutingContext context) in 
C:\projects\jsonapidotnetcore\src\JsonApiDotNetCore\Middleware\TypeMatchFilter.cs:line 34

Using a standard dotnet Core Controller when returning a Json(result) of type string.
My code is as follows:

namespace Foo.Controllers
{
    [Authorize]
  public class UploadController : Controller
  {
    IPhotoService _photoService;

    public UploadController(IPhotoService photoService)
    {
      _photoService = photoService;
    }

    [Route("api/v1/attachments/upload")]
    [HttpPost]
    public async Task<IActionResult> PostFileAsync(IFormFile file)
    {
      if(file == null || file.Length == 0)
        return Content("No content in file or file not uploaded");

      string path = await _photoService.UploadPhotoAsync(file);
      
      return Json(path);
    }
  }
}

Looking at the code I'm gonna guess it's something to do with the IsJsonApiRequest not filtering replies properly?

Environment

  • JsonApiDotNetCore Version: 2.5.2
  • Other Relevant Package Versions:
@joshhubers
Copy link
Contributor Author

joshhubers commented Sep 13, 2018

Rolling back to 2.5.0 before the middleware update fixed the issue for me.

@jaredcnance
Copy link
Contributor

jaredcnance commented Sep 13, 2018

Thanks for reporting this. Currently, we only check the Content-Type and assume that all application/vnd.api+json POST|PATCH requests should be handled by this filter. Are you using this content type in your non-json:api requests? We may be able to run a check against the activating controller type, but I'll need to verify that's possible.

Adding the source method for context:

public void OnActionExecuting(ActionExecutingContext context)
{
var request = context.HttpContext.Request;
if (IsJsonApiRequest(request) && request.Method == "PATCH" || request.Method == "POST")
{
var deserializedType = context.ActionArguments.FirstOrDefault().Value?.GetType();
var targetType = context.ActionDescriptor.Parameters.FirstOrDefault()?.ParameterType;
if (deserializedType != null && targetType != null && deserializedType != targetType)
{
var expectedJsonApiResource = _jsonApiContext.ContextGraph.GetContextEntity(targetType);
throw new JsonApiException(409,
$"Cannot '{context.HttpContext.Request.Method}' type '{_jsonApiContext.RequestEntity.EntityName}' "
+ $"to '{expectedJsonApiResource?.EntityName}' endpoint.",
detail: "Check that the request payload type matches the type expected by this endpoint.");
}
}
}
private bool IsJsonApiRequest(HttpRequest request)
{
return (request.ContentType?.Equals(Constants.ContentType, StringComparison.OrdinalIgnoreCase) == true);
}

@joshhubers
Copy link
Contributor Author

joshhubers commented Sep 14, 2018

Yes, I am using the application/vnd.api+json content type. Good to know!

@medokin
Copy link
Contributor

medokin commented Feb 10, 2019

This bug was resolved with #457

Currently available in alpha.

@jaredcnance
Copy link
Contributor

Thanks @medokin! Closing this. Please re-open if the issue is not resolved.

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

No branches or pull requests

3 participants