Exemple #1
0
        /// <summary>
        /// Converts an <see cref="ApiParameterDescription"/> to one or more <see cref="SimpleApiParameterDescriptor"/>s.
        /// In simple cases, such as a string or primitive type parameter, a simple mapping will occur. When a parameter
        /// represents a complex type, one parameter will be returned for each public property on the type.
        /// </summary>
        public static IEnumerable <SimpleApiParameterDescriptor> Flatten(ApiDescription apiDescription, ApiParameterDescription parameterDescription, IDocumentationProviderEx documentationProvider)
        {
            var descriptor = parameterDescription.ParameterDescriptor;

            if (descriptor == null)
            {
                return(new[]
                {
                    new SimpleApiParameterDescriptor(parameterDescription.Name, typeof(string), apiDescription.RelativePath, parameterDescription.Source, parameterDescription.Documentation)
                });
            }

            bool isMany;
            var  parameterType = Unwrap(descriptor.ParameterType, out isMany);

            if (IsSimpleType(parameterType))
            {
                return(new[] { new SimpleApiParameterDescriptor(descriptor, apiDescription.RelativePath, isMany, parameterDescription.Documentation) });
            }

            if (UsesCustomModelBinder(descriptor))
            {
                var callingConvention = SimpleApiParameterDescriptor.GetCallingConvention(apiDescription.RelativePath, descriptor.ParameterName);

                return(new[]
                {
                    new SimpleApiParameterDescriptor(descriptor.ParameterName, parameterType, callingConvention, descriptor.DefaultValue, descriptor.IsOptional, isMany, parameterDescription.Documentation)
                });
            }

            return(EnumerateProperties(apiDescription, parameterType, documentationProvider));
        }
Exemple #2
0
        public virtual XElement BuildInput(string name, SimpleApiParameterDescriptor p)
        {
            if (p.ParameterType.IsEnum)
            {
                return(BuildSelect(name, p));
            }

            var input = new XElement("input",
                                     new XAttribute("name", name),
                                     new XAttribute("type", GetHtmlInputType(p)));

            if (p.ParameterType == typeof(bool))
            {
                input.Add(new XAttribute("value", "true"));
                var check = Convert.ToBoolean(p.DefaultValue);

                if (check)
                {
                    input.Add(new XAttribute("checked", "checked"));
                }
            }
            else
            {
                input.Add(new XAttribute("value", p.DefaultValue ?? ""));
            }

            return(input);
        }
Exemple #3
0
        /// <summary>
        /// Determine which type attribute value to use for a <see cref="SimpleApiParameterDescriptor"/>.
        /// Currently this method only returns <c>"text"</c> though support for
        /// other types such as <c>"checkbox"</c>, <c>"radio"</c>, <c>"select"</c> or <c>"textarea"</c>
        /// may be added later.
        /// </summary>
        public virtual string GetHtmlInputType(SimpleApiParameterDescriptor apiParam)
        {
            var type = Nullable.GetUnderlyingType(apiParam.ParameterType) ?? apiParam.ParameterType;

            if (type == typeof(bool))
            {
                return("checkbox");
            }
            return("text");
        }
Exemple #4
0
        public virtual XElement BuildSelect(string name, SimpleApiParameterDescriptor p)
        {
            var input = new XElement("select", new XAttribute("name", name));

            if (p.IsMany)
            {
                input.Add(new XElement("multiple", "true"));
            }

            foreach (var value in Enum.GetValues(p.ParameterType).Cast <Enum>())
            {
                var option = new XElement("option", new XAttribute("value", value), new XText(value.ToString()));
                if (value.Equals(p.DefaultValue))
                {
                    option.Add(new XAttribute("selected", "selected"));
                }
                input.Add(option);
            }

            return(input);
        }