Exemple #1
0
        public MetadataParameter[] GetTypeParameters(Type type, JsonMapperEntryMode?mode = null, bool patch = false, 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 => mode == null || (mode.Value & e.Mode) == mode.Value))
            {
                // add parameter that corresponds to the mapped property
                var propertyType = parameter.EntityProperty.PropertyType;

                var isJsonObject = parameter.EntityProperty.IsDefined(typeof(JsonFieldAttribute), false);
                var param        = new MetadataParameter
                {
                    Name          = prefix + parameter.JsonProperty,
                    Type          = isJsonObject ? "object" : ToJsonType(propertyType),
                    IsRequred     = !patch && IsRequired(parameter.EntityProperty),
                    Documentation = _xmlCommentReader.GetPropertyElement(parameter.EntityProperty).ElementContents("summary"),
                };
                parameters.Add(param);

                // add child object parameters
                if (param.Type == "object" && !isJsonObject)
                {
                    parameters.AddRange(GetTypeParameters(propertyType, mode, false, param.Name + "."));
                }
                else if (param.Type == "array")
                {
                    var elementType = propertyType.GetInterfaces().First(i => i.IsGenericType &&
                                                                         i.GetGenericTypeDefinition() == typeof(IEnumerable <>)).GetGenericArguments().First();
                    if (ToJsonType(elementType) == "object")
                    {
                        parameters.AddRange(GetTypeParameters(elementType, mode, false, param.Name + "[]."));
                    }
                }
            }
            return(parameters.ToArray());
        }