Skip to content

Commit 43e94e7

Browse files
craigomaticsalmon131
authored andcommitted
Fix incorrect attribute causing the Qdrant connector to break (microsoft#1313)
### Motivation and Context This PR fixes a regression introduced in microsoft#1075 and includes unit tests to correctly validate that both string and integer property ids are supported. ### Description The changes are based on the code suggested by AwesomeYuer over in microsoft#794 - the JsonNumberHandling attribute behaved in an unexpected way and was removed.
1 parent a94637c commit 43e94e7

3 files changed

Lines changed: 123 additions & 1 deletion

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System;
4+
using System.Globalization;
5+
using System.Text.Json;
6+
using System.Text.Json.Serialization;
7+
8+
namespace Microsoft.SemanticKernel.Connectors.Memory.Qdrant.Http.ApiSchema;
9+
#pragma warning disable CA1812 // Avoid uninstantiated internal classes: Used for Json Deserialization
10+
internal sealed class NumberToStringConverter : JsonConverter<string>
11+
{
12+
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
13+
{
14+
if (typeToConvert != typeof(string))
15+
{
16+
throw new NotSupportedException($"{nameof(typeToConvert)}: {typeToConvert}");
17+
}
18+
19+
switch (reader.TokenType)
20+
{
21+
case JsonTokenType.String:
22+
return reader.GetString()!;
23+
case JsonTokenType.Number:
24+
if (reader.TryGetInt32(out var valueInt))
25+
{
26+
return valueInt.ToString("D", NumberFormatInfo.InvariantInfo);
27+
}
28+
29+
throw new NotSupportedException("Invalid integer specified.");
30+
default:
31+
throw new NotSupportedException($"{nameof(JsonTokenType)}.{reader.TokenType}");
32+
}
33+
}
34+
35+
public override void Write(Utf8JsonWriter writer, string @value, JsonSerializerOptions options)
36+
{
37+
writer.WriteStringValue(@value);
38+
}
39+
}
40+
#pragma warning restore CA1812 // Avoid uninstantiated internal classes

‎dotnet/src/Connectors/Connectors.Memory.Qdrant/Http/ApiSchema/SearchVectorsResponse.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal sealed class SearchVectorsResponse : QdrantResponse
1111
internal sealed class ScoredPoint
1212
{
1313
[JsonPropertyName("id")]
14-
[JsonNumberHandling(JsonNumberHandling.WriteAsString)]
14+
[JsonConverter(typeof(NumberToStringConverter))]
1515
public string Id { get; }
1616

1717
[JsonPropertyName("version")]

‎dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantMemoryStoreTests3.cs‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.Globalization;
56
using System.Linq;
7+
using System.Net;
8+
using System.Net.Http;
69
using System.Threading;
710
using System.Threading.Tasks;
811
using Microsoft.SemanticKernel.AI.Embeddings;
912
using Microsoft.SemanticKernel.Connectors.Memory.Qdrant;
1013
using Microsoft.SemanticKernel.Memory;
1114
using Moq;
15+
using Moq.Protected;
1216
using Xunit;
1317

1418
namespace SemanticKernel.Connectors.UnitTests.Memory.Qdrant;
@@ -224,4 +228,82 @@ public async Task ItReturnsEmptyListIfNearestMatchesNotFoundAsync()
224228
// Assert
225229
Assert.Empty(similarityResults);
226230
}
231+
232+
[Fact]
233+
public async Task ScoredVectorSupportsIntegerIds()
234+
{
235+
// Arrange
236+
var payloadId = "payloadId";
237+
var metadataId = "metadataId";
238+
var expectedId = 100;
239+
240+
var scoredPointJsonWithIntegerId =
241+
"{" +
242+
"\"result\": " +
243+
" [{" +
244+
"\"id\": " + expectedId + "," +
245+
"\"version\": 0," +
246+
"\"score\": null," +
247+
"\"payload\": {}," +
248+
"\"vector\": null " +
249+
"}]" +
250+
"}";
251+
252+
using (var httpResponseMessage = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(scoredPointJsonWithIntegerId) })
253+
{
254+
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
255+
mockHttpMessageHandler.Protected()
256+
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
257+
.ReturnsAsync(httpResponseMessage);
258+
259+
//Act
260+
using var httpClient = new HttpClient(mockHttpMessageHandler.Object);
261+
{
262+
var client = new QdrantVectorDbClient("http://localhost", 1536, null, httpClient);
263+
var result = await client.GetVectorByPayloadIdAsync(payloadId, metadataId);
264+
265+
//Assert
266+
Assert.Equal<string>(result!.PointId, expectedId.ToString(CultureInfo.InvariantCulture));
267+
}
268+
}
269+
}
270+
271+
[Fact]
272+
public async Task ScoredVectorSupportsStringIds()
273+
{
274+
// Arrange
275+
var payloadId = "payloadId";
276+
var metadataId = "metadataId";
277+
var expectedId = Guid.NewGuid().ToString();
278+
279+
var scoredPointJsonWithIntegerId =
280+
"{" +
281+
"\"result\": " +
282+
" [{" +
283+
"\"id\": \"" + expectedId + "\"," +
284+
"\"version\": 0," +
285+
"\"score\": null," +
286+
"\"payload\": {}," +
287+
"\"vector\": null " +
288+
"}]" +
289+
"}";
290+
291+
using (var httpResponseMessage = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(scoredPointJsonWithIntegerId) })
292+
{
293+
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
294+
mockHttpMessageHandler.Protected()
295+
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
296+
.ReturnsAsync(httpResponseMessage);
297+
298+
//Act
299+
using var httpClient = new HttpClient(mockHttpMessageHandler.Object);
300+
{
301+
var client = new QdrantVectorDbClient("http://localhost", 1536, null, httpClient);
302+
var result = await client.GetVectorByPayloadIdAsync(payloadId, metadataId);
303+
304+
//Assert
305+
Assert.Equal<string>(result!.PointId, expectedId);
306+
}
307+
}
308+
}
227309
}

0 commit comments

Comments
 (0)