Skip to content

Commit

Permalink
fix null field data json convert error
Browse files Browse the repository at this point in the history
  • Loading branch information
weianweigan committed Jun 4, 2023
1 parent 1b049e5 commit 2f611a7
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/IO.Milvus/MilvusFieldConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ private static Field DeserializeField(ref Utf8JsonReader reader)
reader.Read();reader.Read();
reader.Read();reader.Read();
var scalarTypeName = reader.GetString();
reader.Read();reader.Read();
reader.Read(); reader.Read(); reader.Read();
if (reader.TokenType != JsonTokenType.StartArray) {
reader.Read(); reader.Read();
break;
};
switch (scalarTypeName)
{
case "BoolData":
Expand Down Expand Up @@ -176,7 +180,6 @@ private static Field DeserializeField(ref Utf8JsonReader reader)
{
reader.Read(); reader.Read();
dim = DeserializeVector(ref reader, floatVector,binaryVector);

reader.Read();
}
break;
Expand Down Expand Up @@ -253,30 +256,27 @@ private static long DeserializeVector(ref Utf8JsonReader reader, List<float> flo
reader.Read();

var dataType = reader.GetString();

reader.Read(); reader.Read(); reader.Read();
if (reader.TokenType != JsonTokenType.StartArray)
{
break;
};
switch (dataType)
{
case "FloatVector":
{
reader.Read(); reader.Read();

DeserializePropertyList<float>(ref reader, floatVector);

reader.Read(); reader.Read();
}
break;
case "BinaryVector":
{
reader.Read(); reader.Read();

DeserializePropertyList<byte>(ref reader, binaryVector);

reader.Read(); reader.Read();
}
break;
default:
throw new JsonException($"Not support: {dataType}");
}
reader.Read(); reader.Read();
}
break;

Expand Down
63 changes: 63 additions & 0 deletions src/IO.MilvusTests/MilvusFieldConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,69 @@ public void SampleTest()
""";

var data = JsonSerializer.Deserialize<TestJsonFieldData>(responseData);
data.Should().NotBeNull();
data.FieldData.Count.Should().Be(3);
data.FieldData[1].FieldName.Should().Be("embedding");
data.FieldData[1].Should().BeOfType<FloatVectorField>();
(data.FieldData[1] as FloatVectorField).Data.Count.Should().Be(1);
(data.FieldData[1] as FloatVectorField).Data[0].Count.Should().Be(3);
}

[Fact]
public void SampleTest2()
{
var responseData =
"""
{
"status": {},
"fields_data": [
{
"type": 21,
"field_name": "metadata",
"Field": {
"Scalars": {
"Data": {
"StringData": {}
}
}
},
"field_id": 102
},
{
"type": 101,
"field_name": "embedding",
"Field": {
"Vectors": {
"dim": 3,
"Data": {
"FloatVector": {}
}
}
},
"field_id": 101
},
{
"type": 21,
"field_name": "Id",
"Field": {
"Scalars": {
"Data": {
"StringData": {}
}
}
},
"field_id": 100
}
]
}

""";

var data = JsonSerializer.Deserialize<TestJsonFieldData>(responseData);
data.Should().NotBeNull();
data.FieldData.Count.Should().Be(3);
data.FieldData[1].FieldName.Should().Be("embedding");
data.FieldData[1].Should().BeOfType<FloatVectorField>();
(data.FieldData[1] as FloatVectorField).Data.Should().BeNullOrEmpty();
}
}

0 comments on commit 2f611a7

Please sign in to comment.