Skip to content

Commit 2bae134

Browse files
committed
Add test for maxRecursion on PutMappingDescriptor
1 parent 1e89b91 commit 2bae134

File tree

1 file changed

+123
-63
lines changed

1 file changed

+123
-63
lines changed

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

Lines changed: 123 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using static Tests.Framework.RoundTripper;
88

99
namespace Tests.ClientConcepts.HighLevel.Mapping
10-
{
10+
{
1111
/** # Auto mapping properties
1212
*
1313
* When creating a mapping (either when creating an index or via the put mapping API),
@@ -16,14 +16,14 @@ namespace Tests.ClientConcepts.HighLevel.Mapping
1616
* you're using attributes to map your properties, then calling AutoMap() is required
1717
* in order for your attributes to be applied. We'll look at examples of both.
1818
*
19-
**/
19+
**/
2020
public class AutoMap
21-
{
21+
{
2222
/**
2323
* For these examples, we'll define two POCOS. A Company, which has a name
2424
* and a collection of Employees. And Employee, which has various properties of
2525
* different types, and itself has a collection of Employees.
26-
*/
26+
*/
2727
public class Company
2828
{
2929
public string Name { get; set; }
@@ -38,16 +38,16 @@ public class Employee
3838
public DateTime Birthday { get; set; }
3939
public bool IsManager { get; set; }
4040
public List<Employee> Employees { get; set; }
41-
public TimeSpan Hours { get; set;}
41+
public TimeSpan Hours { get; set; }
4242
}
4343

4444
[U]
4545
public void MappingManually()
46-
{
46+
{
4747
/** ## Manual mapping
4848
* To create a mapping for our Company type, we can use the fluent API
4949
* and map each property explicitly
50-
*/
50+
*/
5151
var descriptor = new CreateIndexDescriptor("myindex")
5252
.Mappings(ms => ms
5353
.Map<Company>(m => m
@@ -72,13 +72,13 @@ public void MappingManually()
7272
)
7373
)
7474
)
75-
);
76-
75+
);
76+
7777
/**
7878
* Which is all fine and dandy, and useful for some use cases. However in most cases
7979
* this is becomes too cumbersome of an approach, and you simply just want to map *all*
8080
* the properties of your POCO in a single go.
81-
*/
81+
*/
8282
var expected = new
8383
{
8484
mappings = new
@@ -115,22 +115,22 @@ public void MappingManually()
115115
}
116116
};
117117

118-
Expect(expected).WhenSerializing((ICreateIndexRequest) descriptor);
118+
Expect(expected).WhenSerializing((ICreateIndexRequest)descriptor);
119119
}
120120

121121
[U]
122122
public void UsingAutoMap()
123-
{
123+
{
124124
/** ## Simple Automapping
125125
* This is exactly where `AutoMap()` becomes useful. Instead of manually mapping each property,
126126
* explicitly, we can instead call `.AutoMap()` for each of our mappings and let NEST do all the work
127-
*/
127+
*/
128128
var descriptor = new CreateIndexDescriptor("myindex")
129129
.Mappings(ms => ms
130130
.Map<Company>(m => m.AutoMap())
131131
.Map<Employee>(m => m.AutoMap())
132-
);
133-
132+
);
133+
134134
/**
135135
* Observe that NEST has inferred the Elasticsearch types based on the CLR type of our POCO properties.
136136
* In this example,
@@ -140,7 +140,7 @@ public void UsingAutoMap()
140140
* - Salary as an integer
141141
* - Employees as an object
142142
* and the remaining string properties as strings.
143-
*/
143+
*/
144144
var expected = new
145145
{
146146
mappings = new
@@ -168,7 +168,7 @@ public void UsingAutoMap()
168168
},
169169
hours = new
170170
{
171-
type = "long"
171+
type = "long"
172172
},
173173
isManager = new
174174
{
@@ -229,30 +229,31 @@ public void UsingAutoMap()
229229
}
230230
};
231231

232-
Expect(expected).WhenSerializing((ICreateIndexRequest) descriptor);
233-
}
234-
232+
Expect(expected).WhenSerializing((ICreateIndexRequest)descriptor);
233+
}
234+
235235
/** ## Automapping with overrides
236236
* In most cases, you'll want to map more than just the vanilla datatypes and also provide
237237
* various options on your properties (analyzer, doc_values, etc...). In that case, it's
238238
* possible to use AutoMap() in conjuction with explicitly mapped properties.
239-
*/
240-
[U] public void OverridingAutoMappedProperties()
241-
{
239+
*/
240+
[U]
241+
public void OverridingAutoMappedProperties()
242+
{
242243
/**
243244
* Here we are using AutoMap() to automatically map our company type, but then we're
244245
* overriding our employee property and making it a `nested` type, since by default,
245246
* AutoMap() will infer objects as `object`.
246-
*/
247+
*/
247248
var descriptor = new CreateIndexDescriptor("myindex")
248249
.Mappings(ms => ms
249250
.Map<Company>(m => m
250251
.AutoMap()
251252
.Properties(ps => ps
252253
.Nested<Employee>(n => n
253254
.Name(c => c.Employees)
254-
.Properties(eps => eps
255-
// snip
255+
.Properties(eps => eps
256+
// snip
256257
)
257258
)
258259
)
@@ -274,21 +275,21 @@ [U] public void OverridingAutoMappedProperties()
274275
employees = new
275276
{
276277
type = "nested",
277-
properties = new {}
278+
properties = new { }
278279
}
279280
}
280281
}
281282
}
282283
};
283284

284-
Expect(expected).WhenSerializing((ICreateIndexRequest) descriptor);
285-
}
286-
285+
Expect(expected).WhenSerializing((ICreateIndexRequest)descriptor);
286+
}
287+
287288
/** ## Automap with attributes
288289
* It is also possible to define your mappings using attributes on your POCOS. When you
289290
* use attributes, you MUST use AutoMap() in order for the attributes to be applied.
290291
* Here we define the same two types but this time using attributes.
291-
*/
292+
*/
292293
[ElasticsearchType(Name = "company")]
293294
public class CompanyWithAttributes
294295
{
@@ -467,13 +468,13 @@ public void UsingAutoMapWithAttributes()
467468
};
468469

