1
- From eeb5069c974d3146495f8a9dc52653f029d3cc2d Mon Sep 17 00:00:00 2001
1
+ From d68baa42bb82374b2a8f5292d2cc4fb2dcf57f95 Mon Sep 17 00:00:00 2001
2
2
3
- Date: Tue, 4 Feb 2025 11:59:25 -0800
4
- Subject: [PATCH] manually apply JSON patch
3
+ Date: Fri, 7 Feb 2025 10:12:44 -0800
4
+ Subject: [PATCH] fix & manually apply JSON patch
5
5
6
+ Signed-off-by: Min Jin <
[email protected] >
6
7
---
7
- .../io/kubernetes/client/openapi/JSON.java | 36 ++++++++++++++++ ---
8
- 1 file changed, 32 insertions(+), 4 deletions(-)
8
+ .../io/kubernetes/client/openapi/JSON.java | 120 +++++++++++---- ---
9
+ 1 file changed, 72 insertions(+), 48 deletions(-)
9
10
10
11
diff --git a/kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java b/kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java
11
- index dda3ec708..fe902b293 100644
12
+ index 811bc84b9..ad2193a1a 100644
12
13
--- a/kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java
13
14
+++ b/kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java
14
- @@ -23,6 +23,9 @@ import com.google.gson.JsonElement;
15
- import io.gsonfire.GsonFireBuilder;
16
- import io.gsonfire.TypeSelector;
15
+ @@ -14,79 +14,102 @@ package io.kubernetes.client.openapi;
17
16
18
- + import io.kubernetes.client.gson.V1MetadataExclusionStrategy;
17
+ import com.google.gson.Gson;
18
+ import com.google.gson.GsonBuilder;
19
+ + import com.google.gson.JsonElement;
20
+ import com.google.gson.JsonParseException;
21
+ import com.google.gson.TypeAdapter;
22
+ - import com.google.gson.internal.bind.util.ISO8601Utils;
23
+ import com.google.gson.stream.JsonReader;
24
+ import com.google.gson.stream.JsonWriter;
25
+ - import com.google.gson.JsonElement;
26
+ import io.gsonfire.GsonFireBuilder;
27
+ - import io.gsonfire.TypeSelector;
28
+ -
29
+ - import io.kubernetes.client.openapi.models.*;
30
+ - import okio.ByteString;
31
+ -
19
32
+ import io.kubernetes.client.gson.V1StatusPreProcessor;
20
33
+ import io.kubernetes.client.openapi.models.V1Status;
21
- import okio.ByteString;
22
-
23
34
import java.io.IOException;
24
- @@ -35,6 +38,9 @@ import java.time.OffsetDateTime;
25
- import java.time.ZoneId;
26
- import java.time.ZoneOffset;
35
+ import java.io.StringReader;
36
+ import java.lang.reflect.Type;
37
+ import java.text.DateFormat;
38
+ import java.text.ParseException;
39
+ - import java.text.ParsePosition;
40
+ + import java.time.Instant;
41
+ import java.time.LocalDate;
42
+ import java.time.OffsetDateTime;
27
43
import java.time.format.DateTimeFormatter;
28
44
+ import java.time.format.DateTimeFormatterBuilder;
29
45
+ import java.time.format.DateTimeParseException;
30
46
+ import java.time.temporal.ChronoField;
31
47
import java.util.Date;
32
- import java.util.Locale;
48
+ - import java.util.Locale;
33
49
import java.util.Map;
34
- @@ -50,9 +56,20 @@ import java.util.TimeZone;
50
+ - import java.util.HashMap;
51
+ + import okio.ByteString;
52
+
35
53
public class JSON {
36
- private static Gson gson;
37
- private static boolean isLenientOnJson = false;
54
+ +
55
+ private Gson gson;
56
+ +
57
+ private boolean isLenientOnJson = false;
38
58
+
39
59
+ private static final DateTimeFormatter RFC3339MICRO_FORMATTER =
40
- + new DateTimeFormatterBuilder()
41
- + .parseDefaulting(ChronoField.OFFSET_SECONDS, 0)
42
- + .append(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))
43
- + .optionalStart()
44
- + .appendFraction(ChronoField.NANO_OF_SECOND, 6, 6, true)
45
- + .optionalEnd()
46
- + .appendLiteral("Z" )
47
- + .toFormatter();
60
+ + new DateTimeFormatterBuilder()
61
+ + .parseDefaulting(ChronoField.OFFSET_SECONDS, 0)
62
+ + .append(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))
63
+ + .optionalStart()
64
+ + .appendFraction(ChronoField.NANO_OF_SECOND, 6, 6, true)
65
+ + .optionalEnd()
66
+ + .appendOffsetId( )
67
+ + .toFormatter();
48
68
+
49
- private static DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
50
- private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
51
- - private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
52
- + private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(RFC3339MICRO_FORMATTER);
53
- private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
54
- private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();
55
-
56
- @@ -65,8 +82,11 @@ public class JSON {
69
+ private DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
70
+ +
71
+ private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
72
+ - private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
73
+ +
74
+ + private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter =
75
+ + new OffsetDateTimeTypeAdapter(RFC3339MICRO_FORMATTER);
76
+ +
77
+ private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
78
+ +
79
+ private ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();
80
+
57
81
public static GsonBuilder createGson() {
58
- GsonFireBuilder fireBuilder = new GsonFireBuilder()
59
- ;
82
+ - GsonFireBuilder fireBuilder = new GsonFireBuilder()
83
+ - ;
60
84
- GsonBuilder builder = fireBuilder.createGsonBuilder();
61
- - return builder ;
85
+ + GsonFireBuilder fireBuilder = new GsonFireBuilder() ;
62
86
+ GsonBuilder builder =
63
87
+ fireBuilder
64
88
+ .registerPreProcessor(V1Status.class, new V1StatusPreProcessor())
65
89
+ .createGsonBuilder();
66
- + return builder.setExclusionStrategies(new V1MetadataExclusionStrategy()) ;
90
+ return builder;
67
91
}
68
92
69
93
private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {
70
- @@ -793,11 +813,14 @@ public class JSON {
94
+ JsonElement element = readElement.getAsJsonObject().get(discriminatorField);
95
+ if (null == element) {
96
+ - throw new IllegalArgumentException("missing discriminator field: <" + discriminatorField + ">");
97
+ + throw new IllegalArgumentException(
98
+ + "missing discriminator field: <" + discriminatorField + ">");
99
+ }
100
+ return element.getAsString();
101
+ }
102
+
103
+ /**
104
+ - * Returns the Java class that implements the OpenAPI schema for the specified discriminator value.
105
+ + * Returns the Java class that implements the OpenAPI schema for the specified discriminator
106
+ + * value.
107
+ *
108
+ * @param classByDiscriminatorValue The map of discriminator values to Java classes.
109
+ * @param discriminatorValue The value of the OpenAPI discriminator in the input data.
110
+ * @return The Java class that implements the OpenAPI schema
111
+ */
112
+ - private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) {
113
+ + private static Class getClassByDiscriminator(
114
+ + Map classByDiscriminatorValue, String discriminatorValue) {
115
+ Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue);
116
+ if (null == clazz) {
117
+ - throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">");
118
+ + throw new IllegalArgumentException(
119
+ + "cannot determine model class of name: <" + discriminatorValue + ">");
120
+ }
121
+ return clazz;
122
+ }
123
+
124
+ public JSON() {
125
+ - gson = createGson()
126
+ - .registerTypeAdapter(Date.class, dateTypeAdapter)
127
+ - .registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
128
+ - .registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
129
+ - .registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
130
+ - .registerTypeAdapter(byte[].class, byteArrayAdapter)
131
+ - .create();
132
+ + gson =
133
+ + createGson()
134
+ + .registerTypeAdapter(Date.class, dateTypeAdapter)
135
+ + .registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
136
+ + .registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
137
+ + .registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
138
+ + .registerTypeAdapter(byte[].class, byteArrayAdapter)
139
+ + .create();
140
+ }
141
+
142
+ /**
143
+ @@ -127,8 +150,8 @@ public class JSON {
144
+ /**
145
+ * Deserialize the given JSON string to Java object.
146
+ *
147
+ - * @param <T> Type
148
+ - * @param body The JSON string
149
+ + * @param <T> Type
150
+ + * @param body The JSON string
151
+ * @param returnType The type to deserialize into
152
+ * @return The deserialized Java object
153
+ */
154
+ @@ -137,7 +160,8 @@ public class JSON {
155
+ try {
156
+ if (isLenientOnJson) {
157
+ JsonReader jsonReader = new JsonReader(new StringReader(body));
158
+ - // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
159
+ + // see
160
+ + // https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
161
+ jsonReader.setLenient(true);
162
+ return gson.fromJson(jsonReader, returnType);
163
+ } else {
164
+ @@ -154,18 +178,19 @@ public class JSON {
165
+ }
166
+ }
167
+
168
+ - /**
169
+ - * Gson TypeAdapter for Byte Array type
170
+ - */
171
+ + /** Gson TypeAdapter for Byte Array type */
172
+ public class ByteArrayAdapter extends TypeAdapter<byte[]> {
71
173
72
174
@Override
73
175
public void write(JsonWriter out, byte[] value) throws IOException {
@@ -82,20 +184,98 @@ index dda3ec708..fe902b293 100644
82
184
}
83
185
84
186
@Override
85
- @@ -853,7 +876,12 @@ public class JSON {
187
+ @@ -182,9 +207,7 @@ public class JSON {
188
+ }
189
+ }
190
+
191
+ - /**
192
+ - * Gson TypeAdapter for JSR310 OffsetDateTime type
193
+ - */
194
+ + /** Gson TypeAdapter for JSR310 OffsetDateTime type */
195
+ public static class OffsetDateTimeTypeAdapter extends TypeAdapter<OffsetDateTime> {
196
+
197
+ private DateTimeFormatter formatter;
198
+ @@ -219,16 +242,19 @@ public class JSON {
199
+ default:
200
+ String date = in.nextString();
86
201
if (date.endsWith("+0000")) {
87
- date = date.substring(0, date.length()-5) + "Z";
88
- }
89
- - return OffsetDateTime.parse(date, formatter);
202
+ - date = date.substring(0, date.length()-5) + "Z";
203
+ + date = date.substring(0, date.length() - 5) + "Z";
204
+ + }
90
205
+ try {
91
206
+ return OffsetDateTime.parse(date, formatter);
92
207
+ } catch (DateTimeParseException e) {
93
208
+ // backward-compatibility for ISO8601 timestamp format
94
209
+ return OffsetDateTime.parse(date, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
95
- + }
210
+ }
211
+ - return OffsetDateTime.parse(date, formatter);
96
212
}
97
213
}
98
214
}
215
+
216
+ - /**
217
+ - * Gson TypeAdapter for JSR310 LocalDate type
218
+ - */
219
+ + /** Gson TypeAdapter for JSR310 LocalDate type */
220
+ public class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
221
+
222
+ private DateTimeFormatter formatter;
223
+ @@ -278,9 +304,8 @@ public class JSON {
224
+ }
225
+
226
+ /**
227
+ - * Gson TypeAdapter for java.sql.Date type
228
+ - * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used
229
+ - * (more efficient than SimpleDateFormat).
230
+ + * Gson TypeAdapter for java.sql.Date type If the dateFormat is null, a simple "yyyy-MM-dd" format
231
+ + * will be used (more efficient than SimpleDateFormat).
232
+ */
233
+ public static class SqlDateTypeAdapter extends TypeAdapter<java.sql.Date> {
234
+
235
+ @@ -323,7 +348,8 @@ public class JSON {
236
+ if (dateFormat != null) {
237
+ return new java.sql.Date(dateFormat.parse(date).getTime());
238
+ }
239
+ - return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
240
+ + return new java.sql.Date(
241
+ + Instant.from(DateTimeFormatter.ISO_INSTANT.parse(date)).toEpochMilli());
242
+ } catch (ParseException e) {
243
+ throw new JsonParseException(e);
244
+ }
245
+ @@ -332,8 +358,7 @@ public class JSON {
246
+ }
247
+
248
+ /**
249
+ - * Gson TypeAdapter for java.util.Date type
250
+ - * If the dateFormat is null, ISO8601Utils will be used.
251
+ + * Gson TypeAdapter for java.util.Date type If the dateFormat is null, ISO8601Utils will be used.
252
+ */
253
+ public static class DateTypeAdapter extends TypeAdapter<Date> {
254
+
255
+ @@ -358,7 +383,7 @@ public class JSON {
256
+ if (dateFormat != null) {
257
+ value = dateFormat.format(date);
258
+ } else {
259
+ - value = ISO8601Utils.format(date, true);
260
+ + value = DateTimeFormatter.ISO_INSTANT.format(date.toInstant());
261
+ }
262
+ out.value(value);
263
+ }
264
+ @@ -377,7 +402,7 @@ public class JSON {
265
+ if (dateFormat != null) {
266
+ return dateFormat.parse(date);
267
+ }
268
+ - return ISO8601Utils.parse(date, new ParsePosition(0));
269
+ + return Date.from(Instant.from(DateTimeFormatter.ISO_INSTANT.parse(date)));
270
+ } catch (ParseException e) {
271
+ throw new JsonParseException(e);
272
+ }
273
+ @@ -397,5 +422,4 @@ public class JSON {
274
+ sqlDateTypeAdapter.setFormat(dateFormat);
275
+ return this;
276
+ }
277
+ -
278
+ }
99
279
- -
100
280
2.40.0
101
281
0 commit comments