Skip to content

PrimaryResourceType not to be null at this point #1285

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
sasa-vasiljevic opened this issue Jun 23, 2023 · 4 comments
Closed

PrimaryResourceType not to be null at this point #1285

sasa-vasiljevic opened this issue Jun 23, 2023 · 4 comments
Labels

Comments

@sasa-vasiljevic
Copy link

SUMMARY

I tried to make very basic working sample of JSONAPI rest service. I followed instructions in building sample. I can run the sample and it shows swagger documentation. However when I try to call a method get on my controller (using Postman, or Swagger) I get following error:

System.InvalidOperationException: Expected IJsonApiRequest.PrimaryResourceType not to be null at this point.
   at JsonApiDotNetCore.Services.JsonApiResourceService`2.GetAsync(CancellationToken cancellationToken) in C:\projects\jsonapidotnetcore\src\JsonApiDotNetCore\Services\JsonApiResourceService.cs:line 63

DETAILS

I have a Reastaurant object like this:

[Resource]
public class Restaurant : Identifiable<int>
{
    public Restaurant() { }

    [Attr(PublicName = "name")]
    public string? Name { get; set; }

    [Attr(PublicName = "address")]
    public string? Address { get; set; }

    [HasMany(PublicName = "dishes")]
    public virtual List<Dish>? Dishes { get; set; }
}

I have Dish object as this:

[Resource]
public class Dish : Identifiable<int>
{
    public Dish() { }

    [Attr(PublicName = "name")]
    public string? Name { get; set; }

    [Attr(PublicName = "rating")]
    public int? Rating { get; set; }


    [HasOne(PublicName = "restaurant")]
    public virtual Restaurant? Restaurant { get; set; }
    public int RestaurantId { get; set; }
}

My restaurant controller looks like this:

[Route("[controller]")]
public partial class RestaurantsController : JsonApiController<Restaurant, int>
{
    [ActivatorUtilitiesConstructor]
    public RestaurantsController(
        IJsonApiOptions options,
        IResourceGraph resourceGraph,
        IResourceService<Restaurant, int> resourceService,
        ILoggerFactory loggerFactory)
        : base(options, resourceGraph, loggerFactory, resourceService)
    { }
}

and Dish controller same.
That is it. Plus this is startup code:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

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


builder.Services.AddDbContext<AppDbContext>(opt =>
{
    opt.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});
builder.Services.AddJsonApi<AppDbContext>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.MapControllers();

using (var serviceScope = app.Services.CreateScope())
{
    var context = serviceScope.ServiceProvider.GetService<AppDbContext>();

    context?.Database.EnsureCreated();
    if (context?.Restaurants.Any() == false)
    {
        //...fill database
        context.SaveChanges();
    }
}
app.UseRouting();
app.UseJsonApi();
app.UseEndpoints(endpoints => endpoints.MapControllers());
app.Run();

VERSIONS USED

  • JsonApiDotNetCore version: 5.2.0
  • ASP.NET Core version: 6.0
  • Entity Framework Core version: 7.0.8
  • Database provider: MicrosoftSqlServer
@bkoelman
Copy link
Member

bkoelman commented Jun 23, 2023

The stable version of JsonApiDotNetCore does not work with Swagger/OpenAPI. Did you try this on the openapi branch? See also #1046 and https://www.jsonapi.net/getting-started/faq.html#why-cant-i-use-openapi.

@sasa-vasiljevic
Copy link
Author

I completely removed Swagger/OpenAPI from application. Still the same error. Here is my startup code now:

            var builder = WebApplication.CreateBuilder(args);

            builder.Services.AddControllers();
            builder.Services.AddDbContext<AppDbContext>(opt =>
            {
                opt.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
            });
            builder.Services.AddJsonApi<AppDbContext>();

            var app = builder.Build();

            // Configure the HTTP request pipeline.

            app.UseHttpsRedirection();

            app.MapControllers();

            app.UseRouting();
            app.UseAuthorization();


            app.UseJsonApi();
            app.UseEndpoints(endpoints => endpoints.MapControllers());


            app.Run();

Controllers and model are the same.

@bkoelman
Copy link
Member

bkoelman commented Jun 23, 2023

I got your sample code working after I removed the following line:

app.MapControllers();

The bare minimum is described at: https://www.jsonapi.net/getting-started/step-by-step.html#add-services-and-middleware.

If you then get an error about multiple controllers found, make sure that your RestaurantsController is in exactly the same namespace as the auto-generated one. You can remove the [Route("[controller]")] line.

Also, you can leave out the PublicName = "..." parameters, the casing convention is automatically applied.

@sasa-vasiljevic
Copy link
Author

sasa-vasiljevic commented Jun 26, 2023 via email

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

2 participants