469470
Expect(expected).WhenSerializing(descriptor as ICreateIndexRequest);
470-
}
471-
471+
}
472+
472473
/**
473474
* Just as we were able to override the inferred properties in our earlier example, explicit (manual)
474475
* mappings also take precedence over attributes. Therefore we can also override any mappings applied
475476
* via any attributes defined on the POCO
476-
*/
477+
*/
477478
[U]
478479
public void OverridingAutoMappedAttributes()
479480
{
@@ -492,7 +493,7 @@ public void OverridingAutoMappedAttributes()
492493
.TtlField(ttl => ttl
493494
.Enable()
494495
.Default("10m")
495-
)
496+
)
496497
.Properties(ps => ps
497498
.String(s => s
498499
.Name(e => e.FirstName)
@@ -633,7 +634,7 @@ public void OverridingAutoMappedAttributes()
633634
}
634635
};
635636

636-
Expect(expected).WhenSerializing((ICreateIndexRequest) descriptor);
637+
Expect(expected).WhenSerializing((ICreateIndexRequest)descriptor);
637638
}
638639

639640
[ElasticsearchType(Name = "company")]
@@ -699,7 +700,7 @@ public void IgnoringProperties()
699700
)
700701
);
701702

702-
settings.Expect(expected).WhenSerializing((ICreateIndexRequest) descriptor);
703+
settings.Expect(expected).WhenSerializing((ICreateIndexRequest)descriptor);
703704
}
704705

705706
/**
@@ -722,14 +723,14 @@ public class A
722723

723724
[U]
724725
public void ControllingRecursionDepth()
725-
{
726-
/** By default, AutoMap() only goes as far as depth 1 */
726+
{
727+
/** By default, AutoMap() only goes as far as depth 1 */
727728
var descriptor = new CreateIndexDescriptor("myindex")
728729
.Mappings(ms => ms
729730
.Map<A>(m => m.AutoMap())
730-
);
731-
732-
/** Thus we do not map properties on the second occurrence of our Child property */
731+
);
732+
733+
/** Thus we do not map properties on the second occurrence of our Child property */
733734
var expected = new
734735
{
735736
mappings = new
@@ -748,15 +749,15 @@ public void ControllingRecursionDepth()
748749
}
749750
};
750751

751-
Expect(expected).WhenSerializing((ICreateIndexRequest) descriptor);
752-
753-
/** Now lets specify a maxRecursion of 3 */
752+
Expect(expected).WhenSerializing((ICreateIndexRequest)descriptor);
753+
754+
/** Now lets specify a maxRecursion of 3 */
754755
var withMaxRecursionDescriptor = new CreateIndexDescriptor("myindex")
755756
.Mappings(ms => ms
756757
.Map<A>(m => m.AutoMap(3))
757-
);
758-
759-
/** AutoMap() has now mapped three levels of our Child property */
758+
);
759+
760+
/** AutoMap() has now mapped three levels of our Child property */
760761
var expectedWithMaxRecursion = new
761762
{
762763
mappings = new
@@ -796,9 +797,68 @@ public void ControllingRecursionDepth()
796797
}
797798
};
798799

