コード例 #1
0
        public void Apply(Operation operation, DataTypeRegistry dataTypeRegistry, ApiDescription apiDescription)
        {
            var methodNode = _navigator.SelectSingleNode(GetXPathFor(apiDescription.ActionDescriptor));

            operation.Summary = GetChildValueOrDefault(methodNode, SummaryExpression);
            operation.Notes   = GetChildValueOrDefault(methodNode, RemarksExpression);

            foreach (var paramDesc in apiDescription.ParameterDescriptions)
            {
                var parameter = operation.Parameters.SingleOrDefault(p => p.Name == paramDesc.Name);
                if (parameter == null)
                {
                    continue;
                }

                parameter.Description = GetChildValueOrDefault(methodNode, String.Format(ParameterExpression, paramDesc.Name));
            }

            if (methodNode == null)
            {
                return;
            }

            foreach (var responseMessage in GetResponseMessages(methodNode))
            {
                operation.ResponseMessages.Add(responseMessage);
            }
        }
コード例 #2
0
        public ApiDeclaration GetDeclaration(string resourceName)
        {
            var apiDescriptionGroup = _apiDescriptions
                .GroupBy(apiDesc => _options.ResourceNameResolver(apiDesc))
                .SingleOrDefault(apiDescGrp => apiDescGrp.Key == resourceName);

            if (apiDescriptionGroup == null)
                throw new ApiDeclarationNotFoundException(resourceName);

            var dataTypeRegistry = new DataTypeRegistry(
                _options.CustomTypeMappings,
                _options.PolymorphicTypes,
                _options.ModelFilters);

            var operationGenerator = new OperationGenerator(
                dataTypeRegistry,
                _options.OperationFilters);

            // Group further by relative path - each group corresponds to an Api
            var apis = apiDescriptionGroup
                .GroupBy(apiDesc => apiDesc.RelativePathSansQueryString())
                .Select(apiDescGrp => CreateApi(apiDescGrp, operationGenerator))
                .OrderBy(api => api.Path)
                .ToList();

            return new ApiDeclaration
            {
                SwaggerVersion = SwaggerVersion,
                ApiVersion = _apiVersion,
                BasePath = _basePath,
                ResourcePath = "/" + resourceName,
                Apis = apis,
                Models = dataTypeRegistry.GetModels()
            };
        }
コード例 #3
0
        public ApiDeclaration GetDeclaration(string basePath, string version, string resourceName)
        {
            var apiDescriptionGroup = GetApplicableApiDescriptions(version)
                .Single(apiDescGrp => apiDescGrp.Key == resourceName);

            var dataTypeRegistry = new DataTypeRegistry(_customTypeMappings, _polymorphicTypes, _modelFilters);
            var operationGenerator = new OperationGenerator(dataTypeRegistry, _operationFilters);

            // Group further by relative path - each group corresponds to an Api
            var apis = apiDescriptionGroup
                .GroupBy(apiDesc => apiDesc.RelativePathSansQueryString())
                .Select(apiDescGrp => CreateApi(apiDescGrp, operationGenerator))
                .OrderBy(api => api.Path)
                .ToList();

            return new ApiDeclaration
            {
                SwaggerVersion = SwaggerVersion,
                ApiVersion = version,
                BasePath = basePath,
                ResourcePath = "/" + resourceName,
                Apis = apis,
                Models = dataTypeRegistry.GetModels()
            };
        }
コード例 #4
0
        public void Apply(Operation operation, DataTypeRegistry dataTypeRegistry, ApiDescription apiDescription)
        {
            var methodNode = _navigator.SelectSingleNode(GetXPathFor(apiDescription.ActionDescriptor));

            operation.Summary = GetChildValueOrDefault(methodNode, SummaryExpression);
            operation.Notes = GetChildValueOrDefault(methodNode, RemarksExpression);

            foreach (var paramDesc in apiDescription.ParameterDescriptions)
            {
                if (paramDesc.ParameterDescriptor == null) continue; // not in action signature (e.g. route parameter)

                var parameter = operation.Parameters.SingleOrDefault(p => p.Name == paramDesc.Name);
                if (parameter == null) continue;

                parameter.Description = GetChildValueOrDefault(
                    methodNode,
                    String.Format(ParameterExpression, paramDesc.ParameterDescriptor.ParameterName));
            }

            if (methodNode == null) return;

            foreach (var responseMessage in GetResponseMessages(methodNode))
            {
                operation.ResponseMessages.Add(responseMessage);
            }
        }
