Ejemplo n.º 1
0
        public MetadataTypes GetMetadataTypes(IRequest req)
        {
            var metadata = new MetadataTypes
            {
                Config = config,
            };

            var skipTypes = new HashSet <Type>(config.SkipExistingTypes);

            config.IgnoreTypes.Each(x => skipTypes.Add(x));

            foreach (var operation in meta.Operations)
            {
                if (!meta.IsVisible(req, operation))
                {
                    continue;
                }

                metadata.Operations.Add(new MetadataOperationType
                {
                    Actions  = operation.Actions,
                    Request  = ToType(operation.RequestType),
                    Response = ToType(operation.ResponseType),
                });

                skipTypes.Add(operation.RequestType);
                if (operation.ResponseType != null)
                {
                    skipTypes.Add(operation.ResponseType);
                }
            }

            foreach (var type in meta.GetAllTypes())
            {
                if (skipTypes.Contains(type))
                {
                    continue;
                }

                metadata.Operations.Add(new MetadataOperationType
                {
                    Request = ToType(type),
                });

                skipTypes.Add(type);
            }

            var considered = new HashSet <Type>(skipTypes);
            var queue      = new Queue <Type>(skipTypes);

            while (queue.Count > 0)
            {
                var type = queue.Dequeue();
                foreach (var pi in type.GetSerializableProperties())
                {
                    if (pi.PropertyType.IsUserType())
                    {
                        if (considered.Contains(pi.PropertyType))
                        {
                            continue;
                        }

                        considered.Add(pi.PropertyType);
                        queue.Enqueue(pi.PropertyType);
                        metadata.Types.Add(ToType(pi.PropertyType));
                    }
                }

                if (type.BaseType != null &&
                    type.BaseType.IsUserType() &&
                    !considered.Contains(type.BaseType))
                {
                    considered.Add(type.BaseType);
                    queue.Enqueue(type.BaseType);
                    metadata.Types.Add(ToType(type.BaseType));
                }
            }
            return(metadata);
        }
Ejemplo n.º 2
0
        public MetadataTypes GetMetadataTypes(IRequest req)
        {
            var metadata = new MetadataTypes
            {
                Config = config,
            };

            var skipTypes        = config.IgnoreTypes ?? new HashSet <Type>();
            var opTypes          = new HashSet <Type>();
            var ignoreNamespaces = config.IgnoreTypesInNamespaces ?? new List <string>();

            foreach (var operation in meta.Operations)
            {
                if (!meta.IsVisible(req, operation))
                {
                    continue;
                }

                if (opTypes.Contains(operation.RequestType))
                {
                    continue;
                }

                if (skipTypes.Contains(operation.RequestType))
                {
                    continue;
                }

                if (ignoreNamespaces.Contains(operation.RequestType.Namespace))
                {
                    continue;
                }

                var opType = new MetadataOperationType
                {
                    Actions  = operation.Actions,
                    Request  = ToType(operation.RequestType),
                    Response = ToType(operation.ResponseType),
                };
                metadata.Operations.Add(opType);
                opTypes.Add(operation.RequestType);

                if (operation.ResponseType != null)
                {
                    if (skipTypes.Contains(operation.ResponseType))
                    {
                        //Config.IgnoreTypesInNamespaces in CSharpGenerator
                        opType.Response = null;
                    }
                    else
                    {
                        opTypes.Add(operation.ResponseType);
                    }
                }
            }

            foreach (var type in meta.GetAllTypes())
            {
                if (opTypes.Contains(type))
                {
                    continue;
                }
                if (skipTypes.Contains(type))
                {
                    continue;
                }
                if (ignoreNamespaces.Contains(type.Namespace))
                {
                    continue;
                }

                metadata.Operations.Add(new MetadataOperationType
                {
                    Request = ToType(type),
                });

                opTypes.Add(type);
            }

            var considered = new HashSet <Type>(opTypes);
            var queue      = new Queue <Type>(opTypes);

            Func <Type, bool> ignoreTypeFn = t =>
                                             t == null ||
                                             considered.Contains(t) ||
                                             skipTypes.Contains(t) ||
                                             ignoreNamespaces.Contains(t.Namespace);

            Action <Type> registerTypeFn = t => {
                considered.Add(t);
                queue.Enqueue(t);
                if (t.IsUserType())
                {
                    metadata.Types.Add(ToType(t));
                }
            };

            while (queue.Count > 0)
            {
                var type = queue.Dequeue();
                foreach (var pi in type.GetSerializableProperties()
                         .Where(pi => !ignoreTypeFn(pi.PropertyType)))
                {
                    registerTypeFn(pi.PropertyType);
                }

                if (!ignoreTypeFn(type.BaseType))
                {
                    registerTypeFn(type.BaseType);
                }

                if (!type.IsGenericType())
                {
                    continue;
                }

                var args = type.GetGenericArguments();
                foreach (var arg in args.Where(arg => !ignoreTypeFn(arg)))
                {
                    registerTypeFn(arg);
                }
            }

            return(metadata);
        }