Example #1
0
        private Operation CreateOperation(ApiDescription apiDesc, SchemaRegistry schemaRegistry, HashSet <string> operationNames)
        {
            var parameters = apiDesc.ParameterDescriptions
                             .Select(paramDesc =>
            {
                string location = GetParameterLocation(apiDesc, paramDesc);
                return(CreateParameter(location, paramDesc, schemaRegistry));
            })
                             .ToList();

            var responses    = new Dictionary <string, Response>();
            var responseType = apiDesc.ResponseType();

            if (responseType == null || responseType == typeof(void))
            {
                responses.Add("204", new Response {
                    description = "No Content"
                });
            }
            else
            {
                responses.Add("200", new Response {
                    description = "OK", schema = schemaRegistry.GetOrRegister(responseType)
                });
            }

            var operation = new Operation
            {
                tags        = new[] { _options.GroupingKeySelector(apiDesc) },
                operationId = this.GetUniqueFriendlyId(apiDesc, operationNames),
                produces    = apiDesc.Produces().ToList(),
                consumes    = apiDesc.Consumes().ToList(),
                parameters  = parameters.Any() ? parameters : null, // parameters can be null but not empty
                responses   = responses,
                deprecated  = apiDesc.IsObsolete() ? true : (bool?)null
            };

            foreach (var filter in _options.OperationFilters)
            {
                filter.Apply(operation, schemaRegistry, apiDesc);
            }

            return(operation);
        }
Example #2
0
        public SwaggerDocument GetSwagger(string rootUrl, string apiVersion)
        {
            var schemaRegistry = new SchemaRegistry(_jsonSerializerSettings, _options);

            Info info;

            _apiVersions.TryGetValue(apiVersion, out info);
            if (info == null)
            {
                throw new UnknownApiVersion(apiVersion);
            }
            info.swaggerNetVersion = SwaggerAssemb.Version;

            HashSet <string> operationNames = new HashSet <string>();
            var apiDescriptions             = GetApiDescriptionsFor(apiVersion)
                                              .Where(apiDesc => !(_options.IgnoreObsoleteActions && apiDesc.IsObsolete()))
                                              .ToList();

            var paths = apiDescriptions
                        .OrderBy(_options.GroupingKeySelector, _options.GroupingKeyComparer)
                        .GroupBy(apiDesc => apiDesc.RelativePathSansQueryString())
                        .ToDictionary(group => "/" + group.Key, group => CreatePathItem(group, schemaRegistry, operationNames));

            var tags = apiDescriptions
                       .OrderBy(_options.GroupingKeySelector, _options.GroupingKeyComparer)
                       .Select(a => new Tag {
                description = a.Documentation, name = _options.GroupingKeySelector(a)
            })
                       .Distinct(new TagNameEqualityComparer())
                       .ToList();

            var controllers = apiDescriptions
                              .GroupBy(x => x.ActionDescriptor.ControllerDescriptor)
                              .Select(x => new
            {
                name    = x.Key.ControllerName,
                context = new ModelFilterContext(x.Key.ControllerType, null, null)
            });

            foreach (var filter in _options.ModelFilters)
            {
                foreach (var c in controllers)
                {
                    var model = new Schema();
                    filter.Apply(model, c.context);
                    if (!string.IsNullOrEmpty(model.description))
                    {
                        var ftags = tags.Where(t => t.name.Equals(c.name));
                        if (ftags != null && ftags.Count() > 0)
                        {
                            ftags.First().description = model.description;
                        }
                    }
                }
            }

            var rootUri = new Uri(rootUrl);
            var port    = (!rootUri.IsDefaultPort) ? ":" + rootUri.Port : string.Empty;

            var swaggerDoc = new SwaggerDocument
            {
                info     = info,
                host     = rootUri.Host + port,
                basePath = (rootUri.AbsolutePath != "/") ? rootUri.AbsolutePath : null,
                tags     = tags,
                schemes  = (_options.Schemes != null) ? _options.Schemes.ToList() : new[] { rootUri.Scheme }.ToList(),
                paths               = paths,
                definitions         = schemaRegistry.Definitions,
                securityDefinitions = _options.SecurityDefinitions
            };

            foreach (var filter in _options.DocumentFilters)
            {
                filter.Apply(swaggerDoc, schemaRegistry, _apiExplorer);
            }

            return(swaggerDoc);
        }