RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.69k stars 1.23k forks source link

Swagger document is not loading for version v2. #4689

Open sarojmcts opened 8 months ago

sarojmcts commented 8 months ago

I have implemented API versioning, but Swagger document is not generating for v2. Anything is wrong here?

image

builder.Services.AddApiVersioning(config => {
// Specify the default API Version as 1.0 config.DefaultApiVersion = new ApiVersion(1, 0); // If the client hasn't specified the API version in the request, use the default API version number config.AssumeDefaultVersionWhenUnspecified = true; // Advertise the API versions supported for the particular endpoint config.ReportApiVersions = true; config.ApiVersionReader = new UrlSegmentApiVersionReader(); });

builder.Services.AddVersionedApiExplorer(config => {
config.GroupNameFormat = "'V'VVV"; config.SubstituteApiVersionInUrl = true; });

internal static IServiceCollection AddOpenApiDocumentation(this IServiceCollection services, AppConfig settings) { if (settings.SwaggerSetting.Enable) { services.AddEndpointsApiExplorer(); var dict = new Dictionary<string, string>(); dict.Add(settings.SwaggerSetting.ApiScope, "Access the api");

     services.AddSwaggerDocument((document, provider) =>
     {
         document.OperationProcessors.Add(new RequestBodyExampleProcessor(provider));

         document.PostProcess = doc =>
         {
             doc.Info.Title = settings.SwaggerSetting.Title;
             doc.Info.Version = settings.SwaggerSetting.Version;
             doc.Info.Description = settings.SwaggerSetting.Description;
             doc.Info.Contact = new()
             {
                 Name = settings.SwaggerSetting.ContactName,
                 Email = settings.SwaggerSetting.ContactEmail,
                 Url = settings.SwaggerSetting.ContactUrl
             };

             doc.Info.License = new()
             {
                 Name = settings.SwaggerSetting.LicenseName,
                 Url = settings.SwaggerSetting.LicenseUrl
             };
         };
         document.ResolveExternalXmlDocumentation = true;
         document.UseXmlDocumentation = true;
         document.AddSecurity(JwtBearerDefaults.AuthenticationScheme, Enumerable.Empty<string>(), new OpenApiSecurityScheme
         {
             Type = OpenApiSecuritySchemeType.OAuth2,
             Description = "Api Authentication",
             Flow = OpenApiOAuth2Flow.Implicit,
             Flows = new OpenApiOAuthFlows()
             {
                 Implicit = new OpenApiOAuthFlow()
                 {
                     Scopes = dict,
                     AuthorizationUrl = settings.SwaggerSetting.AuthorizationUrl,
                     TokenUrl = settings.SwaggerSetting.TokenUrl
                 },
             }
         }
         );

         document.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor(JwtBearerDefaults.AuthenticationScheme));
     }

     );

// //services.AddSwaggerDocument((document,provider) => // //{ // // document.OperationProcessors.Add(new RequestBodyExampleProcessor(provider)); // // document.DocumentName = "v2"; // // document.PostProcess = doc => // // { // // doc.Info.Version = "v2"; // // }; // // document.ApiGroupNames = new[] { "v2" }; // // document.ResolveExternalXmlDocumentation = true; // // document.UseXmlDocumentation = true; // //});

 }

 return services;

}

internal static IApplicationBuilder UseOpenApiDocumentation(this IApplicationBuilder app, AppConfig settings) { if (settings.SwaggerSetting.Enable) { string path = "/apidoc/{documentName}/swagger.json"; app.UseOpenApi(p => { p.Path = path; });

    app.UseSwaggerUi3(options =>
    {
        options.DefaultModelsExpandDepth = -1;
        options.DocExpansion = "none";
        options.TagsSorter = "alpha";
        options.DocumentPath = path;
        options.Path = "/apidoc";
        options.EnableTryItOut = settings.SwaggerSetting.EnableTryitOut;

        options.OAuth2Client = new OAuth2ClientSettings
        {
            AppName = settings.SwaggerSetting.Description,
            ClientId = settings.SwaggerSetting.OpenApiClientId,
            //ClientSecret = settings.SwaggerSetting.OpenApiClientSecret,
            UsePkceWithAuthorizationCodeGrant = true,
            ScopeSeparator = " ",
        };
        options.OAuth2Client.Scopes.Add(settings.SwaggerSetting.ApiScope);
    });
}
return app;

}