@@ -228,43 +228,52 @@ private static <T> T unmarshal(InputStream is, ObjectMapper mapper, TypeReferenc
228228 } while (intch > -1 && Character .isWhitespace (intch ));
229229 bis .reset ();
230230
231- T result = null ;
232- List <KubernetesResource > listResult = null ;
231+ final T result ;
233232 if (intch != '{' && intch != '[' ) {
234- final Load yaml = new Load (LoadSettings .builder ().build ());
235- // if multiple docs exist, only non-null resources will be kept
236- final Iterable <Object > objs = yaml .loadAllFromInputStream (bis );
237- for (Object obj : objs ) {
238- Object value = null ;
239- if (obj instanceof Map ) {
240- value = mapper .convertValue (obj , type );
241- } else if (obj != null ) {
242- value = mapper .convertValue (new RawExtension (obj ), type );
243- }
244- if (value != null ) {
245- if (result == null ) {
246- result = (T ) value ;
247- } else {
248- if (listResult == null ) {
249- listResult = new ArrayList <>();
250- accumulateResult (result , listResult );
251- }
252- accumulateResult (value , listResult );
253- }
254- }
255- }
233+ result = parseYaml (bis , mapper , type );
256234 } else {
257235 result = mapper .readerFor (type ).readValue (bis );
258236 }
259- if (listResult != null ) {
260- return (T ) listResult ;
261- }
262237 return result ;
263238 } catch (IOException e ) {
264239 throw KubernetesClientException .launderThrowable (e );
265240 }
266241 }
267242
243+ /**
244+ * If multiple docs exist, only non-null resources will be kept. Results spanning multiple docs
245+ * will be returned as a List of KubernetesResource
246+ */
247+ private static <T > T parseYaml (BufferedInputStream bis , ObjectMapper mapper , TypeReference <T > type ) {
248+ T result = null ;
249+ List <KubernetesResource > listResult = null ;
250+ final Load yaml = new Load (LoadSettings .builder ().build ());
251+ final Iterable <Object > objs = yaml .loadAllFromInputStream (bis );
252+ for (Object obj : objs ) {
253+ Object value = null ;
254+ if (obj instanceof Map ) {
255+ value = mapper .convertValue (obj , type );
256+ } else if (obj != null ) {
257+ value = mapper .convertValue (new RawExtension (obj ), type );
258+ }
259+ if (value != null ) {
260+ if (result == null ) {
261+ result = (T ) value ;
262+ } else {
263+ if (listResult == null ) {
264+ listResult = new ArrayList <>();
265+ accumulateResult (result , listResult );
266+ }
267+ accumulateResult (value , listResult );
268+ }
269+ }
270+ }
271+ if (listResult != null ) {
272+ return (T ) listResult ;
273+ }
274+ return result ;
275+ }
276+
268277 private static <T > void accumulateResult (T result , List <KubernetesResource > listResult ) {
269278 if (result instanceof KubernetesResourceList ) {
270279 listResult .addAll (((KubernetesResourceList ) result ).getItems ());
0 commit comments