Swagger setup error after upgrade

Ejekwu Graveth Uzoma 40 Reputation points
2025-12-30T10:17:36.9166667+00:00

builder.Services.AddSwaggerGen(c =>

{

var securityScheme = new OpenApiSecurityScheme

{

    Name = "Authorization",

    Type = SecuritySchemeType.Http,

    Scheme = "bearer",

    BearerFormat = "JWT",

    In = ParameterLocation.Header,

    Description = "JWT Authorization header using the Bearer scheme. **Enter Bearer Token Only**",

    Reference = new OpenApiReference

    {

        Id = "Bearer",

        Type = ReferenceType.SecurityScheme

    }

};

c.EnableAnnotations();

c.AddSecurityDefinition(securityScheme.Reference.Id, securityScheme);

c.AddSecurityRequirement(new OpenApiSecurityRequirement

                {

                { securityScheme, Array.Empty<string>() }

                });

c.OperationFilter<SwaggerFileOperationFilter>();

}); I keep getting error here: Reference to type 'OpenApiOperation' claims it is defined in 'Microsoft.OpenApi', but it could not be found, 'OpenApiSecurityScheme' does not contain a definition for 'Reference':

Here is my package verision:

Microsoft.OpenApi 2.0.0 2.0.0

Swashbuckle.AspNetCore 9.0.4 9.0.4
Swashbuckle.AspNetCore.Annotations 9.0.4 9.0.4

Developer technologies | .NET | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Gade Harika (INFOSYS LIMITED) 2,415 Reputation points Microsoft External Staff
    2025-12-31T10:53:10.01+00:00

    Thanks for the details! Since you upgraded the app to .NET 10, the errors come from an OpenAPI package mismatch. The short answer to “what do I do with OpenAPI and Swashbuckle to keep my .NET 9 behavior?” is:

     **Option A — Keep Swashbuckle on .NET 10:**
    
    1. Upgrade to Swashbuckle.AspNetCore v10 (compatible with .NET 10 & Microsoft.OpenApi v2). dotnet add package Swashbuckle.AspNetCore --version 10. dotnet add package Swashbuckle.AspNetCore.Annotations --version 10.
    2. Remove any explicit Microsoft.OpenApi reference from .csproj and run dotnet clean && dotnet restore. Swashbuckle v10 brings OpenAPI.NET v2 transitively; mixing versions causes your compile/runtime errors. [github.com]
    3. Update your JWT security code to the v10 pattern (register a named scheme and reference it when adding AddSecurityRequirement). [github.com]
    4. Keep your UI as before (UseSwagger + UseSwaggerUI). [nuget.org]

    Option B — Use built‑in OpenAPI and add a UI:

    1. Add Microsoft.AspNetCore.OpenApi, then AddOpenApi() + MapOpenApi() (serves /openapi/v1.json). [learn.microsoft.com]
    2. Add a UI: either Swagger UI (Swashbuckle.AspNetCore.SwaggerUI) and point to /openapi/v1.json, or use Scalar (Scalar.AspNetCore) which maps with app.MapScalarApiReference(). [learn.microsoft.com], [guides.scalar.com] Both options are fully supported in .NET 10. The built‑in OpenAPI path is the direction the templates moved in .NET 9 (OpenAPI 3.1, transformers, multiple docs, AoT‑friendly). [learn.microsoft.com] If you share your .csproj package list, I can confirm which path you’re currently on and provide the exact code edits.
    0 comments No comments

Answer accepted by question author
  1. Gade Harika (INFOSYS LIMITED) 2,415 Reputation points Microsoft External Staff
    2025-12-30T11:33:29.8066667+00:00

    Thanks for reaching out.
    This error is caused by a version mismatch between Swashbuckle and Microsoft.OpenApi.

    Swashbuckle.AspNetCore 9.x depends on OpenAPI.NET 1.x, where OpenApiSecurityScheme still has the Reference property and OpenApiOperation types match. You’ve pinned Microsoft.OpenApi 2.0.0, which introduced breaking API changes, so the code no longer compiles.

    Fix:

    1. Remove the explicit Microsoft.OpenApi 2.0.0 package from the project.
    2. Run dotnet clean and dotnet restore so Swashbuckle restores Microsoft.OpenApi 1.6.x.
    3. Rebuild. Your Reference usage and filters should work again.

    If you’re moving to .NET 9/10’s built‑in OpenAPI (Microsoft.AspNetCore.OpenApi), drop Swashbuckle and use AddOpenApi()/MapOpenApi(). Note that OpenAPI.NET 2.x removed Reference from OpenApiSecurityScheme and changed reference handling; you’ll need to update your transformer/filter code accordingly and add a UI (e.g., Scalar/NSwag) to render /openapi/v1.json.

    Refs:

    Let us know if the issue persists after following these steps. I’ll be happy to assist further if needed. If the issue has been resolved, Kindly mark the provided solution as "Accept Answer", so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.