/// <summary>
        /// Filter and add Security JWT definition to all accion with has "AuthorizaAttribute"
        /// </summary>
        /// <param name="operation">Operation</param>
        /// <param name="schemaRegistry">Schema</param>
        /// <param name="apiDescription">Description for the api</param>
        public void Apply(Swashbuckle.Swagger.Operation operation, Swashbuckle.Swagger.SchemaRegistry schemaRegistry, System.Web.Http.Description.ApiDescription apiDescription)
        {
            if (
                apiDescription.ActionDescriptor.GetFilters().OfType <AuthorizeAttribute>().Any() ||
                apiDescription.ActionDescriptor.ControllerDescriptor.GetFilters().OfType <AuthorizeAttribute>().Any()
                )
            {
                if (operation.security == null)
                {
                    operation.security = new List <IDictionary <string, IEnumerable <string> > >();
                }

                var oAuthRequirements = new Dictionary <string, IEnumerable <string> >();
                oAuthRequirements.Add(_securityDefinitionNameSchema, new List <string>());

                operation.security.Add(oAuthRequirements);
            }
        }
Example #2
0
 /// <summary>
 /// Remove Swagger Defaults
 /// </summary>
 /// <param name="operation">Operation</param>
 /// <param name="schemaRegistry">Schema</param>
 /// <param name="apiDescription">Description for the api</param>
 public void Apply(Swashbuckle.Swagger.Operation operation, Swashbuckle.Swagger.SchemaRegistry schemaRegistry, System.Web.Http.Description.ApiDescription apiDescription)
 {
     //not Work
     operation.responses.Clear();
 }
        public void Apply(Swashbuckle.Swagger.Operation operation, Swashbuckle.Swagger.SchemaRegistry schemaRegistry, System.Web.Http.Description.ApiDescription apiDescription)
        {
            #region Add Documentation Nodes
            string TModel = null;
            bool   isBlueprintEndpoint = false;


            Type BlueprintController = apiDescription.ActionDescriptor.ControllerDescriptor.ControllerType.BaseType;
            if (BlueprintController.IsGenericType)
            {
                Type modelType = BlueprintController.GetGenericArguments()[0];
                TModel = modelType.Name;

                //Remove all Auto DB generated Properties
                CleanModel(schemaRegistry, modelType);

                isBlueprintEndpoint = true;
            }

            //Has Queryable Endpoint Mark??
            if (apiDescription.ActionDescriptor.GetCustomAttributes <Swashbuckle.Swagger.Annotations.QueryableEndpoint>().Any())
            {
                var attr = apiDescription.ActionDescriptor.GetCustomAttributes <Swashbuckle.Swagger.Annotations.QueryableEndpoint>().First();

                if (attr.QueryableType != null)
                {
                    var EnumerableType = attr.QueryableType;

                    //Remove all Auto DB generated Properties
                    if (!typeof(System.Collections.IEnumerable).IsAssignableFrom(EnumerableType))
                    {
                        //Convert to List (Queryable ALWAYS Return a List)
                        EnumerableType = typeof(List <>).MakeGenericType(attr.QueryableType);
                    }

                    CleanModel(schemaRegistry, EnumerableType);
                    operation.responses.Clear();

                    var statusCode = "200";
                    operation.responses[statusCode] = new Swashbuckle.Swagger.Response
                    {
                        schema = (EnumerableType != null) ? schemaRegistry.GetOrRegister(EnumerableType) : null
                    };
                }

                isBlueprintEndpoint = true;
            }



            if (!isBlueprintEndpoint)
            {
                return;
            }

            if (apiDescription.HttpMethod == System.Net.Http.HttpMethod.Get)
            {
                operation.summary     = String.Format(Gale.REST.Resources.SwasbuckleExtension_Blueprint_GET, TModel);
                operation.description = String.Format(Gale.REST.Resources.SwasbuckleExtension_Blueprint_GET_ImplementationNotes, Gale.REST.Resources.GALE_DOCS_SITE);

                #region OData Parameter's
                operation.parameters = new List <Swashbuckle.Swagger.Parameter>();
                operation.parameters.Add(new Swashbuckle.Swagger.Parameter()
                {
                    name        = "$select",
                    description = "Fields selector (comma separated)",
                    @in         = "query",
                    required    = false,
                    type        = "string"
                });

                operation.parameters.Add(new Swashbuckle.Swagger.Parameter()
                {
                    name        = "$filter",
                    description = "collection of filter's (comma separated): {field} {operator} {value}",
                    @in         = "query",
                    required    = false,
                    type        = "string"
                });

                operation.parameters.Add(new Swashbuckle.Swagger.Parameter()
                {
                    name        = "$orderBy",
                    description = "Order by clause: {field} (asc|desc)",
                    @in         = "query",
                    required    = false,
                    type        = "string"
                });

                operation.parameters.Add(new Swashbuckle.Swagger.Parameter()
                {
                    name        = "$limit",
                    description = "Limit the number of records returned",
                    @in         = "query",
                    required    = false,
                    type        = "number"
                });

                operation.parameters.Add(new Swashbuckle.Swagger.Parameter()
                {
                    name        = "$offset",
                    description = "Skip records before returning anything",
                    @in         = "query",
                    required    = false,
                    type        = "number"
                });
                #endregion
            }
            else if (apiDescription.HttpMethod == System.Net.Http.HttpMethod.Post)
            {
                operation.summary     = String.Format(Gale.REST.Resources.SwasbuckleExtension_Blueprint_POST, TModel);;
                operation.description = String.Format(Gale.REST.Resources.SwasbuckleExtension_Blueprint_POST_ImplementationNotes, TModel);
            }
            else if (apiDescription.HttpMethod == System.Net.Http.HttpMethod.Put)
            {
                operation.summary = String.Format(Gale.REST.Resources.SwasbuckleExtension_Blueprint_PUT, TModel);;
            }
            else if (apiDescription.HttpMethod == System.Net.Http.HttpMethod.Delete)
            {
                operation.summary = String.Format(Gale.REST.Resources.SwasbuckleExtension_Blueprint_DELETE, TModel);;
            }
            #endregion
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="operation">Operation</param>
        /// <param name="context">OperationFilterContext</param>
        public void Apply(Operation operation, OperationFilterContext context)
        {
            var pars = context.ApiDescription.ParameterDescriptions;

            foreach (var par in pars)
            {
                var swaggerParam = operation.parameters.SingleOrDefault(p => p.name == par.Name);

                var attributes = ((ControllerParameterDescriptor)par.ParameterDescriptor).ParameterInfo.CustomAttributes;

                if (attributes != null && attributes.Count() > 0 && swaggerParam != null)
                {
                    // Required - [Required]
                    var requiredAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RequiredAttribute));
                    if (requiredAttr != null)
                    {
                        swaggerParam.required = true;
                    }

                    // Regex Pattern [RegularExpression]
                    var regexAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RegularExpressionAttribute));
                    if (regexAttr != null)
                    {
                        string regex = (string)regexAttr.ConstructorArguments[0].Value;
                        if (swaggerParam is NonBodyParameter)
                        {
                        }
                    }

                    // String Length [StringLength]
                    int?minLenght = null, maxLength = null;
                    var stringLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(StringLengthAttribute));
                    if (stringLengthAttr != null)
                    {
                        if (stringLengthAttr.NamedArguments.Count == 1)
                        {
                            minLenght = (int)stringLengthAttr.NamedArguments.Single(p => p.MemberName == "MinimumLength").TypedValue.Value;
                        }
                        maxLength = (int)stringLengthAttr.ConstructorArguments[0].Value;
                    }

                    var minLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(MinLengthAttribute));
                    if (minLengthAttr != null)
                    {
                        minLenght = (int)minLengthAttr.ConstructorArguments[0].Value;
                    }

                    var maxLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(MaxLengthAttribute));
                    if (maxLengthAttr != null)
                    {
                        maxLength = (int)maxLengthAttr.ConstructorArguments[0].Value;
                    }

// Range [Range]
                    var rangeAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RangeAttribute));
                    if (rangeAttr != null)
                    {
                        int rangeMin = (int)rangeAttr.ConstructorArguments[0].Value;
                        int rangeMax = (int)rangeAttr.ConstructorArguments[1].Value;

                        if (swaggerParam is NonBodyParameter)
                        {
                        }
                    }
                }
            }
        }