Skip to content

Commit 1caa15d

Browse files
Address review feedback: use string buffer instead of YAML dump, remove docs
Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com>
1 parent 7013d59 commit 1caa15d

File tree

2 files changed

+12
-119
lines changed

2 files changed

+12
-119
lines changed

docs/yaml-create-resource.md

Lines changed: 0 additions & 114 deletions
This file was deleted.

util/src/main/java/io/kubernetes/client/util/Yaml.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -629,10 +629,19 @@ public static Object createResource(io.kubernetes.client.openapi.ApiClient clien
629629
*/
630630
public static Object createResource(io.kubernetes.client.openapi.ApiClient client, Reader reader)
631631
throws IOException, io.kubernetes.client.openapi.ApiException {
632+
// Read the entire content into a string first so we can parse it twice
633+
StringBuilder sb = new StringBuilder();
634+
char[] buffer = new char[8192];
635+
int read;
636+
while ((read = reader.read(buffer)) != -1) {
637+
sb.append(buffer, 0, read);
638+
}
639+
String yamlContent = sb.toString();
640+
632641
// Load the YAML as a map to extract apiVersion and kind
633642
// Note: The getSnakeYaml() method already configures LoaderOptions with appropriate
634643
// security settings to prevent YAML bombs and other attacks
635-
Map<String, Object> data = getSnakeYaml(null).load(reader);
644+
Map<String, Object> data = getSnakeYaml(null).load(new StringReader(yamlContent));
636645

637646
String kind = (String) data.get("kind");
638647
if (kind == null) {
@@ -650,10 +659,8 @@ public static Object createResource(io.kubernetes.client.openapi.ApiClient clien
650659
"Unknown apiVersion/kind: " + apiVersion + "/" + kind + ". Is it registered?");
651660
}
652661

653-
// Load the YAML into the strongly typed object
654-
// Note: This double-loading approach (first as Map, then as typed object) follows the
655-
// design recommended in the issue discussion to properly handle type determination
656-
Object resource = loadAs(new StringReader(getSnakeYaml(clazz).dump(data)), clazz);
662+
// Load the YAML into the strongly typed object using the same content
663+
Object resource = loadAs(new StringReader(yamlContent), clazz);
657664

658665
// Ensure the resource is a KubernetesObject
659666
if (!(resource instanceof io.kubernetes.client.common.KubernetesObject)) {

0 commit comments

Comments
 (0)