Skip to content

Conversation

@mpartipilo
Copy link
Collaborator

Improve type safety and validation for typed clients, add integration and unit tests, and optimize the test workflow. Introduce optional client-side schema validation and enhance the TypeValidator to support anonymous types. Refactor code for better performance and readability.

Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca

@mpartipilo mpartipilo force-pushed the feat/generics-handling branch from ecce02b to 65d7322 Compare November 26, 2025 22:01
@mpartipilo mpartipilo requested a review from g-despot November 27, 2025 13:02
@mpartipilo mpartipilo self-assigned this Nov 27, 2025
Base automatically changed from feat/generics-handling to main November 30, 2025 12:01
private bool AreTypesCompatible(string csDataType, string schemaDataType)
{
// Exact match
if (string.Equals(csDataType, schemaDataType, StringComparison.OrdinalIgnoreCase))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this correctly handle compatibility between float/double in C# and numbers in Weaviate? Or for example C# enums to Weaviate text? Can also be added in future releases

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you give me more concrete use cases I can create tests for these scenarios

Copy link
Contributor

@g-despot g-despot Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried these additional tests in TestTypeValidator.cs:

    // Models for compatibility tests
    private class ProductFloat { public float Price { get; set; } }
    private class ProductDecimal { public decimal Price { get; set; } }
    private class OrderLong { public long Quantity { get; set; } }
    private enum TaskStatusEnum { Pending, InProgress, Completed }
    private class UserTask { public TaskStatusEnum Status { get; set; } }

    [Theory]
    [InlineData(typeof(ProductFloat))]
    [InlineData(typeof(ProductDecimal))]
    public void ValidateType_NumberCompatibility_IsValid(Type type)
    {
        // Arrange
        var schema = new CollectionConfig
        {
            Name = "Product",
            Properties =
            [
                new Property { Name = "price", DataType = [DataType.Number] }
            ]
        };
        var validator = TypeValidator.Default;

        // Act
        var result = validator.ValidateType(type, schema);

        // Assert
        Assert.True(result.IsValid);
        Assert.Empty(result.Errors);
    }

    [Fact]
    public void ValidateType_IntCompatibility_IsValid()
    {
        // Arrange
        var schema = new CollectionConfig
        {
            Name = "Order",
            Properties =
            [
                new Property { Name = "quantity", DataType = [DataType.Int] }
            ]
        };
        var validator = TypeValidator.Default;

        // Act
        var result = validator.ValidateType<OrderLong>(schema);

        // Assert
        Assert.True(result.IsValid);
        Assert.Empty(result.Errors);
    }

    [Fact]
    public void ValidateType_EnumCompatibility_IsValid()
    {
        // Arrange
        var schema = new CollectionConfig
        {
            Name = "UserTask",
            Properties = new List<Property>
            {
                new Property { Name = "status", DataType = new List<string> { DataType.Text } }
            }
        };
        var validator = TypeValidator.Default;

        // Act
        var result = validator.ValidateType<UserTask>(schema);

        // Assert
        Assert.True(result.IsValid);
        Assert.Empty(result.Errors);
    }

The first two worked 👏🏻 but the third one doesn't ValidateType_EnumCompatibility_IsValid. Maybe it makes sense to add enums mapping to text in DataTypeForType

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's a very good idea.

Copy link
Collaborator Author

@mpartipilo mpartipilo Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the PR with the logic to handle Enums in both numerical and string forms. Check it out!

Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed SAST high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca

mpartipilo and others added 15 commits December 1, 2025 15:11
- Implemented unit tests for CollectionClientExtensions, TypedDataClient, TypedGenerateClient, TypedQueryClient, ensuring proper functionality and type safety.
- Added extension methods for CollectionClient to support typed operations, including validation against collection schemas.
- Introduced validation extensions to validate C# types against collection schemas, throwing exceptions on mismatches.
- Enhanced test coverage for various scenarios, including insertion, fetching, and type validation for articles and products.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants