public ModelFilterContext( Type systemType, JsonObjectContract jsonObjectContract, SchemaRegistry schemaRegistry) { SystemType = systemType; JsonObjectContract = jsonObjectContract; SchemaRegistry = schemaRegistry; }
private PathItem CreatePathItem(IEnumerable<ApiDescription> apiDescriptions, SchemaRegistry schemaRegistry) { var pathItem = new PathItem(); // Group further by http method var perMethodGrouping = apiDescriptions .GroupBy(apiDesc => apiDesc.HttpMethod.Method.ToLower()); foreach (var group in perMethodGrouping) { var httpMethod = group.Key; var apiDescription = (group.Count() == 1) ? group.First() : _options.ConflictingActionsResolver(group); switch (httpMethod) { case "get": pathItem.get = CreateOperation(apiDescription, schemaRegistry); break; case "put": pathItem.put = CreateOperation(apiDescription, schemaRegistry); break; case "post": pathItem.post = CreateOperation(apiDescription, schemaRegistry); break; case "delete": pathItem.delete = CreateOperation(apiDescription, schemaRegistry); break; case "options": pathItem.options = CreateOperation(apiDescription, schemaRegistry); break; case "head": pathItem.head = CreateOperation(apiDescription, schemaRegistry); break; case "patch": pathItem.patch = CreateOperation(apiDescription, schemaRegistry); break; } } return pathItem; }
public SwaggerDocument GetSwagger(string rootUrl, string apiVersion) { var schemaRegistry = new SchemaRegistry( _jsonSerializerSettings, _options.CustomSchemaMappings, _options.SchemaFilters, _options.ModelFilters, _options.IgnoreObsoleteProperties, _options.SchemaIdSelector, _options.DescribeAllEnumsAsStrings, _options.DescribeStringEnumsInCamelCase); Info info; _apiVersions.TryGetValue(apiVersion, out info); if (info == null) throw new UnknownApiVersion(apiVersion); var paths = GetApiDescriptionsFor(apiVersion) .Where(apiDesc => !(_options.IgnoreObsoleteActions && apiDesc.IsObsolete())) .OrderBy(_options.GroupingKeySelector, _options.GroupingKeyComparer) .GroupBy(apiDesc => apiDesc.RelativePathSansQueryString()) .ToDictionary(group => "/" + group.Key, group => CreatePathItem(group, schemaRegistry)); 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, 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; }
private Parameter CreateParameter(ApiParameterDescription paramDesc, bool inPath, SchemaRegistry schemaRegistry) { var @in = (inPath) ? "path" : (paramDesc.Source == ApiParameterSource.FromUri) ? "query" : "body"; var parameter = new Parameter { name = paramDesc.Name, @in = @in }; if (paramDesc.ParameterDescriptor == null) { parameter.type = "string"; parameter.required = true; return parameter; } parameter.required = inPath || !paramDesc.ParameterDescriptor.IsOptional; parameter.@default = paramDesc.ParameterDescriptor.DefaultValue; var schema = schemaRegistry.GetOrRegister(paramDesc.ParameterDescriptor.ParameterType); if (parameter.@in == "body") parameter.schema = schema; else parameter.PopulateFrom(schema); return parameter; }
private Operation CreateOperation(ApiDescription apiDescription, SchemaRegistry schemaRegistry) { var parameters = apiDescription.ParameterDescriptions .Select(paramDesc => { var inPath = apiDescription.RelativePathSansQueryString().Contains("{" + paramDesc.Name + "}"); return CreateParameter(paramDesc, inPath, schemaRegistry); }) .ToList(); var responses = new Dictionary<string, Response>(); var responseType = apiDescription.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(apiDescription) }, operationId = apiDescription.FriendlyId(), produces = apiDescription.Produces().ToList(), consumes = apiDescription.Consumes().ToList(), parameters = parameters.Any() ? parameters : null, // parameters can be null but not empty responses = responses, deprecated = apiDescription.IsObsolete() }; foreach (var filter in _options.OperationFilters) { filter.Apply(operation, schemaRegistry, apiDescription); } return operation; }