Skip to content

Commit 8a4204b

Browse files
committed
fix #2992 throw a better error message when Join<TParent, TChild>() is called multiple times with different children
1 parent fb279fa commit 8a4204b

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/Nest/Mapping/Types/Core/Join/Relations.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using Newtonsoft.Json;
@@ -15,9 +16,19 @@ public Relations(Dictionary<RelationName, Children> container)
1516
: base(container.Select(kv => kv).ToDictionary(kv => kv.Key, kv => kv.Value))
1617
{ }
1718

18-
public void Add(RelationName type, Children children) => BackingDictionary.Add(type, children);
19-
public void Add(RelationName type, RelationName child, params RelationName[] moreChildren) =>
19+
public void Add(RelationName type, Children children)
20+
{
21+
if (BackingDictionary.ContainsKey(type))
22+
throw new ArgumentException($"{type} is already mapped as parent, you have to map all it's children as a single entry");
23+
BackingDictionary.Add(type, children);
24+
}
25+
26+
public void Add(RelationName type, RelationName child, params RelationName[] moreChildren)
27+
{
28+
if (BackingDictionary.ContainsKey(type))
29+
throw new ArgumentException($"{type} is already mapped as parent, you have to map all it's children as a single entry");
2030
BackingDictionary.Add(type, new Children(child, moreChildren));
31+
}
2132
}
2233

2334
public class RelationsDescriptor : IsADictionaryDescriptorBase<RelationsDescriptor, IRelations, RelationName, Children>
@@ -27,8 +38,20 @@ internal RelationsDescriptor(IRelations relations) : base(relations) { }
2738

2839
public RelationsDescriptor Join(RelationName parent, RelationName child, params RelationName[] moreChildren) =>
2940
Assign(parent, new Children(child, moreChildren));
30-
public RelationsDescriptor Join<TParent>(RelationName child, params RelationName[] moreChildren) =>
31-
Assign(typeof(TParent), new Children(child, moreChildren));
32-
public RelationsDescriptor Join<TParent, TChild>() => Assign(typeof(TParent), typeof(TChild));
41+
42+
public RelationsDescriptor Join<TParent>(RelationName child, params RelationName[] moreChildren)
43+
{
44+
if (this.PromisedValue.ContainsKey(typeof(TParent))) throw new ArgumentException(Message(typeof(TParent)));
45+
return Assign(typeof(TParent), new Children(child, moreChildren));
46+
}
47+
48+
public RelationsDescriptor Join<TParent, TChild>()
49+
{
50+
if (this.PromisedValue.ContainsKey(typeof(TParent))) throw new ArgumentException(Message(typeof(TParent)));
51+
return Assign(typeof(TParent), typeof(TChild));
52+
}
53+
54+
private static string Message(Type t) =>
55+
$"{t.Name} is already mapped. Use Join<TParent>(typeof(ChildA), typeof(ChildB), ..) to add multiple children in one go";
3356
}
3457
}

src/Tests/ClientConcepts/HighLevel/Mapping/ParentChildJoins.doc.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public void SimpleParentChildMapping()
7272
.Name(p=>p.MyJoinField)
7373
.Relations(r => r
7474
.Join<MyDocument, MyChild>()
75+
.Join<MyDocument>(typeof(MyChild), typeof(MyChild))
7576
)
7677
)
7778
)

0 commit comments

Comments
 (0)