json-api-dotnet/JsonApiDotNetCore

PrimaryResourceType is null and giving error "Expected IJsonApiRequest.PrimaryResourceType not to be null at this point."

AnushreeSoman21 opened this issue · 6 comments

SUMMARY

I attempted to create an API using JsonAPIDotNetCore, however I was receiving an exception with the message "Expected IJsonApiRequest.PrimaryResourceType not to be null at this point" when I tried to call the GetAsync API for an entity.

DETAILS

I have developed controllers by inheriting BaseJsonapicontroller exactly like below and am attempting to call the GetAsync API. I am using Jsonapidotnetcore 5.5.1,.NET 8, and EFCore 8. This gives the error as "Expected IJsonApiRequest.PrimaryResourceType not to be null at this point."

Controller -
public class DemoController : BaseJsonApiController<Demo, long>

VERSIONS USED

  • JsonApiDotNetCore version: 5.5.1
  • ASP.NET Core version: 8
  • Entity Framework Core version: 8
  • Database provider: SQL

I've looked over one of the jsonapidotnetcore issues in your problem list and have attempted the same solutions, but nothing has worked. (PrimaryResourceType not to be null at this point #1285)

Note: I have recently updated the API from .NET 6 to .NET8 and JsonAPIDotNEtCore package from 5.1.0 to 5.5.0

Hi @AnushreeSoman21, thanks for reaching out. I'm not seeing anything wrong in what you've described. Can you share a minimal repro project so I can analyse what's happening?

JSONAPISample.zip

I have used your sample project only but still facing the same issue. Attached sample project here

Thanks, that explains a lot!

This issue should have started with: I'm trying to hack in OpenAPI support, but can't get that to work.

You're running into the exact same problem as reported in #1285, which you already linked to. Apparently, you added builder.Services.AddControllers(); (which breaks many things by itself), so you'll see some endpoints show up in SwaggerUI. However, what you see there is completely wrong, as indicated in our FAQ. There's a good reason we've been working on OpenAPI support for a long time: it's not that simple.

If you'd like to use OpenAPI, pick the experimental version from our dev feed and install as described here. See also the documentation on using OpenAPI.

Otherwise, just removing the lines you added:

builder.Services.AddControllers();
builder.Services.AddSwaggerDocument();
builder.Services.AddEndpointsApiExplorer();

makes your repro work just fine.

Hi @bkoelman,

Thank you for the response. I have tried removing below lines of code and swagger support but still it is not working for me.

builder.Services.AddControllers();
builder.Services.AddSwaggerDocument();
builder.Services.AddEndpointsApiExplorer();

and swagger support but still it is not working for me.

app.UseOpenApi();
app.UseSwaggerUI();

Can you be more specific? What have you tried exactly?
If I download your project and comment out those lines, a GET request to http://localhost:5210/api/books returns the list of books.

Does the GettingStarted sample work on your machine? If not, can you try adding things from your project one-by-one until it breaks?

What you can also try is clearing caches, restart your IDE, etc.

Closing due to inactivity. Please provide the requested information if you need this issue to be reopened.