Пример #1
0
        /// <summary>
        /// 生成文档结构
        /// </summary>
        /// <param name="type">具体的 AppService 类型</param>
        /// <param name="interfaceType">具体的 IAppService 类型</param>
        /// <param name="excludedMethodName">排除的方法名称</param>
        /// <returns></returns>
        public SwaggerServiceExtended GenerateForAbpAppService(Type type, Type interfaceType,
                                                               string excludedMethodName = "Swagger")
        {
            _service     = new SwaggerServiceExtended();
            _serviceType = type;
            var schemaResolver = new SchemaResolver();

            var deriveMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            var methods       = interfaceType.GetMethods();

            dynamic webApiDesc = interfaceType.GetCustomAttributes()
                                 .FirstOrDefault(x => x.GetType().Name == "WebApiDescriptionAttribute");

            if (webApiDesc != null)
            {
                dynamic name        = webApiDesc.Name;
                dynamic description = webApiDesc.Description;

                _service.Tags.Add(new SwaggerTagInfo
                {
                    Name        = name,
                    Description = description
                });
            }

            var hasGenerated = false;

            foreach (var method in methods.Where(m => m.Name != excludedMethodName))
            {
                //根据特性判断是否是公共的WebAPI方法
                var canOpen = method.GetCustomAttributes().Any(x => x.GetType().Name == "OpenWebApiAttribute");
                if (!canOpen)
                {
                    continue;
                }

                var driveMethod = GetSpecifiedMethod(deriveMethods, method);
                var parameters  = method.GetParameters().ToList();
                var methodName  = method.Name;

                var operation = new SwaggerOperationExtended {
                    OperationId = methodName
                };

                dynamic webApiMethodDesc = method.GetCustomAttributes().FirstOrDefault(x => x.GetType().Name == "WebApiDescriptionAttribute");
                if (webApiMethodDesc != null)
                {
                    operation.Tags.Add(webApiMethodDesc.Name);
                }

                var httpPath = GetHttpPath(operation, driveMethod, parameters, schemaResolver);

                LoadParameters(operation, parameters, schemaResolver);

                LoadReturnType(operation, driveMethod, schemaResolver);

                LoadMetaData(operation, method);

                var httpMethod = GetMethod(method);

                if (!_service.Paths.ContainsKey(httpPath))
                {
                    var path = new SwaggerOperationsExtended();
                    _service.Paths[httpPath] = path;
                }

                _service.Paths[httpPath][httpMethod] = operation;

                hasGenerated = true;
            }

            foreach (var schema in schemaResolver.Schemes)
            {
                _service.Definitions[schema.TypeName] = schema;
            }

            _service.GenerateOperationIds();

            if (!hasGenerated)
            {
                _service = null;
            }

            return(_service);
        }
 /// <summary>Initializes a new instance of the <see cref="RootTypeJsonSchemaGenerator" /> class.</summary>
 /// <param name="service">The service.</param>
 /// <param name="settings">The settings.</param>
 public RootTypeJsonSchemaGenerator(SwaggerServiceExtended service, JsonSchemaGeneratorSettings settings)
     : base(settings)
 {
     _service = service;
 }