/// <inheritdoc /> public IDocument ApplyDocumentFilters(DocumentFilterCollection collection) { foreach (var filter in GenericExtensions.ThrowIfNullOrDefault(collection).DocumentFilters) { filter.Apply(this._req, this.OpenApiDocument); } return(this); }
/// <inheritdoc /> public IDocument AddServer(IHttpRequestDataObject req, string routePrefix, IOpenApiConfigurationOptions options = null) { this._req = req; var prefix = string.IsNullOrWhiteSpace(routePrefix) ? string.Empty : $"/{routePrefix}"; var baseUrl = $"{HttpRequestDataObjectExtensions.GetScheme(this._req, options)}://{this._req.Host}{prefix}"; var server = new OpenApiServer { Url = baseUrl }; if (GenericExtensions.IsNullOrDefault(options)) { this.OpenApiDocument.Servers = new List <OpenApiServer>() { server }; return(this); } // Filters out the existing base URLs that are the same as the current host URL. var servers = options.Servers .Where(p => p.Url.TrimEnd('/') != baseUrl.TrimEnd('/')) .ToList(); if (!servers.Any()) { servers.Insert(0, server); } if (options.IncludeRequestingHostName && !servers.Any(p => p.Url.TrimEnd('/') == baseUrl.TrimEnd('/'))) { servers.Insert(0, server); } this.OpenApiDocument.Servers = servers; return(this); }
/// <summary> /// Initializes a new instance of the <see cref="Document"/> class. /// </summary> public Document(IDocumentHelper helper) { this._helper = GenericExtensions.ThrowIfNullOrDefault(helper); }
/// <inheritdoc /> public IDocument Build(Assembly assembly, OpenApiVersionType version = OpenApiVersionType.V2) { if (GenericExtensions.IsNullOrDefault(this._strategy)) { this._strategy = new DefaultNamingStrategy(); } var paths = new OpenApiPaths(); var tags = StringExtensions.ToArray(this._req.Query["tag"], ","); var methods = this._helper.GetHttpTriggerMethods(assembly, tags); foreach (var method in methods) { var trigger = this._helper.GetHttpTriggerAttribute(method); if (GenericExtensions.IsNullOrDefault(trigger)) { continue; } var function = this._helper.GetFunctionNameAttribute(method); if (GenericExtensions.IsNullOrDefault(function)) { continue; } var path = this._helper.GetHttpEndpoint(function, trigger); if (StringExtensions.IsNullOrWhiteSpace(path)) { continue; } var verb = this._helper.GetHttpVerb(trigger); var item = this._helper.GetOpenApiPath(path, paths); var operations = item.Operations; var operation = this._helper.GetOpenApiOperation(method, function, verb); if (GenericExtensions.IsNullOrDefault(operation)) { continue; } operation.Security = this._helper.GetOpenApiSecurityRequirement(method, this._strategy); operation.Parameters = this._helper.GetOpenApiParameters(method, trigger, this._strategy, this._collection, version); operation.RequestBody = this._helper.GetOpenApiRequestBody(method, this._strategy, this._collection, version); operation.Responses = this._helper.GetOpenApiResponses(method, this._strategy, this._collection, version); operations[verb] = operation; item.Operations = operations; paths[path] = item; } this.OpenApiDocument.Paths = paths; this.OpenApiDocument.Components.Schemas = this._helper.GetOpenApiSchemas(methods, this._strategy, this._collection); this.OpenApiDocument.Components.SecuritySchemes = this._helper.GetOpenApiSecuritySchemes(methods, this._strategy); // this.OpenApiDocument.SecurityRequirements = this.OpenApiDocument // .Paths // .SelectMany(p => p.Value.Operations.SelectMany(q => q.Value.Security)) // .Where(p => !p.IsNullOrDefault()) // .Distinct(new OpenApiSecurityRequirementComparer()) // .ToList(); return(this); }
/// <inheritdoc /> public IDocument AddVisitors(VisitorCollection collection) { this._collection = GenericExtensions.ThrowIfNullOrDefault(collection); return(this); }
/// <inheritdoc /> public IDocument AddNamingStrategy(NamingStrategy strategy) { this._strategy = GenericExtensions.ThrowIfNullOrDefault(strategy); return(this); }