示例#1
0
        public async Task <SwaggerService> RunAsync()
        {
            return(await Task.Run(() =>
            {
                var generator = new WebApiAssemblyToSwaggerGenerator(Settings);

                var controllerNames = ControllerNames.ToList();
                if (!string.IsNullOrEmpty(ControllerName))
                {
                    controllerNames.Add(ControllerName);
                }

                controllerNames = controllerNames.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();
                if (!controllerNames.Any() && Settings.AssemblyPaths?.Length > 0)
                {
                    controllerNames = generator.GetControllerClasses().ToList();
                }

                var service = generator.GenerateForControllers(controllerNames);

                if (!string.IsNullOrEmpty(ServiceHost))
                {
                    service.Host = ServiceHost;
                }
                if (ServiceSchemes != null && ServiceSchemes.Any())
                {
                    service.Schemes = ServiceSchemes.Select(s => (SwaggerSchema)Enum.Parse(typeof(SwaggerSchema), s, true)).ToList();
                }

                return service;
            }));
        }
示例#2
0
        protected override async Task <string> RunIsolatedAsync(AssemblyLoader.AssemblyLoader assemblyLoader)
        {
            await TransformAsync(assemblyLoader);

            var controllerNames = ControllerNames.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();

            if (!controllerNames.Any() && AssemblyPaths?.Length > 0)
            {
                controllerNames = GetControllerNames(assemblyLoader).ToList();
            }

            var controllerTypes = await GetControllerTypesAsync(controllerNames, assemblyLoader);

            var generator = new WebApiToSwaggerGenerator(Settings);
            var document  = await generator.GenerateForControllersAsync(controllerTypes).ConfigureAwait(false);

            if (ServiceHost == ".")
            {
                document.Host = string.Empty;
            }
            else if (!string.IsNullOrEmpty(ServiceHost))
            {
                document.Host = ServiceHost;
            }

            if (string.IsNullOrEmpty(DocumentTemplate))
            {
                if (!string.IsNullOrEmpty(InfoTitle))
                {
                    document.Info.Title = InfoTitle;
                }
                if (!string.IsNullOrEmpty(InfoVersion))
                {
                    document.Info.Version = InfoVersion;
                }
                if (!string.IsNullOrEmpty(InfoDescription))
                {
                    document.Info.Description = InfoDescription;
                }
            }

            if (ServiceSchemes != null && ServiceSchemes.Any())
            {
                document.Schemes = ServiceSchemes.Select(s => (SwaggerSchema)Enum.Parse(typeof(SwaggerSchema), s, true)).ToList();
            }

            if (!string.IsNullOrEmpty(ServiceBasePath))
            {
                document.BasePath = ServiceBasePath;
            }

            return(document.ToJson());
        }
示例#3
0
        public async Task <SwaggerDocument> RunAsync()
        {
            return(await Task.Run(() =>
            {
                if (!string.IsNullOrEmpty(DocumentTemplate))
                {
                    if (DynamicApis.FileExists(DocumentTemplate))
                    {
                        Settings.DocumentTemplate = DynamicApis.FileReadAllText(DocumentTemplate);
                    }
                    else
                    {
                        Settings.DocumentTemplate = DocumentTemplate;
                    }
                }
                else
                {
                    Settings.DocumentTemplate = null;
                }

                var generator = CreateGenerator();
                var controllerNames = ControllerNames.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();
                if (!controllerNames.Any() && Settings.AssemblyPaths?.Length > 0)
                {
                    controllerNames = generator.GetControllerClasses().ToList();
                }

                var document = generator.GenerateForControllers(controllerNames);

                if (ServiceHost == ".")
                {
                    document.Host = string.Empty;
                }
                else if (!string.IsNullOrEmpty(ServiceHost))
                {
                    document.Host = ServiceHost;
                }

                if (ServiceSchemes != null && ServiceSchemes.Any())
                {
                    document.Schemes = ServiceSchemes.Select(s => (SwaggerSchema)Enum.Parse(typeof(SwaggerSchema), s, true)).ToList();
                }

                if (!string.IsNullOrEmpty(ServiceBasePath))
                {
                    document.BasePath = ServiceBasePath;
                }

                return document;
            }));
        }
示例#4
0
        public async Task <SwaggerDocument> RunAsync()
        {
            return(await Task.Run(async() =>
            {
                if (!string.IsNullOrEmpty(DocumentTemplate))
                {
                    if (await DynamicApis.FileExistsAsync(DocumentTemplate).ConfigureAwait(false))
                    {
                        Settings.DocumentTemplate = await DynamicApis.FileReadAllTextAsync(DocumentTemplate).ConfigureAwait(false);
                    }
                    else
                    {
                        Settings.DocumentTemplate = DocumentTemplate;
                    }

                    if (!string.IsNullOrEmpty(Settings.DocumentTemplate) && !Settings.DocumentTemplate.StartsWith("{"))
                    {
                        Settings.DocumentTemplate = (await SwaggerYamlDocument.FromYamlAsync(Settings.DocumentTemplate)).ToJson();
                    }
                }
                else
                {
                    Settings.DocumentTemplate = null;
                }

                var generator = new WebApiAssemblyToSwaggerGenerator(Settings);

                var controllerNames = ControllerNames.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();
                if (!controllerNames.Any() && Settings.AssemblySettings.AssemblyPaths?.Length > 0)
                {
                    controllerNames = generator.GetExportedControllerClassNames().ToList();
                }

                var document = await generator.GenerateForControllersAsync(controllerNames).ConfigureAwait(false);
                if (ServiceHost == ".")
                {
                    document.Host = string.Empty;
                }
                else if (!string.IsNullOrEmpty(ServiceHost))
                {
                    document.Host = ServiceHost;
                }

                if (!string.IsNullOrEmpty(InfoTitle))
                {
                    document.Info.Title = InfoTitle;
                }
                if (!string.IsNullOrEmpty(InfoVersion))
                {
                    document.Info.Version = InfoVersion;
                }
                if (!string.IsNullOrEmpty(InfoDescription))
                {
                    document.Info.Description = InfoDescription;
                }

                if (ServiceSchemes != null && ServiceSchemes.Any())
                {
                    document.Schemes = ServiceSchemes.Select(s => (SwaggerSchema)Enum.Parse(typeof(SwaggerSchema), s, true)).ToList();
                }

                if (!string.IsNullOrEmpty(ServiceBasePath))
                {
                    document.BasePath = ServiceBasePath;
                }

                return document;
            }));
        }