Skip to content

Commit 2551654

Browse files
committed
feat: Enhance TypeValidator and PropertyHelper to support enum compatibility and serialization
1 parent 915a632 commit 2551654

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed

src/Weaviate.Client.Tests/Unit/TestTypeValidator.cs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,124 @@ public class NestedModel
358358
{
359359
public string? Name { get; set; }
360360
}
361+
362+
// Models for compatibility tests
363+
private class ProductFloat
364+
{
365+
public float Price { get; set; }
366+
}
367+
368+
private class ProductDecimal
369+
{
370+
public decimal Price { get; set; }
371+
}
372+
373+
private class OrderLong
374+
{
375+
public long Quantity { get; set; }
376+
}
377+
378+
private enum TaskStatusEnum
379+
{
380+
Pending,
381+
InProgress,
382+
Completed,
383+
}
384+
385+
private class UserTask
386+
{
387+
public TaskStatusEnum Status { get; set; }
388+
}
389+
390+
[Theory]
391+
[InlineData(typeof(ProductFloat))]
392+
[InlineData(typeof(ProductDecimal))]
393+
public void ValidateType_NumberCompatibility_IsValid(Type type)
394+
{
395+
// Arrange
396+
var schema = new CollectionConfig
397+
{
398+
Name = "Product",
399+
Properties = [new Property { Name = "price", DataType = [DataType.Number] }],
400+
};
401+
var validator = TypeValidator.Default;
402+
403+
// Act
404+
var result = validator.ValidateType(type, schema);
405+
406+
// Assert
407+
Assert.True(result.IsValid);
408+
Assert.Empty(result.Errors);
409+
}
410+
411+
[Fact]
412+
public void ValidateType_IntCompatibility_IsValid()
413+
{
414+
// Arrange
415+
var schema = new CollectionConfig
416+
{
417+
Name = "Order",
418+
Properties = [new Property { Name = "quantity", DataType = [DataType.Int] }],
419+
};
420+
var validator = TypeValidator.Default;
421+
422+
// Act
423+
var result = validator.ValidateType<OrderLong>(schema);
424+
425+
// Assert
426+
Assert.True(result.IsValid);
427+
Assert.Empty(result.Errors);
428+
}
429+
430+
[Fact]
431+
public void ValidateType_EnumCompatibility_IsValid()
432+
{
433+
// Arrange
434+
var schema = new CollectionConfig
435+
{
436+
Name = "UserTask",
437+
Properties = new[]
438+
{
439+
new Property
440+
{
441+
Name = "status",
442+
DataType = new List<string> { DataType.Text },
443+
},
444+
},
445+
};
446+
var validator = TypeValidator.Default;
447+
448+
// Act
449+
var result = validator.ValidateType<UserTask>(schema);
450+
451+
// Assert
452+
Assert.True(result.IsValid);
453+
Assert.Empty(result.Errors);
454+
}
455+
456+
[Fact]
457+
public void ValidateType_EnumCompatibility_WithInteger_IsValid()
458+
{
459+
// Arrange
460+
var schema = new CollectionConfig
461+
{
462+
Name = "UserTask",
463+
Properties = new[]
464+
{
465+
new Property
466+
{
467+
Name = "status",
468+
DataType = new List<string> { DataType.Int },
469+
},
470+
},
471+
};
472+
var validator = TypeValidator.Default;
473+
474+
// Act
475+
var result = validator.ValidateType<UserTask>(schema);
476+
477+
// Assert
478+
Assert.True(result.IsValid);
479+
Assert.Empty(result.Errors);
480+
}
361481
}

src/Weaviate.Client/Models/Property.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ internal static string DataTypeForCollectionType(Type? elementType)
6666
return DataType.TextArray; // Assuming you have array-specific methods
6767
}
6868

69+
// Enums serialize as text by default (EnumMember or name)
70+
if (elementType.IsEnum)
71+
{
72+
return DataType.TextArray;
73+
}
74+
6975
var tc = Type.GetTypeCode(elementType);
7076

7177
// Handle primitive collection element types
@@ -120,6 +126,12 @@ internal static string DataTypeForType(Type t)
120126
return DataType.Text;
121127
}
122128

129+
// Enums serialize as text (EnumMember or name)
130+
if (actualType.IsEnum)
131+
{
132+
return DataType.Text;
133+
}
134+
123135
// Handle arrays and collections
124136
if (IsArrayOrCollection(actualType, out Type? elementType))
125137
{

src/Weaviate.Client/Validation/TypeValidator.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,15 @@ List<ValidationWarning> warnings
166166
var schemaDataType = schemaProp.DataType[0]; // Primary type
167167

168168
// Compare expected vs actual data types
169-
if (!AreTypesCompatible(expectedDataType, schemaDataType))
169+
var isEnum = (csProperty.PropertyType.IsEnum);
170+
var isEnumArray =
171+
csProperty.PropertyType.IsArray
172+
&& csProperty.PropertyType.GetElementType()?.IsEnum == true;
173+
var enumIntCompatible =
174+
(isEnum && schemaDataType == DataType.Int)
175+
|| (isEnumArray && schemaDataType == DataType.IntArray);
176+
177+
if (!AreTypesCompatible(expectedDataType, schemaDataType) && !enumIntCompatible)
170178
{
171179
// Check if it's an array mismatch specifically
172180
var isArrayMismatch =

0 commit comments

Comments
 (0)