private string GetOperationId(ConsulOption document, string controllerName, MethodInfo method) { string operationId; dynamic swaggerOperationAttribute = method .GetCustomAttributes() .FirstAssignableToTypeNameOrDefault("SwaggerOperationAttribute", TypeNameStyle.Name); if (swaggerOperationAttribute != null && !string.IsNullOrEmpty(swaggerOperationAttribute.OperationId)) { operationId = swaggerOperationAttribute.OperationId; } else { if (controllerName.EndsWith("Controller")) { controllerName = controllerName.Substring(0, controllerName.Length - 10); } operationId = controllerName + "_" + GetActionName(method); } var number = 1; //while (document.Operations.Any(o => o.Operation.OperationId == operationId + (number > 1 ? "_" + number : string.Empty))) //{ // number++; //} return(operationId + (number > 1 ? number.ToString() : string.Empty)); }
public string GetSectionString() { var types = ConsulGenerator.GetControllerClasses(typeof(Program).Assembly); ConsulGenerator generator = new ConsulGenerator(); ConsulOption option = generator.GenerateForControllers(types); return(JsonConvert.SerializeObject(option)); }
/// <summary>Generates a Swagger specification for the given controller types.</summary> /// <param name="controllerTypes">The types of the controller.</param> /// <returns>The <see cref="OpenApiDocument" />.</returns> /// <exception cref="InvalidOperationException">The operation has more than one body parameter.</exception> public ConsulOption GenerateForControllers(IEnumerable <Type> controllerTypes) { ConsulOption option = new ConsulOption(); var usedControllerTypes = new List <Type>(); foreach (var controllerType in controllerTypes) { var isIncluded = GenerateForController(option, controllerType); if (isIncluded) { usedControllerTypes.Add(controllerType); } } return(option); }
/// <exception cref="InvalidOperationException">The operation has more than one body parameter.</exception> private bool GenerateForController(ConsulOption option, Type controllerType) { var hasIgnoreAttribute = controllerType.GetTypeInfo() .GetCustomAttributes() .GetAssignableToTypeName("ConsulIgnoreAttribute", TypeNameStyle.Name) .Any(); if (hasIgnoreAttribute) { return(false); } var operations = new List <Tuple <OpenApiOperationDescription, MethodInfo> >(); var currentControllerType = controllerType; ConsulOption.CheckItem item; while (currentControllerType != null) { foreach (var method in GetActionMethods(currentControllerType)) { var httpPaths = GetHttpPaths(controllerType, method).ToList(); var httpMethods = GetSupportedHttpMethods(method).ToList(); foreach (var httpPath in httpPaths) { foreach (var httpMethod in httpMethods) { var isPathAlreadyDefinedInInheritanceHierarchy = operations.Any(o => o.Item1.Path == httpPath && o.Item1.Method == httpMethod && o.Item2.DeclaringType != currentControllerType && o.Item2.DeclaringType.IsAssignableToTypeName(currentControllerType.FullName, TypeNameStyle.FullName)); if (isPathAlreadyDefinedInInheritanceHierarchy == false) { var operationDescription = new OpenApiOperationDescription { Path = httpPath, Method = httpMethod, Operation = new { IsDeprecated = method.GetCustomAttribute <ObsoleteAttribute>() != null } }; operations.Add(new Tuple <OpenApiOperationDescription, MethodInfo>(operationDescription, method)); #region 添加Consul逻辑 item = new ConsulOption.CheckItem() { Url = operationDescription.Path }; option.Checks.Add(item); #endregion } } } } currentControllerType = currentControllerType.GetTypeInfo().BaseType; } return(AddOperationDescriptionsToDocument(currentControllerType, operations)); }