string IDocumentationProvider.GetDocumentation(HttpParameterDescriptor parameterDescriptor)
        {
            var actionDoc = parameterDescriptor.GetCustomAttributes<ParameterDocAttribute>().FirstOrDefault();
            if (actionDoc != null)
            {
                return actionDoc.Documentation;
            }

            return string.Format("Documentation for '{0}'.", parameterDescriptor.ParameterName);
        }
        // Determine how a single parameter will get bound.
        // This is all sync. We don't need to actually read the body just to determine that we'll bind to the body.
        private HttpParameterBinding DetermineBinding(HttpParameterDescriptor parameter)
        {
            // Look at Parameter attributes?
            // [FromBody] - we use Formatter.
            bool hasFromBody          = parameter.GetCustomAttributes <FromBodyAttribute>().Any();
            ModelBinderAttribute attr = parameter.ModelBinderAttribute;

            if (hasFromBody)
            {
                if (attr != null)
                {
                    string message = Error.Format(SRResources.ParameterBindingConflictingAttributes, parameter.ParameterName);
                    return(new ErrorParameterBinding(parameter, message));
                }

                return(MakeBodyBinding(parameter)); // It's from the body. Uses a formatter.
            }

            // Presence of a model binder attribute overrides.
            if (attr != null)
            {
                return(GetBinding(parameter, attr));
            }

            // No attribute, key off type
            Type type = parameter.ParameterType;

            if (TypeHelper.IsSimpleUnderlyingType(type) || TypeHelper.HasStringConverter(type))
            {
                attr = new ModelBinderAttribute(); // use default settings
                return(GetBinding(parameter, attr));
            }

            // Handle special types
            if (type == typeof(CancellationToken))
            {
                return(new CancellationTokenParameterBinding(parameter));
            }
            if (type == typeof(HttpRequestMessage))
            {
                return(new HttpRequestParameterBinding(parameter));
            }
            if (typeof(HttpContent).IsAssignableFrom(type))
            {
                string message = Error.Format(SRResources.ParameterBindingIllegalType, type.Name, parameter.ParameterName);
                return(new ErrorParameterBinding(parameter, message));
            }

            // Fallback. Must be a complex type. Default is to look in body.
            return(MakeBodyBinding(parameter));
        }
Exemple #3
0
        public static void PrepareByOptionAttribute(ResourceApiOperationParameter parameter, HttpParameterDescriptor parameterDescriptor)
        {
            var attributes = parameterDescriptor.GetCustomAttributes <SwaggerOptionsAttribute>();

            if (attributes.Count > 0)
            {
                var att = attributes[0] as SwaggerOptionsAttribute;

                if (!String.IsNullOrWhiteSpace(att.Name))
                {
                    parameter.name = att.Name;
                }
            }
        }
		public static void PrepareByOptionAttribute(ResourceApiOperationParameter parameter, HttpParameterDescriptor parameterDescriptor)
		{
			var attributes = parameterDescriptor.GetCustomAttributes<SwaggerOptionsAttribute>();

			if (attributes.Count > 0)
			{
				var att = attributes[0] as SwaggerOptionsAttribute;

				if (!String.IsNullOrWhiteSpace(att.Name))
				{
					parameter.name = att.Name;
				}
			}
		}
Exemple #5
0
        private void ExecuteValidationAttributes(HttpParameterDescriptor parameter, object argument, ModelStateDictionary modelState)
        {
            var dateTimeValidateAttr = parameter.GetCustomAttributes <DateTimeRangeValidateAttribute>().FirstOrDefault();

            if (dateTimeValidateAttr != null)
            {
                var isValid = dateTimeValidateAttr.IsValid(argument);
                if (!isValid)
                {
                    modelState.AddModelError(parameter.ParameterName, dateTimeValidateAttr.ErrorMessage);
                }
            }

            var orderByValidateAttr = parameter.GetCustomAttributes <OrderByValidateAttribute>().FirstOrDefault();

            if (orderByValidateAttr != null)
            {
                var isValid = orderByValidateAttr.IsValid(argument);
                if (!isValid)
                {
                    modelState.AddModelError(parameter.ParameterName, orderByValidateAttr.ErrorMessage);
                }
            }
        }
