@@ -773,32 +773,23 @@ public XContentBuilder field(String name, Object value) throws IOException {
773
773
}
774
774
775
775
public XContentBuilder array (String name , Object ... values ) throws IOException {
776
- return field (name ).values (values );
776
+ return field (name ).values (values , true );
777
777
}
778
778
779
- XContentBuilder values (Object [] values ) throws IOException {
779
+ private XContentBuilder values (Object [] values , boolean ensureNoSelfReferences ) throws IOException {
780
780
if (values == null ) {
781
781
return nullValue ();
782
782
}
783
783
784
- // checks that the array of object does not contain references to itself because
785
- // iterating over entries will cause a stackoverflow error
786
- ensureNoSelfReferences (values );
787
-
788
- startArray ();
789
- for (Object o : values ) {
790
- value (o );
791
- }
792
- endArray ();
793
- return this ;
784
+ return value (Arrays .asList (values ), ensureNoSelfReferences );
794
785
}
795
786
796
787
public XContentBuilder value (Object value ) throws IOException {
797
- unknownValue (value );
788
+ unknownValue (value , true );
798
789
return this ;
799
790
}
800
791
801
- private void unknownValue (Object value ) throws IOException {
792
+ private void unknownValue (Object value , boolean ensureNoSelfReferences ) throws IOException {
802
793
if (value == null ) {
803
794
nullValue ();
804
795
return ;
@@ -810,11 +801,11 @@ private void unknownValue(Object value) throws IOException {
810
801
//Path implements Iterable<Path> and causes endless recursion and a StackOverFlow if treated as an Iterable here
811
802
value ((Path ) value );
812
803
} else if (value instanceof Map ) {
813
- map ((Map ) value );
804
+ map ((Map < String ,?> ) value , ensureNoSelfReferences );
814
805
} else if (value instanceof Iterable ) {
815
- value ((Iterable <?>) value );
806
+ value ((Iterable <?>) value , ensureNoSelfReferences );
816
807
} else if (value instanceof Object []) {
817
- values ((Object []) value );
808
+ values ((Object []) value , ensureNoSelfReferences );
818
809
} else if (value instanceof Calendar ) {
819
810
value ((Calendar ) value );
820
811
} else if (value instanceof ReadableInstant ) {
@@ -863,18 +854,25 @@ public XContentBuilder field(String name, Map<String, Object> values) throws IOE
863
854
}
864
855
865
856
public XContentBuilder map (Map <String , ?> values ) throws IOException {
857
+ return map (values , true );
858
+ }
859
+
860
+ private XContentBuilder map (Map <String , ?> values , boolean ensureNoSelfReferences ) throws IOException {
866
861
if (values == null ) {
867
862
return nullValue ();
868
863
}
869
864
870
865
// checks that the map does not contain references to itself because
871
866
// iterating over map entries will cause a stackoverflow error
872
- ensureNoSelfReferences (values );
867
+ if (ensureNoSelfReferences ) {
868
+ ensureNoSelfReferences (values );
869
+ }
873
870
874
871
startObject ();
875
872
for (Map .Entry <String , ?> value : values .entrySet ()) {
876
873
field (value .getKey ());
877
- unknownValue (value .getValue ());
874
+ // pass ensureNoSelfReferences=false as we already performed the check at a higher level
875
+ unknownValue (value .getValue (), false );
878
876
}
879
877
endObject ();
880
878
return this ;
@@ -884,7 +882,7 @@ public XContentBuilder field(String name, Iterable<?> values) throws IOException
884
882
return field (name ).value (values );
885
883
}
886
884
887
- private XContentBuilder value (Iterable <?> values ) throws IOException {
885
+ private XContentBuilder value (Iterable <?> values , boolean ensureNoSelfReferences ) throws IOException {
888
886
if (values == null ) {
889
887
return nullValue ();
890
888
}
@@ -895,11 +893,14 @@ private XContentBuilder value(Iterable<?> values) throws IOException {
895
893
} else {
896
894
// checks that the iterable does not contain references to itself because
897
895
// iterating over entries will cause a stackoverflow error
898
- ensureNoSelfReferences (values );
896
+ if (ensureNoSelfReferences ) {
897
+ ensureNoSelfReferences (values );
898
+ }
899
899
900
900
startArray ();
901
901
for (Object value : values ) {
902
- unknownValue (value );
902
+ // pass ensureNoSelfReferences=false as we already performed the check at a higher level
903
+ unknownValue (value , false );
903
904
}
904
905
endArray ();
905
906
}
@@ -1076,9 +1077,9 @@ private static void ensureNoSelfReferences(final Object value, final Set<Object>
1076
1077
1077
1078
Iterable <?> it ;
1078
1079
if (value instanceof Map ) {
1079
- it = ((Map ) value ).values ();
1080
+ it = ((Map <?,?> ) value ).values ();
1080
1081
} else if ((value instanceof Iterable ) && (value instanceof Path == false )) {
1081
- it = (Iterable ) value ;
1082
+ it = (Iterable <?> ) value ;
1082
1083
} else if (value instanceof Object []) {
1083
1084
it = Arrays .asList ((Object []) value );
1084
1085
} else {
0 commit comments