コード例 #5
0
        public ApiDeclaration GetDeclaration(string basePath, string version, string resourceName)
        {
            var apiDescriptionGroup = GetApplicableApiDescriptions(version)
                                      .Single(apiDescGrp => apiDescGrp.Key == resourceName);

            var dataTypeRegistry   = new DataTypeRegistry(_customTypeMappings, _polymorphicTypes, _modelFilters);
            var operationGenerator = new OperationGenerator(dataTypeRegistry, _operationFilters);

            // Group further by relative path - each group corresponds to an Api
            var apis = apiDescriptionGroup
                       .GroupBy(apiDesc => apiDesc.RelativePathSansQueryString())
                       .Select(apiDescGrp => CreateApi(apiDescGrp, operationGenerator))
                       .OrderBy(api => api.Path)
                       .ToList();

            return(new ApiDeclaration
            {
                SwaggerVersion = SwaggerVersion,
                ApiVersion = version,
                BasePath = basePath,
                ResourcePath = "/" + resourceName,
                Apis = apis,
                Models = dataTypeRegistry.GetModels()
            });
        }
コード例 #6
0
 public void Apply(Operation operation, DataTypeRegistry dataTypeRegistry, ApiDescription apiDescription)
 {
     if (_errorStatusCodes == null) return;
     _errorStatusCodes.ToList().ForEach(e => operation.ResponseMessages.Add(new ResponseMessage
     {
         Code = (int)e,
         Message = Regex.Replace(Enum.GetName(typeof(HttpStatusCode), e),
                                 @"([a-z])([A-Z])", @"$1 $2", RegexOptions.None),
     }));
 }
コード例 #7
0
        private void ExecuteAttributePropertyHandlers(Operation operation, DataTypeRegistry dataTypeRegistry, ApiDescription apiDescription, SwaggerAttribute attribute)
        {
            AttributePropertyHandler.AsParallel().ForAll(p =>
            {
                var propertyInfo = typeof(SwaggerAttribute).GetProperty(p.Key);
                if (propertyInfo == null) return;

                var handler = (ISwaggerAttributeHandler)(Activator.CreateInstance(p.Value));
                handler.Handle(operation, dataTypeRegistry, apiDescription, attribute);
            });
        }
コード例 #8
0
 public void Apply(Operation operation, DataTypeRegistry dataTypeRegistry, ApiDescription apiDescription)
 {
     if (apiDescription.ActionDescriptor.GetFilters().OfType<AuthorizeAttribute>().Any())
     {
         operation.ResponseMessages.Add(new ResponseMessage
         {
             Code = (int)HttpStatusCode.Unauthorized,
             Message = "Authentication required"
         });
     }
 }
コード例 #9
0
        public void Apply(DataType model, DataTypeRegistry dataTypeRegistry, Type type)
        {
            var camelCasedProperties = new Dictionary<string, DataType>();
            foreach (var entry in model.Properties)
            {
                var key = entry.Key;
                camelCasedProperties[Char.ToLower(key[0]) + key.Substring(1)] = entry.Value;
            }

            model.Properties = camelCasedProperties;
        }
コード例 #10
0
        public void Apply(DataType model, DataTypeRegistry dataTypeRegistry, Type type)
        {
            var typeNode = _navigator.SelectSingleNode(String.Format(TypeExpression, type.FullName));
            model.Description = GetChildValueOrDefault(typeNode, SummaryExpression);

            foreach (var property in model.Properties)
            {
                var propertyNode = _navigator.SelectSingleNode(String.Format(PropertyExpression, type.FullName, property.Key));
                property.Value.Description = GetChildValueOrDefault(propertyNode, SummaryExpression);
            }
        }
コード例 #11
0
        public void Apply(Operation operation, DataTypeRegistry dataTypeRegistry, ApiDescription apiDescription)
        {
            var actionName = apiDescription.ActionDescriptor.ActionName;
            var controllerType = apiDescription.ActionDescriptor.ControllerDescriptor.ControllerType;
            var paramTypes = GetTypeOfActionParameters(apiDescription);
            var attribute =
                controllerType.GetMethod(actionName, paramTypes.ToArray()).GetCustomAttributes(typeof(SwaggerAttribute), false).FirstOrDefault();

            if (attribute == null) return;

            ExecuteAttributePropertyHandlers(operation, dataTypeRegistry, apiDescription, (SwaggerAttribute)attribute);
        }
