Esempio n. 1
0
        private MetadataParameter[] GetTypeParameters(Type type, JsonMapperEntryMode? exclude = null, string prefix = null)
        {
            // get JSON mapping manager
            var mapper = JsonMapperManager.GetMapper(type);
            if (mapper == null)
                return new MetadataParameter[] { };

            // create parameters from mapping entries
            var parameters = new List<MetadataParameter>();
            foreach (var parameter in mapper.Entries.Where(e => exclude == null || e.Mode != exclude.Value))
            {
                // add parameter that corresponds to the mapped property
                var isJsonObject = parameter.EntityProperty.IsDefined(typeof(JsonFieldAttribute), false);
                var param = new MetadataParameter
                    {
                        Name = (prefix == null ? null : prefix + ".") + parameter.JsonProperty,
                        Type = isJsonObject ? "object" : ToJsonType(parameter.EntityProperty.PropertyType),
                        IsRequred = IsRequired(parameter.EntityProperty),
                        Documentation = DataXmlCommentReader.GetPropertyElement(parameter.EntityProperty).ElementContents("summary"),
                    };
                parameters.Add(param);

                // add child object parameters
                if (param.Type == "object" && !isJsonObject)
                {
                    parameters.AddRange(GetTypeParameters(parameter.EntityProperty.PropertyType, exclude, param.Name));
                }
            }
            return parameters.ToArray();
        }
Esempio n. 2
0
        private MetadataParameter[] GetResponseParameters(ReflectedHttpActionDescriptor descriptor)
        {
            var methodElement = ApiXmlCommentReader.GetMethodElement(descriptor.MethodInfo);
            if (methodElement == null)
                return null;

            var parameters = new List<MetadataParameter>();

            // read cref type on the XML returns element
            var methodReturnsElement = methodElement.Element("returns");
            if (methodReturnsElement != null)
            {
                var resourceType = GetCrefType(methodReturnsElement);
                if (resourceType != null)
                {
                    parameters.AddRange(GetTypeParameters(resourceType, JsonMapperEntryMode.OneWayToSource));
                }
            }

            // read XML response element
            var responseElement = methodElement.Element("response");
            if (responseElement != null)
            {
                foreach (var parameterElement in responseElement.Elements("parameter")
                    .Where(p => p.Attribute("name") != null))
                {
                    var name = (string)parameterElement.Attribute("name");
                    var type = (string)parameterElement.Attribute("type");
                    var mode = (string)parameterElement.Attribute("mode");

                    // remove an existing parameter
                    if (mode == "remove")
                    {
                        parameters.RemoveAll(p => p.Name.StartsWith(name));
                        continue;
                    }

                    // add or update an existing parameter
                    var param = parameters.FirstOrDefault(p => p.Name == name);
                    if (param == null)
                    {
                        param = new MetadataParameter { Name = name, Type = type };
                        parameters.Add(param);
                    }
                    if (!string.IsNullOrEmpty(parameterElement.Contents()))
                    {
                        param.Documentation = parameterElement.Contents();
                    }

                    // if element includes cref - parse the specified type and add parameters from it
                    var cref = GetCrefType(parameterElement);
                    if (cref != null)
                    {
                        parameters.AddRange(GetTypeParameters(cref, JsonMapperEntryMode.OneWayToSource, param.Name + (type == "array" ? "[]" : null)));
                    }
                }
            }

            return parameters.ToArray();
        }