799-
Expect(expectedWithMaxRecursion).WhenSerializing((ICreateIndexRequest) withMaxRecursionDescriptor);
800+
Expect(expectedWithMaxRecursion).WhenSerializing((ICreateIndexRequest)withMaxRecursionDescriptor);
800801
}
801802

803+
[U]
804+
//hide
805+
public void PutMappingAlsoAdheresToMaxRecursion()
806+
{
807+
var descriptor = new PutMappingDescriptor<A>().AutoMap();
808+
809+
var expected = new
810+
{
811+
properties = new
812+
{
813+
child = new
814+
{
815+
properties = new { },
816+
type = "object"
817+
}
818+
}
819+
};
820+
821+
Expect(expected).WhenSerializing((IPutMappingRequest)descriptor);
822+
823+
var withMaxRecursionDescriptor = new PutMappingDescriptor<A>().AutoMap(3);
824+
825+
var expectedWithMaxRecursion = new
826+
{
827+
properties = new
828+
{
829+
child = new
830+
{
831+
type = "object",
832+
properties = new
833+
{
834+
child = new
835+
{
836+
type = "object",
837+
properties = new
838+
{
839+
child = new
840+
{
841+
type = "object",
842+
properties = new
843+
{
844+
child = new
845+
{
846+
type = "object",
847+
properties = new { }
848+
}
849+
}
850+
}
851+
}
852+
}
853+
}
854+
}
855+
}
856+
};
857+
858+
Expect(expectedWithMaxRecursion).WhenSerializing((IPutMappingRequest)withMaxRecursionDescriptor);
859+
}
860+
//endhide
861+
802862
/** # Applying conventions through the Visitor pattern
803863
* It is also possible to apply a transformation on all or specific properties.
804864
*
@@ -807,16 +867,16 @@ public void ControllingRecursionDepth()
807867
*
808868
* For instance, lets create a custom visitor that disables doc values for numeric and boolean types.
809869
* (Not really a good idea in practice, but let's do it anyway for the sake of a clear example.)
810-
*/
870+
*/
811871
public class DisableDocValuesPropertyVisitor : NoopPropertyVisitor
812-
{
813-
/** Override the Visit method on INumberProperty and set DocValues = false */
872+
{
873+
/** Override the Visit method on INumberProperty and set DocValues = false */
814874
public override void Visit(INumberProperty type, PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute)
815875
{
816876
type.DocValues = false;
817-
}
818-
819-
/** Similarily, override the Visit method on IBooleanProperty and set DocValues = false */
877+
}
878+
879+
/** Similarily, override the Visit method on IBooleanProperty and set DocValues = false */
820880
public override void Visit(IBooleanProperty type, PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute)
821881
{
822882
type.DocValues = false;
@@ -825,17 +885,17 @@ public override void Visit(IBooleanProperty type, PropertyInfo propertyInfo, Ela
825885

826886
[U]
827887
public void UsingACustomPropertyVisitor()
828-
{
829-
/** Now we can pass an instance of our custom visitor to AutoMap() */
888+
{
889+
/** Now we can pass an instance of our custom visitor to AutoMap() */
830890
var descriptor = new CreateIndexDescriptor("myindex")
831891
.Mappings(ms => ms
832892
.Map<Employee>(m => m.AutoMap(new DisableDocValuesPropertyVisitor()))
833-
);
834-
893+
);
894+
835895
/** and anytime it maps a property as a number (INumberProperty) or boolean (IBooleanProperty)
836896
* it will apply the transformation defined in each Visit() respectively, which in this example
837897
* disables doc values.
838-
*/
898+
*/
839899
var expected = new
840900
{
841901
mappings = new
@@ -875,12 +935,12 @@ public void UsingACustomPropertyVisitor()
875935
}
876936
}
877937
};
878-
}
879-
938+
}
939+
880940
/** You can even take the visitor approach a step further, and instead of visiting on IProperty types, visit
881941
* directly on your POCO properties (PropertyInfo). For example, lets create a visitor that maps all CLR types
882942
* to an Elasticsearch string (IStringProperty).
883-
*/
943+
*/
884944
public class EverythingIsAStringPropertyVisitor : NoopPropertyVisitor
885945
{
886946
public override IProperty Visit(PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute) => new StringProperty();

0 commit comments

Comments
 (0)