Пример #1
0
        public PropertyType Register(Type returnType, int depth = 0)
        {
            returnType = Nullable.GetUnderlyingType(returnType) ?? returnType;

            if (_models.ContainsKey(returnType))
            {
                return(new PropertyType
                {
                    Type = _models[returnType].id,
                    Description = returnType.FriendlyName(),
                    Items = new Items
                    {
                        Ref = _models[returnType].id
                    }
                });
            }

            if (PrimitiveMappings.ContainsKey(returnType))
            {
                var mapping = PrimitiveMappings[returnType];
                return(new PropertyType
                {
                    Description = returnType.FriendlyName(),
                    Type = mapping.Type,
                    Format = mapping.Format
                });
            }

            if (returnType.IsEnum)
            {
                return(new PropertyType
                {
                    Description = returnType.FriendlyName(),
                    Type = "string",
                    Enum = Enum.GetNames(returnType)
                });
            }

            if (returnType.Implements <IEnumerable>())
            {
                var collectionType = returnType.GetElementType();
                if (returnType.IsGenericType)
                {
                    collectionType = returnType.GetGenericArguments()[0];
                }

                var colMapping = Register(collectionType, depth++);
                var isComplex  = _models.ContainsKey(collectionType);

                return(new PropertyType
                {
                    Description = returnType.FriendlyName(),
                    Type = "array",
                    Items = new Items
                    {
                        Ref = (isComplex)? colMapping.Type : "",
                        Type = (!isComplex)? colMapping.Type : ""
                    }
                });
            }

            if (returnType.IsPrimitive)
            {
                return(new PropertyType
                {
                    Description = returnType.FriendlyName(),
                    Type = returnType.Name
                });
            }

            var modelSpec = new ModelSpec {
                id = ModelIdFromType(returnType)
            };

            _models.Add(returnType, modelSpec);

            foreach (var prop in returnType.GetProperties())
            {
                var mapping = Register(prop.PropertyType, depth++);
                modelSpec.properties.Add(prop.Name, mapping);
            }

            return(new PropertyType
            {
                Description = returnType.FriendlyName(),
                Type = modelSpec.id,
            });
        }
Пример #2
0
 public static bool IsTypeSwaggerPrimitive(Type type)
 {
     type = Nullable.GetUnderlyingType(type) ?? type;
     return(type.IsPrimitive || PrimitiveMappings.ContainsKey(type));
 }