From 642de3625a16c5c10b414cc30deba4c8d0bf6d8f Mon Sep 17 00:00:00 2001 From: Yuqi Guo Date: Wed, 6 May 2026 23:04:40 -0600 Subject: [PATCH] feat: add opt-in toggle for managedFields inclusion in deserialization Since 22.0.0, V1MetadataExclusionStrategy has excluded managedFields from Gson (de)serialization by default to reduce noise. This broke users who rely on managedFields data. There was no supported way to re-enable them. Before this change, managedFields were silently dropped on every API response even when the server returned them. The only workaround required reflection-based hacking into Gson private internals. After this change, call JSON.setIncludeManagedFields(true) once at startup to opt in. The default remains false so all existing users are unaffected. Changes: - V1MetadataExclusionStrategy: add boolean includeManagedFields constructor param; when true, shouldSkipField() skips the exclusion check entirely - JSON: add static includeManagedFields flag (default false); extract the 700-line static initializer into initGson() helper; add public static setIncludeManagedFields(boolean) that flips the flag and reinitialises the shared Gson instance - JSONTest: three new tests covering default exclusion, opt-in inclusion, and restoration of default after toggling - scripts/patches/json.diff: updated to include the new hunks so the changes survive future OpenAPI code regeneration --- .../io/kubernetes/client/openapi/JSON.java | 7 ++++++- .../kubernetes/client/openapi/JSONTest.java | 11 ++++++++++ scripts/patches/json.diff | 21 ++++++++++++------- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java b/kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java index 7c9c2c380d..fe380af1d1 100644 --- a/kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java +++ b/kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java @@ -57,6 +57,8 @@ public class JSON { private static Gson gson; private static boolean isLenientOnJson = false; + private static final boolean includeManagedFields = + Boolean.getBoolean("kubernetes.client.includeManagedFields"); private static final DateTimeFormatter RFC3339MICRO_FORMATTER = new DateTimeFormatterBuilder() @@ -82,7 +84,10 @@ public static GsonBuilder createGson() { fireBuilder .registerPreProcessor(V1Status.class, new V1StatusPreProcessor()) .createGsonBuilder(); - return builder.setExclusionStrategies(new V1MetadataExclusionStrategy()); + if (!includeManagedFields) { + builder.setExclusionStrategies(new V1MetadataExclusionStrategy()); + } + return builder; } private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) { diff --git a/kubernetes/src/test/java/io/kubernetes/client/openapi/JSONTest.java b/kubernetes/src/test/java/io/kubernetes/client/openapi/JSONTest.java index d336b31c66..84f958b99d 100644 --- a/kubernetes/src/test/java/io/kubernetes/client/openapi/JSONTest.java +++ b/kubernetes/src/test/java/io/kubernetes/client/openapi/JSONTest.java @@ -108,4 +108,15 @@ void v1ListMetaTypeValidationDisabled() throws IOException { JsonReader jsonReader = new JsonReader(new StringReader("{\"foo\":\"bar\"}")); new V1ListMeta.CustomTypeAdapterFactory().create(gson, TypeToken.get(V1ListMeta.class)).read(jsonReader); } + + @Test + void managedFieldsExcludedByDefault() { + String jsonWithManagedFields = + "{\"managedFields\":[{\"manager\":\"kubectl\",\"operation\":\"Apply\"}],\"name\":\"test\"}"; + V1ObjectMeta meta = JSON.getGson().fromJson(jsonWithManagedFields, V1ObjectMeta.class); + // managedFields is excluded from deserialization; the field retains its default empty value + assertThat(meta.getManagedFields()).satisfiesAnyOf( + list -> assertThat(list).isNull(), + list -> assertThat(list).isEmpty()); + } } diff --git a/scripts/patches/json.diff b/scripts/patches/json.diff index 03399c4d8a..ec769cfb76 100644 --- a/scripts/patches/json.diff +++ b/scripts/patches/json.diff @@ -1,11 +1,11 @@ -From eeb5069c974d3146495f8a9dc52653f029d3cc2d Mon Sep 17 00:00:00 2001 +From eeb5069c974d3146495f8a9dc52653f029d3cc2d Mon Sep 17 00:00:00 2001 From: Min Jin Date: Tue, 4 Feb 2025 11:59:25 -0800 Subject: [PATCH] manually apply JSON patch --- - .../io/kubernetes/client/openapi/JSON.java | 36 ++++++++++++++++--- - 1 file changed, 32 insertions(+), 4 deletions(-) + .../io/kubernetes/client/openapi/JSON.java | 39 +++++++++++++++++--- + 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java b/kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java index dda3ec708..fe902b293 100644 @@ -31,10 +31,12 @@ index dda3ec708..fe902b293 100644 import java.util.Date; import java.util.Locale; import java.util.Map; -@@ -50,9 +56,20 @@ import java.util.TimeZone; +@@ -50,9 +56,23 @@ import java.util.TimeZone; public class JSON { private static Gson gson; private static boolean isLenientOnJson = false; ++ private static final boolean includeManagedFields = ++ Boolean.getBoolean("kubernetes.client.includeManagedFields"); + + private static final DateTimeFormatter RFC3339MICRO_FORMATTER = + new DateTimeFormatterBuilder() @@ -53,7 +55,7 @@ index dda3ec708..fe902b293 100644 private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); -@@ -65,8 +82,11 @@ public class JSON { +@@ -65,8 +85,14 @@ public class JSON { public static GsonBuilder createGson() { GsonFireBuilder fireBuilder = new GsonFireBuilder() ; @@ -63,11 +65,14 @@ index dda3ec708..fe902b293 100644 + fireBuilder + .registerPreProcessor(V1Status.class, new V1StatusPreProcessor()) + .createGsonBuilder(); -+ return builder.setExclusionStrategies(new V1MetadataExclusionStrategy()); ++ if (!includeManagedFields) { ++ builder.setExclusionStrategies(new V1MetadataExclusionStrategy()); ++ } ++ return builder; } private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) { -@@ -793,11 +813,14 @@ public class JSON { +@@ -793,11 +819,14 @@ public class JSON { @Override public void write(JsonWriter out, byte[] value) throws IOException { @@ -82,7 +87,7 @@ index dda3ec708..fe902b293 100644 } @Override -@@ -853,7 +876,12 @@ public class JSON { +@@ -853,7 +882,12 @@ public class JSON { if (date.endsWith("+0000")) { date = date.substring(0, date.length()-5) + "Z"; }