コード例 #1
0
ファイル: ApiManager.cs プロジェクト: ranyjax/WebApi
 private ApiManager(Type type)
 {
     var container = new TypeDefinitionContainer(true);
     _apiDescriptor = new ApiDescriptor(type);
     _apiDescriptor.Container = container;
     _apiDescriptor.ScanTypeDefinitions();
 }
コード例 #2
0
        private ApiManager(Type type)
        {
            var container = new TypeDefinitionContainer(true);

            _apiDescriptor           = new ApiDescriptor(type);
            _apiDescriptor.Container = container;
            _apiDescriptor.ScanTypeDefinitions();
        }
コード例 #3
0
        public TypeDefinition(Type type, JsonSchemaGenerator generator, TypeDefinitionContainer container)
        {
            _type     = type;
            _schema   = generator.Generate(type, true);
            _canNull  = HasFlag(_schema.Type, JsonSchemaType.Null);
            _isArray  = HasFlag(_schema.Type, JsonSchemaType.Array);
            _isObject = HasFlag(_schema.Type, JsonSchemaType.Object);

            if (_isArray && _isObject)
            {
                throw new ApplicationException(string.Format("Type {0} is both array and object.", type.FullName));
            }

            if (_isArray)
            {
                _subType = GetCollectionItemType(_type);
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                _subType = type.GetGenericArguments()[0];
                var subDef = container.GetOrCreate(_subType);
                _name      = subDef.Name;
                _demoValue = subDef.DemoValue;
            }
            else if (type == typeof(DateTime))
            {
                _name      = "dateTime";
                _demoValue = JsonConvert.SerializeObject(DateTime.Now).Trim('\"');
            }
            else if (type == typeof(byte[]))
            {
                _name      = "base64binary";
                _demoValue = JsonConvert.SerializeObject(Encoding.UTF8.GetBytes("Cus.WebApi")).Trim('\"');
            }

            _isLeaf = !(_isArray || _isObject);

            _id = Guid.NewGuid().ToString("N");

            if (_name == null)
            {
                if (IsArray)
                {
                    _name      = "array";
                    _demoValue = "[]";
                }
                else if (IsObject)
                {
                    _name      = "object";
                    _demoValue = "{}";
                    if (_schema.Id != null)
                    {
                        _isCustomObject = true;
                    }
                }
                else if (HasFlag(_schema.Type, JsonSchemaType.String))
                {
                    _name      = "string";
                    _demoValue = "string1";
                }
                else if (HasFlag(_schema.Type, JsonSchemaType.Boolean))
                {
                    _name      = "boolean";
                    _demoValue = true;
                }
                else if (HasFlag(_schema.Type, JsonSchemaType.Float))
                {
                    _name      = "float";
                    _demoValue = 1.1f;
                }
                else if (HasFlag(_schema.Type, JsonSchemaType.Integer))
                {
                    _name      = "integer";
                    _demoValue = 1;
                }
                else if (_schema.Type == JsonSchemaType.Any)
                {
                    _name = "any";
                }
                else if (_schema.Type == JsonSchemaType.Null)
                {
                    _name = "null";
                }
                else
                {
                    _name = "unknown";
                }
            }
        }
コード例 #4
0
ファイル: TypeDefinition.cs プロジェクト: ranyjax/WebApi
        public TypeDefinition(Type type,JsonSchemaGenerator generator,TypeDefinitionContainer container)
        {
            _type = type;
            _schema = generator.Generate(type, true);
            _canNull = HasFlag(_schema.Type, JsonSchemaType.Null);
            _isArray = HasFlag(_schema.Type, JsonSchemaType.Array);
            _isObject = HasFlag(_schema.Type, JsonSchemaType.Object);

            if (_isArray && _isObject)
                throw new ApplicationException(string.Format("Type {0} is both array and object.", type.FullName));

            if (_isArray)
            {
                _subType = GetCollectionItemType(_type);
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
            {
                _subType = type.GetGenericArguments()[0];
                var subDef = container.GetOrCreate(_subType);
                _name = subDef.Name;
                _demoValue = subDef.DemoValue;
            }
            else if (type == typeof(DateTime))
            {
                _name = "dateTime";
                _demoValue = JsonConvert.SerializeObject(DateTime.Now).Trim('\"');
            }
            else if (type == typeof(byte[]))
            {
                _name = "base64binary";
                _demoValue = JsonConvert.SerializeObject(Encoding.UTF8.GetBytes("Cus.WebApi")).Trim('\"');
            }

            _isLeaf = !(_isArray || _isObject);

            _id = Guid.NewGuid().ToString("N");

            if (_name == null)
            {
                if (IsArray)
                {
                    _name = "array";
                    _demoValue = "[]";
                }
                else if (IsObject)
                {
                    _name = "object";
                    _demoValue = "{}";
                    if (_schema.Id != null)
                        _isCustomObject = true;
                }
                else if (HasFlag(_schema.Type, JsonSchemaType.String))
                {
                    _name = "string";
                    _demoValue = "string1";
                }
                else if (HasFlag(_schema.Type, JsonSchemaType.Boolean))
                {
                    _name = "boolean";
                    _demoValue = true;
                }
                else if (HasFlag(_schema.Type, JsonSchemaType.Float))
                {
                    _name = "float";
                    _demoValue = 1.1f;
                }
                else if (HasFlag(_schema.Type, JsonSchemaType.Integer))
                {
                    _name = "integer";
                    _demoValue = 1;
                }
                else if (_schema.Type == JsonSchemaType.Any) _name = "any";
                else if (_schema.Type == JsonSchemaType.Null) _name = "null";
                else _name = "unknown";
            }
        }