Beispiel #1
0
        public void ApplyColumn(Schema schema, Schema columnSchema, ColumnGenerator column)
        {
            if ((column.Attribute.Keys & DBColumnKeys.Password) == DBColumnKeys.Password ||
                (column.Attribute.Keys & DBColumnKeys.File) == DBColumnKeys.File)
            {
                return;
            }

            if (column.GetDataType() == typeof(string) && column.Size > 0)
            {
                columnSchema.MaxLength = column.Size;
            }

            if ((column.Attribute.Keys & DBColumnKeys.Access) == DBColumnKeys.Access ||
                (column.Attribute.Keys & DBColumnKeys.Date) == DBColumnKeys.Date ||
                (column.Attribute.Keys & DBColumnKeys.Stamp) == DBColumnKeys.Stamp ||
                (column.Attribute.Keys & DBColumnKeys.System) == DBColumnKeys.System ||
                column.PropertyInfo?.GetSetMethod() == null)
            {
                columnSchema.ReadOnly = true;
            }
            if (((column.Attribute.Keys & DBColumnKeys.Notnull) == DBColumnKeys.Notnull ||
                 (column.Attribute.Keys & DBColumnKeys.ItemType) == DBColumnKeys.ItemType) ||
                (column.Attribute.Keys & DBColumnKeys.Primary) == DBColumnKeys.Primary)
            {
                if (schema.Required == null)
                {
                    schema.Required = new List <string>();
                }
                schema.Required.Add(column.PropertyName);
            }
            if ((column.Attribute.Keys & DBColumnKeys.Primary) == DBColumnKeys.Primary)
            {
                schema.Extensions.Add("x-id", column.PropertyName);
            }
            if ((column.Attribute.Keys & DBColumnKeys.ItemType) == DBColumnKeys.ItemType)
            {
                schema.Extensions.Add("x-type", column.PropertyName);
            }
            schema.Properties.Add(column.PropertyName, columnSchema);

            var defaultValue = column.PropertyInfo.GetCustomAttribute <DefaultValueAttribute>();

            if (defaultValue != null && defaultValue.Value != null)
            {
                if (defaultValue.Value.GetType().IsEnum)
                {
                    columnSchema.Default = EnumItem.Format(defaultValue.Value);
                }
                else
                {
                    columnSchema.Default = defaultValue.Value.ToString();
                }
            }
            //if (TypeHelper.GetPassword(column.Property))
            //{
            //    schema.Extensions.Add("x-data", column.PropertyName);
            //}
        }
 private static void ApplyDefault(OpenApiSchema schema, object defaultValue)
 {
     if (defaultValue.GetType().IsEnum)
     {
         schema.Default = new OpenApiString(EnumItem.Format(defaultValue));
     }
     else
     {
         schema.Default = new OpenApiString(defaultValue.ToString());
     }
 }