Skip to content

Microsoft.OpenAPI 2.0.0.0 has breaking changes #61123

@bhrugen

Description

@bhrugen

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In the previous version of Microsoft.OpenAPI we had the following

public OpenApiSecurityScheme(OpenApiSecurityScheme securityScheme)
 {
     Type = securityScheme?. Type ?? Type;
     Description = securityScheme?. Description ?? Description;
     Name = securityScheme?. Name ?? Name;
     In = securityScheme?. In ?? In;
     Scheme = securityScheme?. Scheme ?? Scheme;
     BearerFormat = securityScheme?. BearerFormat ?? BearerFormat;
     Flows = securityScheme?. Flows != null ? new(securityScheme?. Flows) : null;
     OpenIdConnectUrl = securityScheme?. OpenIdConnectUrl != null ? new Uri(securityScheme.OpenIdConnectUrl.OriginalString, UriKind.RelativeOrAbsolute) : null;
     Extensions = securityScheme?. Extensions != null ? new Dictionary<string, IOpenApiExtension>(securityScheme.Extensions) : null;
     UnresolvedReference = securityScheme?. UnresolvedReference ?? UnresolvedReference;
     Reference = securityScheme?. Reference != null ? new(securityScheme?. Reference) : null;
 }

But they remove the reference in version 2.0.0.0 and also i believe there are some breaking changes since if i use .NET 10 preview 2 to add securityrequirement it does not work at all

Previously what works in .NET 9 is below

builder. Services.AddSwaggerGen(options =>
{
    options. AddSecurityDefinition("JwtBearerDefaults.oa", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
    {
        Description =
            "JWT Authorization header using the Bearer scheme. \r\n\r\n " +
            "Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\n" +
            "Example: \"Bearer 12345abcdef\"",
        Name = "Authorization",
        In = ParameterLocation.Header,
        Scheme = JwtBearerDefaults.AuthenticationScheme,

});
    options. AddSecurityRequirement(new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"
                },
                In = ParameterLocation.Header,
            },
            new List<string>()
        }
    });
});

but if i add it exactly in program.cs after adding the swashbuckle nuget package it does not work in .NET 10 Preview 2 Web API

Is there a workaround for this?

Expected Behavior

it should work as expected like in .NET 9

Steps To Reproduce

create a new .net 10 preview 2 web api project.

add swashbuckle nuget package

add the following in program.cs

builder. Services.AddSwaggerGen(options =>
{
    options. AddSecurityDefinition("JwtBearerDefaults.oa", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
    {
        Description =
            "JWT Authorization header using the Bearer scheme. \r\n\r\n " +
            "Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\n" +
            "Example: \"Bearer 12345abcdef\"",
        Name = "Authorization",
        In = ParameterLocation.Header,
        Scheme = JwtBearerDefaults.AuthenticationScheme,

});
    options. AddSecurityRequirement(new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"
                },
                In = ParameterLocation.Header,
            },
            new List<string>()
        }
    });
});

Exceptions (if any)

No response

.NET Version

.NET 10 preview 2

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions