Skip to content

Calling AddOpenApiForJsonApi before AddJsonApi regenerates/re-registers controllers, causing an error #1734

Closed
@isaaclyman

Description

@isaaclyman

DESCRIPTION

In a simple project that uses JsonApiDotNetCore.OpenApi.Swashbuckle, when I create a single resource and do something like:

// Program.cs

var builder = WebApplication.CreateBuilder(args);

// BEGIN FOOTGUN
builder.Services.AddOpenApiForJsonApi();
builder.Services.AddJsonApi(opts => {
  opts.Namespace = "api";
}, discovery: discovery => discovery.AddCurrentAssembly());
// END FOOTGUN

// ...

var app = builder.Build();
app.UseJsonApi();

I repeatedly get the error described in #1179:

JsonApiDotNetCore.Errors.InvalidConfigurationException: Multiple controllers found for resource type 'myResources': 'MyApp.Api.Library.MyResource.MyResourceController' and 'MyApp.Api.Library.MyResource.MyResourceController'

  • This happens whether I use the [Resource] attribute or define an explicit controller.
  • It happens whether I use automatic discovery or manually register the resource.

Any of the following changes stops the error from occurring:

  • Removing the call to app.UseJsonApi();
  • Removing JsonApiDotNetCore.OpenApi.Swashbuckle from the project
  • Putting the AddOpenApiForJsonApi() call after AddJsonApi()

STEPS TO REPRODUCE

  1. Use both JsonApiDotNetCore and JsonApiDotNetCore.OpenApi.Swashbuckle in a project.
  2. Call Services.AddOpenApiForJsonApi() before Services.AddJsonApi().
  3. Start the project.

EXPECTED BEHAVIOR

I would expect the package to handle this gracefully or surface an error telling me that I need to call those methods in a specific order.

ACTUAL BEHAVIOR

I see an error for which the only matching issue in Github tells me it's a problem with source generation in .NET 6.

VERSIONS USED

  • JsonApiDotNetCore version: 5.7.1
  • .NET Core version: 8.0.410
  • Entity Framework Core version: N/A
  • Database provider: N/A

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