public SwaggerService Generate(Type type, InterfaceMapping map, string excludedMethodName = "Swagger", string controllernameused = null) { _service = new SwaggerService(); _serviceType = type; _exceptionType = new JsonSchema4(); _exceptionType.TypeName = "SwaggerException"; _exceptionType.Properties.Add("ExceptionType", new JsonProperty { Type = JsonObjectType.String }); _exceptionType.Properties.Add("Message", new JsonProperty { Type = JsonObjectType.String }); _exceptionType.Properties.Add("StackTrace", new JsonProperty { Type = JsonObjectType.String }); _service.Definitions[_exceptionType.TypeName] = _exceptionType; var schemaResolver = new SchemaResolver(); var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (var method in methods.Where(m => m.Name != excludedMethodName)) { if (Array.IndexOf(map.TargetMethods, method) == -1) { continue; } var parameters = method.GetParameters().ToList(); var methodName = method.Name; var operation = new SwaggerOperation(); operation.OperationId = methodName; var httpPath = GetHttpPath(operation, method, parameters, schemaResolver, controllernameused); LoadParameters(operation, parameters, schemaResolver); LoadReturnType(operation, method, schemaResolver); LoadMetaData(operation, method); var httpMethod = GetMethod(method); if (!_service.Paths.ContainsKey(httpPath)) { var path = new SwaggerOperations(); _service.Paths[httpPath] = path; } _service.Paths[httpPath][httpMethod] = operation; } foreach (var schema in schemaResolver.Schemes) { _service.Definitions[schema.TypeName] = schema; } _service.GenerateOperationIds(); return(_service); }
/// <exception cref="InvalidOperationException">The operation has more than one body parameter.</exception> private void GenerateForController(SwaggerService service, Type controllerType, string excludedMethodName, SchemaResolver schemaResolver) { foreach (var method in GetActionMethods(controllerType, excludedMethodName)) { var operation = new SwaggerOperation { IsDeprecated = method.GetCustomAttribute <ObsoleteAttribute>() != null }; var parameters = method.GetParameters().ToList(); var httpPath = GetHttpPath(service, operation, controllerType, method, parameters, schemaResolver); LoadParameters(service, operation, parameters, schemaResolver); LoadReturnType(service, operation, method, schemaResolver); LoadMetaData(operation, method); LoadOperationTags(method, operation, controllerType); operation.OperationId = GetOperationId(service, controllerType.Name, method); foreach (var httpMethod in GetSupportedHttpMethods(method)) { if (!service.Paths.ContainsKey(httpPath)) { var path = new SwaggerOperations(); service.Paths[httpPath] = path; } service.Paths[httpPath][httpMethod] = operation; } } AppendRequiredSchemasToDefinitions(service, schemaResolver); }
/// <exception cref="InvalidOperationException">The operation has more than one body parameter.</exception> private void GenerateForController(SwaggerService service, Type controllerType, string excludedMethodName, SchemaResolver schemaResolver) { var methods = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (var method in methods.Where(m => m.Name != excludedMethodName)) { var parameters = method.GetParameters().ToList(); var methodName = method.Name; var operation = new SwaggerOperation(); operation.OperationId = methodName; var httpPath = GetHttpPath(service, operation, controllerType, method, parameters, schemaResolver); LoadParameters(service, operation, parameters, schemaResolver); LoadReturnType(service, operation, method, schemaResolver); LoadMetaData(operation, method); foreach (var httpMethod in GetSupportedHttpMethods(method)) { if (!service.Paths.ContainsKey(httpPath)) { var path = new SwaggerOperations(); service.Paths[httpPath] = path; } service.Paths[httpPath][httpMethod] = operation; } } foreach (var schema in schemaResolver.Schemes) { service.Definitions[schema.TypeName] = schema; } }
public async Task ProcessAsync(DocumentProcessorContext context) { var operationsToRemove = new List <string>(); var operationsToAdd = new Dictionary <string, SwaggerOperations>(); foreach (var operation in context.Document.Paths) { if (!operation.Key.Contains(UrlTypeParameter)) { continue; } operationsToRemove.Add(operation.Key); foreach (var type in AnimalTypeMapper.Maps) { var path = operation.Key.Replace(UrlTypeParameter, type.Key); var newOperations = new SwaggerOperations(); foreach (var response in operation.Value) { newOperations.Add(response.Key, await OperationBuilder.BuildOperationAsync(type.Key, response.Value, TypeParameter)); } operationsToAdd.Add(path, newOperations); } } foreach (var remove in operationsToRemove) { context.Document.Paths.Remove(remove); } foreach (var newOperation in operationsToAdd) { context.Document.Paths.Add(newOperation.Key, newOperation.Value); } }
/// <exception cref="InvalidOperationException">The operation has more than one body parameter.</exception> private void GenerateForController(SwaggerService service, Type controllerType, string excludedMethodName, SchemaResolver schemaResolver) { var methods = controllerType.GetRuntimeMethods().Where(m => m.IsPublic); foreach (var method in methods.Where(m => m.Name != excludedMethodName && m.IsSpecialName == false && // avoid property methods m.DeclaringType != null && m.DeclaringType != typeof(object) && m.GetCustomAttributes().All(a => a.GetType().Name != "SwaggerIgnoreAttribute") && m.DeclaringType.FullName.StartsWith("Microsoft.AspNet") == false && // .NET Core (Web API & MVC) m.DeclaringType.FullName != "System.Web.Http.ApiController" && m.DeclaringType.FullName != "System.Web.Mvc.Controller")) { var parameters = method.GetParameters().ToList(); var operation = new SwaggerOperation(); operation.IsDeprecated = method.GetCustomAttribute <ObsoleteAttribute>() != null; var httpPath = GetHttpPath(service, operation, controllerType, method, parameters, schemaResolver); LoadParameters(service, operation, parameters, schemaResolver); LoadReturnType(service, operation, method, schemaResolver); LoadMetaData(operation, method); operation.OperationId = GetOperationId(service, controllerType.Name, method); foreach (var httpMethod in GetSupportedHttpMethods(method)) { if (!service.Paths.ContainsKey(httpPath)) { var path = new SwaggerOperations(); service.Paths[httpPath] = path; } service.Paths[httpPath][httpMethod] = operation; } } foreach (var schema in schemaResolver.Schemes) { if (!service.Definitions.Values.Contains(schema)) { if (!service.Definitions.ContainsKey(schema.TypeName)) { service.Definitions[schema.TypeName] = schema; } else { service.Definitions["ref_" + Guid.NewGuid().ToString().Replace("-", "_")] = schema; } } } }