Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/BLite.SourceGenerators/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ private static void GenerateWriteProperty(StringBuilder sb, PropertyInfo prop, s
var writeMethod = GetPrimitiveWriteMethod(prop, allowKey: false);
if (writeMethod != null)
{
if (prop.IsNullable || prop.TypeName == "string" || prop.TypeName == "String")
if (prop.IsNullable || prop.TypeName == "string" || prop.TypeName == "String" || prop.TypeName == "byte[]")
{
sb.AppendLine($" if (entity.{prop.Name} != null)");
sb.AppendLine($" {{");
Expand Down Expand Up @@ -718,6 +718,27 @@ private static void GenerateReadPropertyToLocal(StringBuilder sb, PropertyInfo p
var readMethod = GetPrimitiveReadMethod(prop);
if (readMethod != null)
{
if (readMethod == "ReadBinary")
{
// ReadBinary returns ReadOnlySpan<byte> — must call .ToArray() to materialise
if (prop.IsNullable)
{
sb.AppendLine($" if ({bsonTypeVar} == global::BLite.Bson.BsonType.Null)");
sb.AppendLine($" {{");
sb.AppendLine($" {localVar} = null;");
sb.AppendLine($" }}");
sb.AppendLine($" else");
sb.AppendLine($" {{");
sb.AppendLine($" {localVar} = reader.ReadBinary(out _).ToArray();");
sb.AppendLine($" }}");
}
else
{
sb.AppendLine($" {localVar} = reader.ReadBinary(out _).ToArray();");
}
}
else
{
var cast = (prop.TypeName == "float" || prop.TypeName == "Single") ? "(float)" : "";

// Handle nullable types - check for null in BSON stream
Expand All @@ -738,6 +759,7 @@ private static void GenerateReadPropertyToLocal(StringBuilder sb, PropertyInfo p
var readArgs = IsCoercedReadMethod(readMethod) ? $"({bsonTypeVar})" : "()";
sb.AppendLine($" {localVar} = {cast}reader.{readMethod}{readArgs};");
}
}
}
else if (prop.ConverterTypeName != null)
{
Expand Down Expand Up @@ -931,6 +953,7 @@ private static string GetBaseMapperClass(PropertyInfo? keyProp, EntityInfo entit
if (cleanType.EndsWith("TimeOnly")) return "WriteTimeOnly";
if (cleanType.EndsWith("Guid")) return "WriteGuid";
if (cleanType.EndsWith("ObjectId")) return "WriteObjectId";
if (cleanType == "byte[]") return "WriteBinary";

return null;
}
Expand Down Expand Up @@ -964,6 +987,7 @@ private static string GetBaseMapperClass(PropertyInfo? keyProp, EntityInfo entit
if (cleanType.EndsWith("TimeOnly")) return "ReadTimeOnly";
if (cleanType.EndsWith("Guid")) return "ReadGuid";
if (cleanType.EndsWith("ObjectId")) return "ReadObjectId";
if (cleanType == "byte[]") return "ReadBinary";

return null;
}
Expand Down Expand Up @@ -1025,6 +1049,7 @@ private static string QualifyType(string typeName)
case "object":
case "dynamic":
case "void":
case "byte[]":
return baseType + (isNullable ? "?" : "");
case "Guid": return "global::System.Guid" + (isNullable ? "?" : "");
case "DateTime": return "global::System.DateTime" + (isNullable ? "?" : "");
Expand Down
3 changes: 3 additions & 0 deletions src/BLite.SourceGenerators/Helpers/SyntaxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ public static bool IsCollectionType(ITypeSymbol type, out ITypeSymbol? itemType)
// Handle arrays
if (type is IArrayTypeSymbol arrayType)
{
// Exclude byte[] — it maps to BSON Binary, not a sequence of integers
if (arrayType.ElementType.SpecialType == SpecialType.System_Byte)
return false;
itemType = arrayType.ElementType;
return true;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/BLite.Shared/MockEntities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ public class DddLineItem
public int Quantity { get; set; }
}

// --- Binary Property Tests ---

public class BinaryEntity
{
public ObjectId Id { get; set; }
public string Label { get; set; } = "";
public byte[] Data { get; set; } = Array.Empty<byte>();
public byte[]? OptionalData { get; set; }
}

public record OrderId(string Value)
{
public OrderId() : this(string.Empty) { }
Expand Down
6 changes: 6 additions & 0 deletions tests/BLite.Shared/TestDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public partial class TestDbContext : DocumentDbContext
// Device – HasConversion on a non-ID property (ulong → long)
public DocumentCollection<string, Device> Devices { get; set; } = null!;

// Binary Property Tests
public DocumentCollection<ObjectId, BinaryEntity> BinaryEntities { get; set; } = null!;

public TestDbContext(string databasePath) : base(databasePath)
{
InitializeCollections();
Expand Down Expand Up @@ -183,6 +186,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

// Benchmark entities
modelBuilder.Entity<CustomerOrder>().ToCollection("customer_orders").HasKey(e => e.Id);

// Binary Property Tests
modelBuilder.Entity<BinaryEntity>().ToCollection("binary_entities");
}

public void ForceCheckpoint()
Expand Down
Loading
Loading