Skip to content

Commit 2c02bb3

Browse files
committed
[MEVD] Add validation for mismatched collection key
Closes #11355
1 parent ea864bf commit 2c02bb3

30 files changed

+99
-29
lines changed

dotnet/src/InternalUtilities/connectors/Memory/MongoDB/MongoModelBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ protected override bool SupportsKeyAutoGeneration(Type keyPropertyType)
4646

4747
protected override void ValidateKeyProperty(KeyPropertyModel keyProperty)
4848
{
49+
base.ValidateKeyProperty(keyProperty);
50+
4951
var type = keyProperty.Type;
5052

5153
if (type != typeof(string) && type != typeof(int) && type != typeof(long) && type != typeof(Guid) && type != typeof(ObjectId))

dotnet/src/VectorData/AzureAISearch/AzureAISearchCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public AzureAISearchCollection(SearchIndexClient searchIndexClient, string name,
7272
static options => typeof(TRecord) == typeof(Dictionary<string, object?>)
7373
? throw new NotSupportedException(VectorDataStrings.NonDynamicCollectionWithDictionaryNotSupported(typeof(AzureAISearchDynamicCollection)))
7474
: new AzureAISearchModelBuilder()
75-
.Build(typeof(TRecord), options.Definition, options.EmbeddingGenerator, options.JsonSerializerOptions ?? JsonSerializerOptions.Default),
75+
.Build(typeof(TRecord), typeof(TKey), options.Definition, options.EmbeddingGenerator, options.JsonSerializerOptions ?? JsonSerializerOptions.Default),
7676
options)
7777
{
7878
}

dotnet/src/VectorData/AzureAISearch/AzureAISearchDynamicModelBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ internal class AzureAISearchDynamicModelBuilder() : CollectionModelBuilder(s_mod
1818

1919
protected override void ValidateKeyProperty(KeyPropertyModel keyProperty)
2020
{
21+
base.ValidateKeyProperty(keyProperty);
22+
2123
var type = keyProperty.Type;
2224

2325
if (type != typeof(string) && type != typeof(Guid))

dotnet/src/VectorData/AzureAISearch/AzureAISearchModelBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ internal class AzureAISearchModelBuilder() : CollectionJsonModelBuilder(s_modelB
2222

2323
protected override void ValidateKeyProperty(KeyPropertyModel keyProperty)
2424
{
25+
base.ValidateKeyProperty(keyProperty);
26+
2527
var type = keyProperty.Type;
2628

2729
if (type != typeof(string) && type != typeof(Guid))

dotnet/src/VectorData/CosmosMongoDB/CosmosMongoCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public CosmosMongoCollection(
9090
name,
9191
static options => typeof(TRecord) == typeof(Dictionary<string, object?>)
9292
? throw new NotSupportedException(VectorDataStrings.NonDynamicCollectionWithDictionaryNotSupported(typeof(CosmosMongoDynamicCollection)))
93-
: new MongoModelBuilder().Build(typeof(TRecord), options.Definition, options.EmbeddingGenerator),
93+
: new MongoModelBuilder().Build(typeof(TRecord), typeof(TKey), options.Definition, options.EmbeddingGenerator),
9494
options)
9595
{
9696
}

dotnet/src/VectorData/CosmosNoSql/CosmosNoSqlCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ internal CosmosNoSqlCollection(
124124
static options => typeof(TRecord) == typeof(Dictionary<string, object?>)
125125
? throw new NotSupportedException(VectorDataStrings.NonDynamicCollectionWithDictionaryNotSupported(typeof(CosmosNoSqlDynamicCollection)))
126126
: new CosmosNoSqlModelBuilder()
127-
.Build(typeof(TRecord), options.Definition, options.EmbeddingGenerator, options.JsonSerializerOptions ?? JsonSerializerOptions.Default),
127+
.Build(typeof(TRecord), typeof(TKey), options.Definition, options.EmbeddingGenerator, options.JsonSerializerOptions ?? JsonSerializerOptions.Default),
128128
options)
129129
{
130130
}

dotnet/src/VectorData/CosmosNoSql/CosmosNoSqlModelBuilder.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ internal class CosmosNoSqlModelBuilder() : CollectionJsonModelBuilder(s_modelBui
2222

2323
protected override void ValidateKeyProperty(KeyPropertyModel keyProperty)
2424
{
25+
// CosmosNoSqlKey is a composite key type (document ID + partition key) that doesn't correspond to any single key property type;
26+
// skip the base TKey-to-key-property validation when it's used.
27+
if (this.KeyType != typeof(CosmosNoSqlKey))
28+
{
29+
base.ValidateKeyProperty(keyProperty);
30+
}
31+
2532
// Note that the key property in Cosmos NoSQL refers to the document ID, not to the CosmosNoSqlKey structure which includes both
2633
// the document ID and the partition key (and which is the generic TKey type parameter of the collection).
2734
var type = keyProperty.Type;

dotnet/src/VectorData/InMemory/InMemoryCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public InMemoryCollection(string name, InMemoryCollectionOptions? options = defa
5656
name,
5757
static options => typeof(TRecord) == typeof(Dictionary<string, object?>)
5858
? throw new NotSupportedException(VectorDataStrings.NonDynamicCollectionWithDictionaryNotSupported(typeof(InMemoryDynamicCollection)))
59-
: new InMemoryModelBuilder().Build(typeof(TRecord), options.Definition, options.EmbeddingGenerator),
59+
: new InMemoryModelBuilder().Build(typeof(TRecord), typeof(TKey), options.Definition, options.EmbeddingGenerator),
6060
options)
6161
{
6262
}

dotnet/src/VectorData/InMemory/InMemoryModelBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ internal class InMemoryModelBuilder() : CollectionModelBuilder(ValidationOptions
1919

2020
protected override void ValidateKeyProperty(KeyPropertyModel keyProperty)
2121
{
22+
base.ValidateKeyProperty(keyProperty);
23+
2224
// All .NET types are supported by the InMemory provider, but we support auto-generation of keys only for GUIDs
2325
if (keyProperty.IsAutoGenerated && keyProperty.Type != typeof(Guid))
2426
{

dotnet/src/VectorData/MongoDB/MongoCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public MongoCollection(
9999
name,
100100
static options => typeof(TRecord) == typeof(Dictionary<string, object?>)
101101
? throw new NotSupportedException(VectorDataStrings.NonDynamicCollectionWithDictionaryNotSupported(typeof(MongoDynamicCollection)))
102-
: new MongoModelBuilder().Build(typeof(TRecord), options.Definition, options.EmbeddingGenerator),
102+
: new MongoModelBuilder().Build(typeof(TRecord), typeof(TKey), options.Definition, options.EmbeddingGenerator),
103103
options)
104104
{
105105
}

0 commit comments

Comments
 (0)