Location>code7788 >text

NET 9 Uses Scalar Instead of Swagger

Popularity:378 ℃/2024-11-13 11:26:18

contexts

NET 9 has just been officially released, and if you create an empty Core 9.0 Web API project and start it up, you'll be surprised to find that Swagger, which has been with you for years, is gone! --That's because the Core project team has removed it from .NET 9, see here for more information.[github]Announcement: is being removed in .NET 9

image

The reasons for Swagger's removal can be summarized as follows:

  • Poor maintenance of Swashbuckle: The Swashbuckle project is no longer actively maintained by its community owners, has many unresolved issues, and has not been released in an official .NET 8-compatible version.

  • Moving to: The Core team will be making enhancements to replace Swashbuckle and enable OpenAPI document generation.

  • Alternatives: In addition to Swashbuckle, there are other projects such as NSwag that support OpenAPI document generation and client/server code generation, so developers can choose the right solution according to their project needs.

  • Enhanced built-in API support: Since Core 3.1, the framework has provided metadata support such as ApiExplorer, which, combined with Visual Studio and Visual Studio Code's built-in support for .http files, makes API testing and debugging a better experience.

  • Promoting OpenAPI as a Core Component: The Core team plans to further enhance the integration of OpenAPI as a core component in .NET 9, focusing on generating OpenAPI documents in JSON format.

In addition to NSwag mentioned above, Scalar is an excellent alternative to Swagger.

Scalar

Scalar is an open source API platform , providing a modern REST API client , beautiful API documentation and first-class OpenAPI/Swagger support , official support for almost all programming languages and platforms .
image

Github address:/scalar/scalar

NET 9 Integration with Scalar

NET is also a first class citizen supported by Scalar, integration is very simple, nuget installer

dotnet add package 

Then just add a code()

using ;

var builder = (args);

();

var app = ();

if (())
{
    (); // scalar/v1
    ();
}

("/", () => "Hello world!");

();

Finally start the project and punch the clockscalar/v1This address is the Scalar interface.
image

The interface is very clean and easy to use, and supportsNight mode

Add JWT Authentication

Adding JWT to Scalar is also easy, customizing aBearerSecuritySchemeTransformerclass to implement theIOpenApiDocumentTransformerinterface will suffice.

public sealed class BearerSecuritySchemeTransformer(IAuthenticationSchemeProvider authenticationSchemeProvider): IOpenApiDocumentTransformer {
  public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken) {
    var authenticationSchemes = await ();
    if ((authScheme =>  == "Bearer")) {
      // Add the security scheme at the document level
      var requirements = new Dictionary < string,
        OpenApiSecurityScheme > {
          ["Bearer"] = new OpenApiSecurityScheme {
            Type = ,
              Scheme = "bearer", // "bearer" refers to the header name here
              In = ,
              BearerFormat = "Json Web Token"
          }
        };
       ??= new OpenApiComponents();
       = requirements;

      // Apply it as a requirement for all operations
      foreach(var operation in (path => )) {
        (new OpenApiSecurityRequirement {
          [new OpenApiSecurityScheme {
            Reference = new OpenApiReference {
              Id = "Bearer", Type = 
            }
          }] =  < string > ()
        });
      }
    }
  }
}

Then just sign up

(opt =>
{
    <BearerSecuritySchemeTransformer>();
});

ultimate

Scalar is an excellent alternative to Swagger, some features are even more powerful than Swagger, we recommend that you hurry to try.

(concluded)