Beispiel #1
0
        public static IServiceCollection AddNSwagSwagger(this IServiceCollection services, NSwagConfig config = null)
        {
            config = config ?? new NSwagConfig {
            };

            services.AddSingleton <IOperationProcessor>(new NSwag.Generation.Processors.Security.AspNetCoreOperationSecurityScopeProcessor(config?.OperationSecurity?.SecurityName ?? DefaultSecurityName)); //授权控制
            services.AddSingleton <IDocumentProcessor, DocumentControllerTagsProcessor>();                                                                                                                   //控制器注释

            if (config.ShowOpenApi)
            {
                // Register an OpenAPI 3.0 document generator
                services.AddOpenApiDocument((document, sp) =>
                {
                    _settings(sp, document, config, "openapi/");
                    //document.DocumentName = "openapi/" + document.Version;
                    //document.ApiGroupNames = new[] { "v1" };
                });
            }
            if (config.ShowSwagger)
            {
                // Register a Swagger 2.0 document generator
                services.AddSwaggerDocument((document, sp) =>
                {
                    _settings(sp, document, config, "swagger/");
                    //document.DocumentName = "swagger/" + document.Version;
                });
            }
            return(services);
        }
Beispiel #2
0
        private static void _settings(System.IServiceProvider sp, AspNetCoreOpenApiDocumentGeneratorSettings document, NSwagConfig config, string versionPrefix)
        {
            document.Title       = config?.Title ?? "WebApi 文档";
            document.Description = config?.Description
//@"天使项目 API 文档,可以使用API Key来授权测试。

//# Introduction
//This API is documented in **OpenAPI format** and is based on

//# Authentication

// Petstore offers two forms of authentication: **OpenAPI format**
//      - API Key
//      - OAuth2
//    OAuth2 - an open protocol to allow secure authorization in a simple
//    and standard method from web, mobile and desktop applications.

//"
            ;
            var version = config?.Version ?? "v1";

            if (string.IsNullOrWhiteSpace(config?.PathPrefix) && config?.ApiGroupNames?.Length == 1)
            {
                config.PathPrefix = config.ApiGroupNames.FirstOrDefault();
            }
            if (!string.IsNullOrWhiteSpace(config?.PathPrefix))
            {
                config.PathPrefix = config.PathPrefix.Trim('/') + "/";
            }

            document.Version       = version;
            document.DocumentName  = versionPrefix + config?.PathPrefix + version;
            document.ApiGroupNames = config?.ApiGroupNames;

            //document.DefaultEnumHandling = NJsonSchema.Generation.EnumHandling.CamelCaseString;
            document.GenerateExamples = true;
            document.GenerateEnumMappingDescription = true;

            //var dfdf = sp.GetService<Microsoft.Extensions.Options.IOptions<JsonOptions>>().Value;
            //dfdf.JsonSerializerOptions.Converters.Any(f => f is System.Text.Json.Serialization.JsonStringEnumConverter)
            if (config?.DefaultEnumHandling != null)
            {
                document.DefaultEnumHandling = config.DefaultEnumHandling.Value;
            }


            document.PostProcess = (f) =>
            {
                f.Info.TermsOfService   = config?.TermsOfService;
                f.Info.Contact          = config?.Contact;
                f.Info.License          = config?.License;
                f.ExternalDocumentation = new OpenApiExternalDocumentation {
                    Description = "ReDoc 文档", Url = "../redoc/" + document.DocumentName
                };

                f.Info.ExtensionData = config?.ExtensionData;
                //f.Info.TermsOfService = "http://www.weberp.com.cn";
                //f.Info.Contact = new NSwag.OpenApiContact { Email = "*****@*****.**", Name = "The KeWei Team", Url = "http://www.weberp.com.cn" };
                //f.Info.License = new NSwag.OpenApiLicense { Name = "Apache 2.0", Url = "http://www.apache.org/licenses/LICENSE-2.0.html" };

                //f.Info.ExtensionData = new Dictionary<string, object>();
                //f.Info.ExtensionData.Add("x-logo", new { url = "https://redocly.github.io/redoc/petstore-logo.png", altText = "Petstore logo" });

                //f.ExternalDocumentation = new OpenApiExternalDocumentation { Description = "ReDoc For {documentName}", Url = "/redoc/{documentName}" };
            };

            if (config?.OperationSecurity != null)
            {
                config?.OperationSecurity.Apply(document);
            }

            #region 基本身份认证
            //document.AddSecurity("Basic", new[] { "skoruba_identity_admin_api" }, new NSwag.OpenApiSecurityScheme
            //{
            //    Description = $"Basic 授权模式",
            //    Type = NSwag.OpenApiSecuritySchemeType.Basic
            //});
            ////貌似同Basic模式
            ////document.AddSecurity("Http", new[] { "skoruba_identity_admin_api" }, new NSwag.OpenApiSecurityScheme
            ////{
            ////    Description = $"Http 授权模式",
            ////    Type = NSwag.OpenApiSecuritySchemeType.Http
            ////});
            //document.AddSecurity("ApiKey_Header"/*, new[] { "skoruba_identity_admin_api" }*/, new NSwag.OpenApiSecurityScheme
            //{
            //    Description = $"ApiKey 授权模式,In 可选 Header 或 Query",
            //    Name = "Authorization",
            //    Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
            //    In = NSwag.OpenApiSecurityApiKeyLocation.Header
            //    //或者
            //    //In = NSwag.OpenApiSecurityApiKeyLocation.Query
            //});
            //document.AddSecurity("ApiKey_Query", new[] { "skoruba_identity_admin_api" }, new NSwag.OpenApiSecurityScheme
            //{
            //    Description = $"ApiKey 授权模式,In 可选 Header 或 Query",
            //    Name = "Authorization",
            //    Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
            //    //In = NSwag.OpenApiSecurityApiKeyLocation.Header
            //    //或者
            //    In = NSwag.OpenApiSecurityApiKeyLocation.Query
            //});
            #endregion 基本身份认证
            #region OAuth2
            //var rsss = new System.Collections.Generic.Dictionary<string, string>
            //{
            //    { "skoruba_identity_admin_api", "Skoruba IdentityServer4 Admin Api" }
            //};

            //document.AddSecurity("OAuth2_Implicit", new[] { "skoruba_identity_admin_api" }, new NSwag.OpenApiSecurityScheme
            //{
            //    Description = $"OAuth2_Implicit 简化模式,\r\n测试ClientId:'Test_Implicit_Development'",
            //    Type = NSwag.OpenApiSecuritySchemeType.OAuth2,
            //    Flow = NSwag.OpenApiOAuth2Flow.Implicit,
            //    Scopes = rsss,
            //    AuthorizationUrl = "http://ser_identity.service.erp.consul/connect/authorize"
            //});

            //document.AddSecurity("OAuth2_Implicit", new[] { "skoruba_identity_admin_api" }, new NSwag.OpenApiSecurityScheme
            //{
            //    Description = $"OAuth2_Implicit 简化模式,\r\n测试ClientId:'Test_Implicit_Development'",
            //    Type = NSwag.OpenApiSecuritySchemeType.OAuth2,
            //    Flow = NSwag.OpenApiOAuth2Flow.Implicit,
            //    Scopes = rsss,
            //    AuthorizationUrl = "http://ser_identity.service.erp.consul/connect/authorize"
            //});
            //document.AddSecurity("OAuth2_Password", new[] { "skoruba_identity_admin_api" }, new NSwag.OpenApiSecurityScheme
            //{
            //    Description = $"OAuth2_Password 密码模式,\r\n测试ClientId:'Test_Password_Development'\r\n测试ClientSecret:'test'",
            //    Type = NSwag.OpenApiSecuritySchemeType.OAuth2,
            //    Flow = NSwag.OpenApiOAuth2Flow.Password,
            //    Scopes = rsss,
            //    TokenUrl = "http://ser_identity.service.erp.consul/connect/token"
            //});
            //document.AddSecurity("OAuth2_Application", new[] { "skoruba_identity_admin_api" }, new NSwag.OpenApiSecurityScheme
            //{
            //    Description = $"OAuth2_Application 客户端模式,\r\n测试ClientId:'Test_ClientCredentials_Development'\r\n测试ClientSecret:'test'",
            //    Type = NSwag.OpenApiSecuritySchemeType.OAuth2,
            //    Flow = NSwag.OpenApiOAuth2Flow.Application,
            //    Scopes = rsss,
            //    TokenUrl = "http://ser_identity.service.erp.consul/connect/token"
            //});
            //document.AddSecurity("OAuth2_AccessCode", new[] { "skoruba_identity_admin_api" }, new NSwag.OpenApiSecurityScheme
            //{
            //    Description = $"OAuth2_AccessCode 授权码模式\r\n测试ClientId:'Test_AuthorizationCode_Development'\r\n测试ClientSecret:'test'",
            //    Type = NSwag.OpenApiSecuritySchemeType.OAuth2,
            //    Flow = NSwag.OpenApiOAuth2Flow.AccessCode,
            //    Scopes = rsss,
            //    AuthorizationUrl = "http://ser_identity.service.erp.consul/connect/authorize",
            //    TokenUrl = "http://ser_identity.service.erp.consul/connect/token",
            //    //OpenIdConnectUrl = "http://ser_identity.service.erp.consul/connect/token"
            //});

            //document.AddSecurity("OAuth2_Bearer", Enumerable.Empty<string>(), new NSwag.OpenApiSecurityScheme
            //{
            //    Type = NSwag.OpenApiSecuritySchemeType.OAuth2,
            //    Description = $"OAuth2_Implicit 授权模式,\r\n测试ClientId:'Test_Implicit_Development'",
            //    Flow = NSwag.OpenApiOAuth2Flow.Implicit,
            //    Flows = new NSwag.OpenApiOAuthFlows()
            //    {
            //        Implicit = new NSwag.OpenApiOAuthFlow()
            //        {
            //            Scopes = rsss,
            //            AuthorizationUrl = "http://ser_identity.service.erp.consul/connect/authorize",
            //            //TokenUrl = "http://ser_identity.service.erp.consul/connect/token"
            //        },
            //    }
            //});
            #endregion OAuth2
        }