Exemple #6
0
    private void EvaluateValidationAttributes(HttpParameterDescriptor parameter, object argument, ModelStateDictionary modelState)
    {
        var validationAttributes = parameter.GetCustomAttributes <ValidationAttribute>();

        foreach (var validationAttribute in validationAttributes)
        {
            if (validationAttribute != null)
            {
                var isValid = validationAttribute.IsValid(argument);
                if (!isValid)
                {
                    modelState.AddModelError(parameter.ParameterName, validationAttribute.FormatErrorMessage(parameter.ParameterName));
                }
            }
        }
    }
Exemple #7
0
        public string GetDocumentation(HttpParameterDescriptor parameterDescriptor)
        {
            var descriptionAttributes      = parameterDescriptor.GetCustomAttributes <DescriptionAttribute>();
            var descriptionAttributesCount = descriptionAttributes.Count;

            if (descriptionAttributesCount == 0)
            {
                return(string.Empty);
            }

            if (descriptionAttributesCount > 1)
            {
                throw new Exception();
            }

            var singleDescriptionAttribute = descriptionAttributes[0];

            return(singleDescriptionAttribute.Description);
        }
Exemple #8
0
        internal static bool TryGetProviderFromAttributes(HttpParameterDescriptor parameterDescriptor, out ModelBinderProvider provider)
        {
            Contract.Assert(parameterDescriptor != null, "parameterDescriptor cannot be null.");

            Type modelType = parameterDescriptor.ParameterType;

            ModelBinderAttribute attr = parameterDescriptor.GetCustomAttributes <ModelBinderAttribute>().FirstOrDefault();

            if (attr == null)
            {
                attr = TypeDescriptorHelper.Get(modelType).GetAttributes().OfType <ModelBinderAttribute>().FirstOrDefault();
            }

            if (attr == null)
            {
                provider = null;
                return(false);
            }

            return(TryGetProviderFromAttribute(modelType, attr, out provider));
        }
        // Determine how a single parameter will get bound. 
        // This is all sync. We don't need to actually read the body just to determine that we'll bind to the body.         
        private HttpParameterBinding DetermineBinding(HttpParameterDescriptor parameter)
        {
            // Look at Parameter attributes?
            // [FromBody] - we use Formatter.
            bool hasFromBody = parameter.GetCustomAttributes<FromBodyAttribute>().Any();
            ModelBinderAttribute attr = parameter.ModelBinderAttribute;
                        
            if (hasFromBody)
            {
                if (attr != null)
                {
                    string message = Error.Format(SRResources.ParameterBindingConflictingAttributes, parameter.ParameterName);
                    return new ErrorParameterBinding(parameter, message);
                }

                return MakeBodyBinding(parameter); // It's from the body. Uses a formatter. 
            }

            // Presence of a model binder attribute overrides.
            if (attr != null)
            {
                return GetBinding(parameter, attr);
            }
            
            // No attribute, key off type
            Type type = parameter.ParameterType;
            if (TypeHelper.IsSimpleUnderlyingType(type) || TypeHelper.HasStringConverter(type))
            {
                attr = new ModelBinderAttribute(); // use default settings
                return GetBinding(parameter, attr);
            }

            // Handle special types
            if (type == typeof(CancellationToken))
            {
                return new CancellationTokenParameterBinding(parameter);
            }
            if (type == typeof(HttpRequestMessage))
            {
                return new HttpRequestParameterBinding(parameter);
            }
            if (typeof(HttpContent).IsAssignableFrom(type))
            {
                string message = Error.Format(SRResources.ParameterBindingIllegalType, type.Name, parameter.ParameterName);
                return new ErrorParameterBinding(parameter, message);
            }            

            // Fallback. Must be a complex type. Default is to look in body.             
            return MakeBodyBinding(parameter);
        }
Exemple #10
0
 private static bool UsesCustomModelBinder(HttpParameterDescriptor descriptor)
 {
     return(descriptor.GetCustomAttributes <ModelBinderAttribute>()
            .Union(descriptor.ParameterType.GetCustomAttributes(typeof(ModelBinderAttribute), true))
            .Any(attr => attr.GetType() != typeof(FromUriAttribute) && attr.GetType() != typeof(FromBodyAttribute)));
 }
        public string GetDocumentation(HttpParameterDescriptor parameterDescriptor)
        {
            var parameterDescriptionAttributes = parameterDescriptor.GetCustomAttributes<ParameterDescriptionAttribute>();

            return parameterDescriptionAttributes.Any() ? parameterDescriptionAttributes.First().Description : string.Empty;
        }
 public override Collection <T> GetCustomAttributes <T>() => decorated.GetCustomAttributes <T>();