コード例 #12
0
        public void Apply(DataType model, DataTypeRegistry dataTypeRegistry, Type type)
        {
            var typeNode = _navigator.SelectSingleNode(String.Format(TypeExpression, type.FullName));

            model.Description = GetChildValueOrDefault(typeNode, SummaryExpression);

            foreach (var property in model.Properties)
            {
                var propertyNode = _navigator.SelectSingleNode(String.Format(PropertyExpression, type.FullName, property.Key));
                property.Value.Description = GetChildValueOrDefault(propertyNode, SummaryExpression);
            }
        }
コード例 #13
0
        /// <summary>
        /// Implement the Apply method in IOperationFilter to define response codes specific to the API
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="dataTypeRegistry"></param>
        /// <param name="apiDescription"></param>
        public void Apply(Operation operation, DataTypeRegistry dataTypeRegistry, ApiDescription apiDescription)
        {
            operation.ResponseMessages.Add(new ResponseMessage
            {
                Code = (int)HttpStatusCode.OK,
                Message = "It's all good!"
            });

            operation.ResponseMessages.Add(new ResponseMessage
            {
                Code = (int)HttpStatusCode.InternalServerError,
                Message = "Somethings up!"
            });
        }
コード例 #14
0
        public void Apply(Operation operation, DataTypeRegistry dataTypeRegistry, ApiDescription apiDescription)
        {
            var scopeIds = apiDescription.ActionDescriptor.GetFilterPipeline()
                .Select(filterInfo => filterInfo.Instance)
                .OfType<ScopeAuthorizeAttribute>()
                .SelectMany(attr => attr.Scopes)
                .Distinct();

            if (scopeIds.Any())
            {
                operation.Authorizations = new Dictionary<string, IList<Scope>>();
                operation.Authorizations["oauth2"] = scopeIds
                    .Select(id => new Scope { ScopeId = id })
                    .ToList();
            }
        }
コード例 #15
0
        public void Handle(Operation operation, DataTypeRegistry dataTypeRegistry, ApiDescription apiDescription, SwaggerAttribute attribute)
        {
            var responseType = attribute.ResponseClassType;
            if (responseType == null || responseType == typeof(void))
            {
                operation.Type = "void";
                return;
            }

            var dataType = dataTypeRegistry.GetOrRegister(responseType);

            if (dataType.Type == "object")
            {
                operation.Type = dataType.Id;
            }
            else
            {
                operation.Type = dataType.Type;
                operation.Format = dataType.Format;
                operation.Items = dataType.Items;
                operation.Enum = dataType.Enum;
            }
        }
コード例 #16
0
        public ApiDeclaration GetDeclaration(string resourceName)
        {
            var apiDescriptionGroup = _apiDescriptions
                                      .GroupBy(apiDesc => _options.ResourceNameResolver(apiDesc))
                                      .SingleOrDefault(apiDescGrp => apiDescGrp.Key == resourceName);

            if (apiDescriptionGroup == null)
            {
                throw new ApiDeclarationNotFoundException(resourceName);
            }

            var dataTypeRegistry = new DataTypeRegistry(
                _options.CustomTypeMappings,
                _options.PolymorphicTypes,
                _options.ModelFilters);

            var operationGenerator = new OperationGenerator(
                dataTypeRegistry,
                _options.OperationFilters);

            // Group further by relative path - each group corresponds to an Api
            var apis = apiDescriptionGroup
                       .GroupBy(apiDesc => apiDesc.RelativePathSansQueryString())
                       .Select(apiDescGrp => CreateApi(apiDescGrp, operationGenerator))
                       .OrderBy(api => api.Path)
                       .ToList();

            return(new ApiDeclaration
            {
                SwaggerVersion = SwaggerVersion,
                ApiVersion = _apiVersion,
                BasePath = _basePath,
                ResourcePath = "/" + resourceName,
                Apis = apis,
                Models = dataTypeRegistry.GetModels()
            });
        }
コード例 #17
0
 public OperationGenerator(DataTypeRegistry dataTypeRegistry, IEnumerable<IOperationFilter> operationFilters)
 {
     _dataTypeRegistry = dataTypeRegistry;
     _operationFilters = operationFilters;
 }
コード例 #18
0
 public OperationGenerator(DataTypeRegistry dataTypeRegistry, IEnumerable <IOperationFilter> operationFilters)
 {
     _dataTypeRegistry = dataTypeRegistry;
     _operationFilters = operationFilters;
 }