@@ -501,55 +501,70 @@ public void setString_or_null(String string_or_null) {
501
501
}
502
502
503
503
public void testParseNamedObject () throws IOException {
504
- XContentParser parser = createParser (JsonXContent .jsonXContent , "{\" named\" : { \" a\" : {} }}" );
504
+ XContentParser parser = createParser (JsonXContent .jsonXContent ,
505
+ "{\" named\" : { \" a\" : {\" foo\" : 11} }, \" bar\" : \" baz\" }" );
505
506
NamedObjectHolder h = NamedObjectHolder .PARSER .apply (parser , null );
507
+ assertEquals ("a" , h .named .name );
508
+ assertEquals (11 , h .named .foo );
509
+ assertEquals ("baz" , h .bar );
510
+ }
511
+
512
+ public void testParseNamedObjectUnexpectedArray () throws IOException {
513
+ XContentParser parser = createParser (JsonXContent .jsonXContent , "{\" named\" : [ \" a\" : {\" foo\" : 11} }]" );
514
+ XContentParseException e = expectThrows (XContentParseException .class , () -> NamedObjectHolder .PARSER .apply (parser , null ));
515
+ assertThat (e .getMessage (), containsString ("[named_object_holder] named doesn't support values of type: START_ARRAY" ));
516
+ }
517
+
518
+ public void testParseNamedObjects () throws IOException {
519
+ XContentParser parser = createParser (JsonXContent .jsonXContent , "{\" named\" : { \" a\" : {} }}" );
520
+ NamedObjectsHolder h = NamedObjectsHolder .PARSER .apply (parser , null );
506
521
assertThat (h .named , hasSize (1 ));
507
522
assertEquals ("a" , h .named .get (0 ).name );
508
523
assertFalse (h .namedSuppliedInOrder );
509
524
}
510
525
511
- public void testParseNamedObjectInOrder () throws IOException {
526
+ public void testParseNamedObjectsInOrder () throws IOException {
512
527
XContentParser parser = createParser (JsonXContent .jsonXContent , "{\" named\" : [ {\" a\" : {}} ] }" );
513
- NamedObjectHolder h = NamedObjectHolder .PARSER .apply (parser , null );
528
+ NamedObjectsHolder h = NamedObjectsHolder .PARSER .apply (parser , null );
514
529
assertThat (h .named , hasSize (1 ));
515
530
assertEquals ("a" , h .named .get (0 ).name );
516
531
assertTrue (h .namedSuppliedInOrder );
517
532
}
518
533
519
- public void testParseNamedObjectTwoFieldsInArray () throws IOException {
534
+ public void testParseNamedObjectsTwoFieldsInArray () throws IOException {
520
535
XContentParser parser = createParser (JsonXContent .jsonXContent , "{\" named\" : [ {\" a\" : {}, \" b\" : {}}]}" );
521
- XContentParseException e = expectThrows (XContentParseException .class , () -> NamedObjectHolder .PARSER .apply (parser , null ));
522
- assertThat (e .getMessage (), containsString ("[named_object_holder ] failed to parse field [named]" ));
536
+ XContentParseException e = expectThrows (XContentParseException .class , () -> NamedObjectsHolder .PARSER .apply (parser , null ));
537
+ assertThat (e .getMessage (), containsString ("[named_objects_holder ] failed to parse field [named]" ));
523
538
assertThat (e .getCause ().getMessage (),
524
539
containsString ("[named] can be a single object with any number of fields " +
525
540
"or an array where each entry is an object with a single field" ));
526
541
}
527
542
528
- public void testParseNamedObjectNoFieldsInArray () throws IOException {
543
+ public void testParseNamedObjectsNoFieldsInArray () throws IOException {
529
544
XContentParser parser = createParser (JsonXContent .jsonXContent , "{\" named\" : [ {} ]}" );
530
- XContentParseException e = expectThrows (XContentParseException .class , () -> NamedObjectHolder .PARSER .apply (parser , null ));
531
- assertThat (e .getMessage (), containsString ("[named_object_holder ] failed to parse field [named]" ));
545
+ XContentParseException e = expectThrows (XContentParseException .class , () -> NamedObjectsHolder .PARSER .apply (parser , null ));
546
+ assertThat (e .getMessage (), containsString ("[named_objects_holder ] failed to parse field [named]" ));
532
547
assertThat (e .getCause ().getMessage (),
533
548
containsString ("[named] can be a single object with any number of fields " +
534
549
"or an array where each entry is an object with a single field" ));
535
550
}
536
551
537
- public void testParseNamedObjectJunkInArray () throws IOException {
552
+ public void testParseNamedObjectsJunkInArray () throws IOException {
538
553
XContentParser parser = createParser (JsonXContent .jsonXContent , "{\" named\" : [ \" junk\" ] }" );
539
- XContentParseException e = expectThrows (XContentParseException .class , () -> NamedObjectHolder .PARSER .apply (parser , null ));
540
- assertThat (e .getMessage (), containsString ("[named_object_holder ] failed to parse field [named]" ));
554
+ XContentParseException e = expectThrows (XContentParseException .class , () -> NamedObjectsHolder .PARSER .apply (parser , null ));
555
+ assertThat (e .getMessage (), containsString ("[named_objects_holder ] failed to parse field [named]" ));
541
556
assertThat (e .getCause ().getMessage (),
542
557
containsString ("[named] can be a single object with any number of fields " +
543
558
"or an array where each entry is an object with a single field" ));
544
559
}
545
560
546
- public void testParseNamedObjectInOrderNotSupported () throws IOException {
561
+ public void testParseNamedObjectsInOrderNotSupported () throws IOException {
547
562
XContentParser parser = createParser (JsonXContent .jsonXContent , "{\" named\" : [ {\" a\" : {}} ] }" );
548
563
549
564
// Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above
550
- ObjectParser <NamedObjectHolder , Void > objectParser = new ObjectParser <>("named_object_holder" ,
551
- NamedObjectHolder ::new );
552
- objectParser .declareNamedObjects (NamedObjectHolder ::setNamed , NamedObject .PARSER , new ParseField ("named" ));
565
+ ObjectParser <NamedObjectsHolder , Void > objectParser = new ObjectParser <>("named_object_holder" ,
566
+ NamedObjectsHolder ::new );
567
+ objectParser .declareNamedObjects (NamedObjectsHolder ::setNamed , NamedObject .PARSER , new ParseField ("named" ));
553
568
554
569
// Now firing the xml through it fails
555
570
XContentParseException e = expectThrows (XContentParseException .class , () -> objectParser .apply (parser , null ));
@@ -714,7 +729,7 @@ public void testNoopDeclareField() throws IOException {
714
729
assertEquals ("parser for [noop] did not end on END_ARRAY" , e .getMessage ());
715
730
}
716
731
717
- public void testNoopDeclareObjectArray () throws IOException {
732
+ public void testNoopDeclareObjectArray () {
718
733
ObjectParser <AtomicReference <String >, Void > parser = new ObjectParser <>("noopy" , AtomicReference ::new );
719
734
parser .declareString (AtomicReference ::set , new ParseField ("body" ));
720
735
parser .declareObjectArray ((a ,b ) -> {}, (p , c ) -> null , new ParseField ("noop" ));
@@ -729,11 +744,33 @@ public void testNoopDeclareObjectArray() throws IOException {
729
744
assertEquals ("expected value but got [FIELD_NAME]" , sneakyError .getCause ().getMessage ());
730
745
}
731
746
747
+ // singular
732
748
static class NamedObjectHolder {
733
749
public static final ObjectParser <NamedObjectHolder , Void > PARSER = new ObjectParser <>("named_object_holder" ,
734
750
NamedObjectHolder ::new );
735
751
static {
736
- PARSER .declareNamedObjects (NamedObjectHolder ::setNamed , NamedObject .PARSER , NamedObjectHolder ::keepNamedInOrder ,
752
+ PARSER .declareNamedObject (NamedObjectHolder ::setNamed , NamedObject .PARSER , new ParseField ("named" ));
753
+ PARSER .declareString (NamedObjectHolder ::setBar , new ParseField ("bar" ));
754
+ }
755
+
756
+ private NamedObject named ;
757
+ private String bar ;
758
+
759
+ public void setNamed (NamedObject named ) {
760
+ this .named = named ;
761
+ }
762
+
763
+ public void setBar (String bar ) {
764
+ this .bar = bar ;
765
+ }
766
+ }
767
+
768
+ // plural
769
+ static class NamedObjectsHolder {
770
+ public static final ObjectParser <NamedObjectsHolder , Void > PARSER = new ObjectParser <>("named_objects_holder" ,
771
+ NamedObjectsHolder ::new );
772
+ static {
773
+ PARSER .declareNamedObjects (NamedObjectsHolder ::setNamed , NamedObject .PARSER , NamedObjectsHolder ::keepNamedInOrder ,
737
774
new ParseField ("named" ));
738
775
}
739
776
0 commit comments