public SwashbuckleConfig(
     IApiDescriptionGroupCollectionProvider apiDescriptionGroupCollectionProvider,
     IOptions <Option> functionsOptions,
     SwashBuckleStartupConfig startupConfig,
     IOptions <HttpOptions> httpOptions)
 {
     _apiDescriptionGroupCollectionProvider = apiDescriptionGroupCollectionProvider;
     _option      = functionsOptions.Value;
     _httpOptions = httpOptions.Value;
     if (!string.IsNullOrWhiteSpace(_option.XmlPath))
     {
         var binPath      = Path.GetDirectoryName(startupConfig.Assembly.Location);
         var binDirectory = Directory.CreateDirectory(binPath);
         var xmlBasePath  = binDirectory?.Parent?.FullName;
         var xmlPath      = Path.Combine(xmlBasePath, _option.XmlPath);
         if (File.Exists(xmlPath))
         {
             _xmlPath = xmlPath;
         }
     }
     _indexHtmLazy = new Lazy <string>(() => IndexHtml.Value.Replace("{title}", _option.Title));
 }
Esempio n. 2
0
        public FunctionApiDescriptionProvider(
            IOptions <Option> functionsOptions,
            SwashBuckleStartupConfig startupConfig,
            IModelMetadataProvider modelMetadataProvider,
            IOutputFormatter outputFormatter,
            IOptions <HttpOptions> httOptions)
        {
            _option = functionsOptions.Value;
            _modelMetadataProvider = modelMetadataProvider;
            _outputFormatter       = outputFormatter;

            var methods = startupConfig.Assembly.GetTypes()
                          .SelectMany(t => t.GetMethods())
                          .Where(m => m.GetCustomAttributes(typeof(FunctionNameAttribute), false).Any())
                          .ToArray();

            var apiDescGroups = new Dictionary <string, List <ApiDescription> >();

            foreach (var methodInfo in methods)
            {
                if (!TryGetHttpTrigger(methodInfo, out var triggerAttribute))
                {
                    continue;
                }

                var functionAttr =
                    (FunctionNameAttribute)methodInfo.GetCustomAttribute(typeof(FunctionNameAttribute), false);
                var apiExplorerSettingsAttribute =
                    (ApiExplorerSettingsAttribute)methodInfo.GetCustomAttribute(typeof(ApiExplorerSettingsAttribute), false);
                var prefix = string.IsNullOrWhiteSpace(httOptions.Value.RoutePrefix) ? "" : $"{httOptions.Value.RoutePrefix.TrimEnd('/')}/";
                var route  =
                    $"{prefix}{(!string.IsNullOrWhiteSpace(triggerAttribute.Route) ? triggerAttribute.Route : functionAttr.Name)}";

                var routes = new List <(string Route, string RemoveParamName)>();

                var regex = new Regex("/\\{(?<paramName>\\w+)\\?\\}$");
                var match = regex.Match(route);

                if (match.Success && match.Captures.Count == 1)
                {
                    routes.Add((route.Replace(match.Value, "").Replace("//", "/"), match.Groups["paramName"].ToString()));
                    routes.Add((route.Replace(match.Value, match.Value.Replace("?", "")), ""));
                }
                else
                {
                    routes.Add((route, ""));
                }
                var verbs = triggerAttribute.Methods ??
                            new[] { "get", "post", "delete", "head", "patch", "put", "options" };


                for (var index = 0; index < routes.Count; index++)
                {
                    var r       = routes[index];
                    var apiName = functionAttr.Name + (index == 0 ? "" : $"-{index}");
                    var items   = verbs.Select(verb =>
                                               CreateDescription(methodInfo, r.Route, functionAttr, apiExplorerSettingsAttribute, verb, triggerAttribute.AuthLevel, r.RemoveParamName)).ToArray();

                    string groupName =
                        (items.FirstOrDefault()?.ActionDescriptor as ControllerActionDescriptor)?.ControllerName ?? apiName;
                    if (!apiDescGroups.ContainsKey(groupName))
                    {
                        apiDescGroups[groupName] = new List <ApiDescription>();
                    }

                    apiDescGroups[groupName].AddRange(items);
                }
            }

            ApiDescriptionGroups =
                new ApiDescriptionGroupCollection(
                    new ReadOnlyCollection <ApiDescriptionGroup>(
                        apiDescGroups.Select(kv => new ApiDescriptionGroup(kv.Key, kv.Value)).ToList()
                        ), 1);
        }