@@ -96,7 +96,7 @@ public String toString() {
96
96
97
97
public static class Defaults {
98
98
public static final boolean ENABLED = true ;
99
- public static final Optional <Subobjects > SUBOBJECTS = Optional . empty ( );
99
+ public static final Explicit <Subobjects > SUBOBJECTS = Explicit . implicit ( Subobjects . ENABLED );
100
100
public static final Explicit <Boolean > STORE_ARRAY_SOURCE = Explicit .IMPLICIT_FALSE ;
101
101
public static final Dynamic DYNAMIC = Dynamic .TRUE ;
102
102
}
@@ -133,13 +133,17 @@ static Dynamic getRootDynamic(MappingLookup mappingLookup) {
133
133
}
134
134
135
135
public static class Builder extends Mapper .Builder {
136
- protected Optional <Subobjects > subobjects ;
136
+ protected Explicit <Subobjects > subobjects ;
137
137
protected Explicit <Boolean > enabled = Explicit .IMPLICIT_TRUE ;
138
138
protected Optional <SourceKeepMode > sourceKeepMode = Optional .empty ();
139
139
protected Dynamic dynamic ;
140
140
protected final List <Mapper .Builder > mappersBuilders = new ArrayList <>();
141
141
142
- public Builder (String name , Optional <Subobjects > subobjects ) {
142
+ public Builder (String name ) {
143
+ this (name , Defaults .SUBOBJECTS );
144
+ }
145
+
146
+ public Builder (String name , Explicit <Subobjects > subobjects ) {
143
147
super (name );
144
148
this .subobjects = subobjects ;
145
149
}
@@ -184,7 +188,7 @@ public Mapper build(MapperBuilderContext context) {
184
188
public final void addDynamic (String name , String prefix , Mapper mapper , DocumentParserContext context ) {
185
189
// If the mapper to add has no dots, or the current object mapper has subobjects set to false,
186
190
// we just add it as it is for sure a leaf mapper
187
- if (name .contains ("." ) == false || ( subobjects .isPresent () && ( subobjects . get () == Subobjects .DISABLED )) ) {
191
+ if (name .contains ("." ) == false || subobjects .value () == Subobjects .DISABLED ) {
188
192
add (name , mapper );
189
193
} else {
190
194
// We strip off the first object path of the mapper name, load or create
@@ -198,7 +202,7 @@ public final void addDynamic(String name, String prefix, Mapper mapper, Document
198
202
if (parentBuilder != null ) {
199
203
parentBuilder .addDynamic (name .substring (firstDotIndex + 1 ), immediateChildFullName , mapper , context );
200
204
add (parentBuilder );
201
- } else if (subobjects .isPresent () && subobjects . get () == Subobjects .AUTO ) {
205
+ } else if (subobjects .value () == Subobjects .AUTO ) {
202
206
// No matching parent object was found, the mapper is added as a leaf - similar to subobjects false.
203
207
add (name , mapper );
204
208
} else {
@@ -236,7 +240,7 @@ protected final Map<String, Mapper> buildMappers(MapperBuilderContext mapperBuil
236
240
// mix of object notation and dot notation.
237
241
mapper = existing .merge (mapper , MapperMergeContext .from (mapperBuilderContext , Long .MAX_VALUE ));
238
242
}
239
- if (subobjects .isPresent () && subobjects . get () == Subobjects .DISABLED && mapper instanceof ObjectMapper objectMapper ) {
243
+ if (subobjects .value () == Subobjects .DISABLED && mapper instanceof ObjectMapper objectMapper ) {
240
244
// We're parsing a mapping that has set `subobjects: false` but has defined sub-objects
241
245
objectMapper .asFlattenedFieldMappers (mapperBuilderContext ).forEach (m -> mappers .put (m .leafName (), m ));
242
246
} else {
@@ -279,7 +283,7 @@ public boolean supportsVersion(IndexVersion indexCreatedVersion) {
279
283
public Mapper .Builder parse (String name , Map <String , Object > node , MappingParserContext parserContext )
280
284
throws MapperParsingException {
281
285
parserContext .incrementMappingObjectDepth (); // throws MapperParsingException if depth limit is exceeded
282
- Optional <Subobjects > subobjects = parseSubobjects (node );
286
+ Explicit <Subobjects > subobjects = parseSubobjects (node );
283
287
Builder builder = new Builder (name , subobjects );
284
288
parseObjectFields (node , parserContext , builder );
285
289
parserContext .decrementMappingObjectDepth ();
@@ -344,10 +348,10 @@ protected static boolean parseObjectOrDocumentTypeProperties(
344
348
return false ;
345
349
}
346
350
347
- protected static Optional <Subobjects > parseSubobjects (Map <String , Object > node ) {
351
+ protected static Explicit <Subobjects > parseSubobjects (Map <String , Object > node ) {
348
352
Object subobjectsNode = node .remove ("subobjects" );
349
353
if (subobjectsNode != null ) {
350
- return Optional .of (Subobjects .from (subobjectsNode ));
354
+ return Explicit .of (Subobjects .from (subobjectsNode ));
351
355
}
352
356
return Defaults .SUBOBJECTS ;
353
357
}
@@ -384,9 +388,7 @@ protected static void parseProperties(Builder objBuilder, Map<String, Object> pr
384
388
}
385
389
}
386
390
387
- if (objBuilder .subobjects .isPresent ()
388
- && objBuilder .subobjects .get () == Subobjects .DISABLED
389
- && type .equals (NestedObjectMapper .CONTENT_TYPE )) {
391
+ if (objBuilder .subobjects .value () == Subobjects .DISABLED && type .equals (NestedObjectMapper .CONTENT_TYPE )) {
390
392
throw new MapperParsingException (
391
393
"Tried to add nested object ["
392
394
+ fieldName
@@ -408,7 +410,7 @@ protected static void parseProperties(Builder objBuilder, Map<String, Object> pr
408
410
);
409
411
}
410
412
Mapper .Builder fieldBuilder ;
411
- if (objBuilder .subobjects .isPresent () && objBuilder . subobjects . get () != Subobjects .ENABLED ) {
413
+ if (objBuilder .subobjects .value () != Subobjects .ENABLED ) {
412
414
fieldBuilder = typeParser .parse (fieldName , propNode , parserContext );
413
415
} else {
414
416
String [] fieldNameParts = fieldName .split ("\\ ." );
@@ -456,7 +458,7 @@ private static void validateFieldName(String fieldName, IndexVersion indexCreate
456
458
private final String fullPath ;
457
459
458
460
protected final Explicit <Boolean > enabled ;
459
- protected final Optional <Subobjects > subobjects ;
461
+ protected final Explicit <Subobjects > subobjects ;
460
462
protected final Optional <SourceKeepMode > sourceKeepMode ;
461
463
protected final Dynamic dynamic ;
462
464
@@ -466,7 +468,7 @@ private static void validateFieldName(String fieldName, IndexVersion indexCreate
466
468
String name ,
467
469
String fullPath ,
468
470
Explicit <Boolean > enabled ,
469
- Optional <Subobjects > subobjects ,
471
+ Explicit <Subobjects > subobjects ,
470
472
Optional <SourceKeepMode > sourceKeepMode ,
471
473
Dynamic dynamic ,
472
474
Map <String , Mapper > mappers
@@ -484,9 +486,7 @@ private static void validateFieldName(String fieldName, IndexVersion indexCreate
484
486
} else {
485
487
this .mappers = Map .copyOf (mappers );
486
488
}
487
- assert subobjects .isEmpty ()
488
- || subobjects .get () != Subobjects .DISABLED
489
- || this .mappers .values ().stream ().noneMatch (m -> m instanceof ObjectMapper )
489
+ assert subobjects .value () != Subobjects .DISABLED || this .mappers .values ().stream ().noneMatch (m -> m instanceof ObjectMapper )
490
490
: "When subobjects is false, mappers must not contain an ObjectMapper" ;
491
491
}
492
492
@@ -540,7 +540,7 @@ public final Dynamic dynamic() {
540
540
}
541
541
542
542
public final Subobjects subobjects () {
543
- return subobjects .orElse ( Subobjects . ENABLED );
543
+ return subobjects .value ( );
544
544
}
545
545
546
546
public final Optional <SourceKeepMode > sourceKeepMode () {
@@ -581,7 +581,7 @@ public ObjectMapper merge(Mapper mergeWith, MapperMergeContext parentMergeContex
581
581
582
582
protected record MergeResult (
583
583
Explicit <Boolean > enabled ,
584
- Optional <Subobjects > subObjects ,
584
+ Explicit <Subobjects > subObjects ,
585
585
Optional <SourceKeepMode > sourceKeepMode ,
586
586
Dynamic dynamic ,
587
587
Map <String , Mapper > mappers
@@ -602,8 +602,8 @@ static MergeResult build(ObjectMapper existing, ObjectMapper mergeWithObject, Ma
602
602
} else {
603
603
enabled = existing .enabled ;
604
604
}
605
- final Optional <Subobjects > subObjects ;
606
- if (mergeWithObject .subobjects .isPresent ()) {
605
+ final Explicit <Subobjects > subObjects ;
606
+ if (mergeWithObject .subobjects .explicit ()) {
607
607
if (reason == MergeReason .INDEX_TEMPLATE ) {
608
608
subObjects = mergeWithObject .subobjects ;
609
609
} else if (existing .subobjects () != mergeWithObject .subobjects ()) {
@@ -650,13 +650,11 @@ private static Map<String, Mapper> buildMergedMappers(
650
650
ObjectMapper existing ,
651
651
ObjectMapper mergeWithObject ,
652
652
MapperMergeContext objectMergeContext ,
653
- Optional <Subobjects > subobjects
653
+ Explicit <Subobjects > subobjects
654
654
) {
655
655
Map <String , Mapper > mergedMappers = new HashMap <>();
656
656
for (Mapper childOfExistingMapper : existing .mappers .values ()) {
657
- if (subobjects .isPresent ()
658
- && subobjects .get () == Subobjects .DISABLED
659
- && childOfExistingMapper instanceof ObjectMapper objectMapper ) {
657
+ if (subobjects .value () == Subobjects .DISABLED && childOfExistingMapper instanceof ObjectMapper objectMapper ) {
660
658
// An existing mapping with sub-objects is merged with a mapping that has set `subobjects: false`
661
659
objectMapper .asFlattenedFieldMappers (objectMergeContext .getMapperBuilderContext ())
662
660
.forEach (m -> mergedMappers .put (m .leafName (), m ));
@@ -667,9 +665,7 @@ private static Map<String, Mapper> buildMergedMappers(
667
665
for (Mapper mergeWithMapper : mergeWithObject ) {
668
666
Mapper mergeIntoMapper = mergedMappers .get (mergeWithMapper .leafName ());
669
667
if (mergeIntoMapper == null ) {
670
- if (subobjects .isPresent ()
671
- && subobjects .get () == Subobjects .DISABLED
672
- && mergeWithMapper instanceof ObjectMapper objectMapper ) {
668
+ if (subobjects .value () == Subobjects .DISABLED && mergeWithMapper instanceof ObjectMapper objectMapper ) {
673
669
// An existing mapping that has set `subobjects: false` is merged with a mapping with sub-objects.
674
670
List <FieldMapper > flattenedMappers = objectMapper .asFlattenedFieldMappers (
675
671
objectMergeContext .getMapperBuilderContext ()
@@ -691,7 +687,7 @@ private static Map<String, Mapper> buildMergedMappers(
691
687
putMergedMapper (mergedMappers , truncateObjectMapper (objectMergeContext , om ));
692
688
}
693
689
} else if (mergeIntoMapper instanceof ObjectMapper objectMapper ) {
694
- assert subobjects .isEmpty () || subobjects .get () != Subobjects .DISABLED
690
+ assert subobjects .explicit () == false || subobjects .value () != Subobjects .DISABLED
695
691
: "existing object mappers are supposed to be flattened if subobjects is false" ;
696
692
putMergedMapper (mergedMappers , objectMapper .merge (mergeWithMapper , objectMergeContext ));
697
693
} else {
@@ -803,7 +799,7 @@ private void ensureFlattenable(MapperBuilderContext context, ContentPath path) {
803
799
if (isEnabled () == false ) {
804
800
throwAutoFlatteningException (path , "the value of [enabled] is [false]" );
805
801
}
806
- if (subobjects .isPresent () && subobjects .get () == Subobjects .ENABLED ) {
802
+ if (subobjects .explicit () && subobjects .value () == Subobjects .ENABLED ) {
807
803
throwAutoFlatteningException (path , "the value of [subobjects] is [true]" );
808
804
}
809
805
}
@@ -838,8 +834,8 @@ void toXContent(XContentBuilder builder, Params params, ToXContent custom) throw
838
834
if (isEnabled () != Defaults .ENABLED ) {
839
835
builder .field ("enabled" , enabled .value ());
840
836
}
841
- if (subobjects .isPresent ()) {
842
- builder .field ("subobjects" , subobjects .get ().printedValue );
837
+ if (subobjects .explicit ()) {
838
+ builder .field ("subobjects" , subobjects .value ().printedValue );
843
839
}
844
840
if (sourceKeepMode .isPresent ()) {
845
841
builder .field ("synthetic_source_keep" , sourceKeepMode .get ());
0 commit comments