private Operation CreateOperation( ApiDescription apiDescription, ISchemaRegistry schemaRegistry) { var operation = new Operation { Tags = new[] { _settings.TagSelector(apiDescription) }, OperationId = apiDescription.FriendlyId(), Consumes = apiDescription.SupportedRequestMediaTypes().ToList(), Produces = apiDescription.SupportedResponseMediaTypes().ToList(), Parameters = CreateParameters(apiDescription, schemaRegistry), Responses = CreateResponses(apiDescription, schemaRegistry), Deprecated = apiDescription.IsObsolete() ? true : (bool?)null }; var filterContext = new OperationFilterContext( apiDescription, schemaRegistry); foreach (var filter in _settings.OperationFilters) { filter.Apply(operation, filterContext); } return(operation); }
public string GetUniqueOperationId(ApiDescription apiDesc, HashSet <string> operationNames) { string operationId; if (_options.OperationIdResolver != null) { operationId = _options.OperationIdResolver(apiDesc); } else { // default behaviour operationId = apiDesc.FriendlyId(); if (operationNames.Contains(operationId)) { operationId = apiDesc.FriendlyId2(); } var nextFriendlyIdPostfix = 1; while (operationNames.Contains(operationId)) { operationId = $"{apiDesc.FriendlyId2()}_{nextFriendlyIdPostfix}"; nextFriendlyIdPostfix++; } } operationNames.Add(operationId); return(operationId); }
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(); //parameters.Add(new Parameter() //{ // @in = "header", // description = "Custom header values", // ignore = false, // name = "SomeHeaderValue", // schema = new Schema() // { // type = "array", // // items = new Schema() { type = "string" } // } //}); 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(), // customRouteName = apiDescription.GetSwaggerRouteName(), 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); }
private Operation CreateOperation(ApiDescription apiDescription, SchemaRegistry schemaRegistry) { Contract.Requires(apiDescription != null); Contract.Requires(schemaRegistry != null); var parameters = apiDescription.ParameterDescriptions .Select(paramDesc => { var inPath = apiDescription.RelativePathSansQueryString().Contains("{" + paramDesc.Name + "}"); var swaggerApiParameterDescription = paramDesc as SwaggerApiParameterDescription; return(swaggerApiParameterDescription != null ? CreateParameter(swaggerApiParameterDescription, inPath, schemaRegistry) : 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.GetOrRegisterODataType(responseType) }); } var operation = new Operation { summary = apiDescription.Documentation, 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); }
private Operation CreateOperation(ApiDescription apiDesc, SchemaRegistry schemaRegistry, float apiVersion) { 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 = apiDesc.FriendlyId(), produces = apiDesc.Produces().ToList(), consumes = apiDesc.Consumes().ToList(), parameters = parameters.Any() ? parameters : new List<Parameter>(), // parameters can be null but not empty responses = responses, deprecated = apiDesc.IsObsolete() ? true : (bool?)null }; if (apiVersion>2) { operation.parameters.Insert(0, new Parameter { name = "JWT", @in = "header", description = "Json Web Token", required = false, type = "string" }); } else { //appInfo控制器排除 if (apiDesc.ActionDescriptor.ControllerDescriptor.ControllerName!=nameof(Controllers.v1.AppInfoController)) { operation.parameters.Insert(0, new Parameter { name = "AppKey", @in = "header", description = "客户端标识", required = false, type = "string" }); operation.parameters.Insert(1, new Parameter { name = "Sign", @in = "header", description = "签名", required = false, type = "string" }); } } foreach (var filter in _options.OperationFilters) { filter.Apply(operation, schemaRegistry, apiDesc); } return operation; }
private Operation CreateOperation(ApiDescription apiDesc, SchemaRegistry schemaRegistry) { var parameters = apiDesc.ParameterDescriptions .Where(paramDesc => { var paramDescriptor = paramDesc.ParameterDescriptor; return(paramDescriptor == null || paramDescriptor.ParameterType != typeof(CancellationToken)); }) .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 = apiDesc.FriendlyId(), 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); }
private Operation CreateOperation(ApiDescription apiDescription, ISchemaRegistry schemaRegistry) { var groupName = _options.GroupNameSelector(apiDescription); var parameters = apiDescription.ParameterDescriptions .Where(paramDesc => paramDesc.Source.IsFromRequest) .Select(paramDesc => CreateParameter(paramDesc, schemaRegistry)) .ToList(); var responses = new Dictionary <string, Response>(); if (!apiDescription.SupportedResponseTypes.Any()) { responses.Add("204", new Response { Description = "No Content" }); } else { var description = apiDescription.SupportedResponseTypes.OrderBy(responseType => responseType.StatusCode).FirstOrDefault(); responses.Add("200", CreateSuccessResponse(description.Type, schemaRegistry)); } var operation = new Operation { Tags = (groupName != null) ? new[] { groupName } : null, OperationId = apiDescription.FriendlyId(), Consumes = apiDescription.SupportedRequestMediaTypes().ToList(), Produces = apiDescription.SupportedResponseMediaTypes().ToList(), Parameters = parameters.Any() ? parameters : null, // parameters can be null but not empty Responses = responses, Deprecated = apiDescription.IsObsolete() }; var filterContext = new OperationFilterContext(apiDescription, schemaRegistry); foreach (var filter in _options.OperationFilters) { filter.Apply(operation, filterContext); } return(operation); }
public string GetUniqueFriendlyId(ApiDescription apiDesc, HashSet <string> operationNames) { string friendlyId = apiDesc.FriendlyId(); if (operationNames.Contains(friendlyId)) { friendlyId = apiDesc.FriendlyId2(); } int nextFriendlyIdPostfix = 1; while (operationNames.Contains(friendlyId)) { friendlyId = string.Format("{0}_{1}", apiDesc.FriendlyId2(), nextFriendlyIdPostfix); nextFriendlyIdPostfix++; } operationNames.Add(friendlyId); return(friendlyId); }
private Operation CreateOperation(ApiDescription apiDescription, ISchemaRegistry schemaRegistry) { var parameters = apiDescription.ParameterDescriptions .Where(paramDesc => { return(paramDesc.Source.IsFromRequest && (paramDesc.ModelMetadata == null || paramDesc.ModelMetadata.IsBindingAllowed) && !paramDesc.IsPartOfCancellationToken()); }) .Select(paramDesc => CreateParameter(apiDescription, paramDesc, schemaRegistry)) .ToList(); var responses = apiDescription.SupportedResponseTypes .DefaultIfEmpty(new ApiResponseType { StatusCode = 200 }) .ToDictionary( apiResponseType => apiResponseType.StatusCode.ToString(), apiResponseType => CreateResponse(apiResponseType, schemaRegistry) ); var operation = new Operation { Tags = new[] { _settings.TagSelector(apiDescription) }, OperationId = apiDescription.FriendlyId(), Consumes = apiDescription.SupportedRequestMediaTypes().ToList(), Produces = apiDescription.SupportedResponseMediaTypes().ToList(), Parameters = parameters.Any() ? parameters : null, // parameters can be null but not empty Responses = responses, Deprecated = apiDescription.IsObsolete() ? true : (bool?)null }; var filterContext = new OperationFilterContext( apiDescription, schemaRegistry); foreach (var filter in _settings.OperationFilters) { filter.Apply(operation, filterContext); } return(operation); }
private Operation CreateOperation( ApiDescription apiDesc, SchemaRegistry schemaRegistry) { List <Parameter> list = ((IEnumerable <ApiParameterDescription>)apiDesc.get_ParameterDescriptions()).Select <ApiParameterDescription, Parameter>((Func <ApiParameterDescription, Parameter>)(paramDesc => this.CreateParameter(this.GetParameterLocation(apiDesc, paramDesc), paramDesc, schemaRegistry))).ToList <Parameter>(); Dictionary <string, Response> dictionary = new Dictionary <string, Response>(); Type type = apiDesc.ResponseType(); if (type == (Type)null || type == typeof(void)) { dictionary.Add("204", new Response() { description = "No Content" }); } else { dictionary.Add("200", new Response() { description = "OK", schema = schemaRegistry.GetOrRegister(type) }); } Operation operation = new Operation() { tags = (IList <string>) new string[1] { this._options.GroupingKeySelector(apiDesc) }, operationId = apiDesc.FriendlyId(), produces = (IList <string>)apiDesc.Produces().ToList <string>(), consumes = (IList <string>)apiDesc.Consumes().ToList <string>(), parameters = list.Any <Parameter>() ? (IList <Parameter>)list : (IList <Parameter>)null, responses = (IDictionary <string, Response>)dictionary, deprecated = apiDesc.IsObsolete() ? new bool?(true) : new bool?() }; foreach (IOperationFilter operationFilter in this._options.OperationFilters) { operationFilter.Apply(operation, schemaRegistry, apiDesc); } return(operation); }
private Operation CreateOperation( ApiDescription apiDescription, ISchemaRegistry schemaRegistry) { // Try to retrieve additional metadata that's not provided by ApiExplorer MethodInfo methodInfo; var customAttributes = Enumerable.Empty <object>(); if (apiDescription.TryGetMethodInfo(out methodInfo)) { customAttributes = methodInfo.GetCustomAttributes(true) .Union(methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(true)); } var isDeprecated = customAttributes.Any(attr => attr.GetType() == typeof(ObsoleteAttribute)); var operation = new Operation { Tags = new[] { _settings.TagSelector(apiDescription) }, OperationId = apiDescription.FriendlyId(), Consumes = apiDescription.SupportedRequestMediaTypes().ToList(), Produces = apiDescription.SupportedResponseMediaTypes().ToList(), Parameters = CreateParameters(apiDescription, schemaRegistry), Responses = CreateResponses(apiDescription, schemaRegistry), Deprecated = isDeprecated ? true : (bool?)null }; var filterContext = new OperationFilterContext( apiDescription, schemaRegistry, methodInfo); foreach (var filter in _settings.OperationFilters) { filter.Apply(operation, filterContext); } return(operation); }
private Operation CreateOperation(ApiDescription apiDescription, ISchemaRegistry schemaRegistry) { var groupName = _options.GroupNameSelector(apiDescription); var parameters = apiDescription.ParameterDescriptions .Where(paramDesc => paramDesc.Source.IsFromRequest) .Select(paramDesc => CreateParameter(paramDesc, schemaRegistry)) .ToList(); var responses = apiDescription.SupportedResponseTypes .DefaultIfEmpty(new ApiResponseType { StatusCode = 200 }) .ToDictionary( apiResponseType => apiResponseType.StatusCode.ToString(), apiResponseType => CreateResponse(apiResponseType, schemaRegistry) ); var operation = new Operation { Tags = (groupName != null) ? new[] { groupName } : null, OperationId = apiDescription.FriendlyId(), Consumes = apiDescription.SupportedRequestMediaTypes().ToList(), Produces = apiDescription.SupportedResponseMediaTypes().ToList(), Parameters = parameters.Any() ? parameters : null, // parameters can be null but not empty Responses = responses, Deprecated = apiDescription.IsObsolete() }; var filterContext = new OperationFilterContext(apiDescription, schemaRegistry); foreach (var filter in _options.OperationFilters) { filter.Apply(operation, filterContext); } return(operation); }
/// <summary> /// Applies a change to a given Swagger API operation /// </summary> /// <param name="operation">API operation being changed</param> /// <param name="schemaRegistry">not used</param> /// <param name="apiDescription">API description that provides filter descriptions</param> public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) { // handle image gets if (apiDescription.FriendlyId() == "Images_GetImage") { operation.produces = imageMimeTypes; operation.responses["200"].schema = new Schema { type = "file", description = "MIME encoded contents of the image" }; } // handle image posts if (apiDescription.FriendlyId() == "Images_PostImage") { // add acceptable image mime types foreach (string imageMimeType in imageMimeTypes) { operation.consumes.Add(imageMimeType); } Parameter image = new Parameter() { name = "image", @in = "body", required = true, schema = new Schema { type = "file", format = "file" }, description = "MIME encoded contents of the image" }; if (operation.parameters == null) { operation.parameters = new List <Parameter>(); } operation.parameters.Add(image); } // handle blob gets if (apiDescription.FriendlyId() == "Blobs_GetBlob") { operation.produces = blobMimeTypes; operation.responses["200"].schema = new Schema { type = "file", description = "MIME encoded contents of the blob" }; } // handle blob posts if (apiDescription.FriendlyId() == "Blobs_PostBlob") { // add acceptable blob mime types foreach (string blobMimeType in blobMimeTypes) { operation.consumes.Add(blobMimeType); } Parameter blob = new Parameter() { name = "blob", @in = "body", required = true, schema = new Schema { type = "file", format = "file" }, description = "MIME encoded contents of the blob" }; if (operation.parameters == null) { operation.parameters = new List <Parameter>(); } operation.parameters.Add(blob); } }