Skip to content

Commit 6bf439c

Browse files
authored
Batch Workaround for Deserialization of Long Properties (#40301)
* Add workaround and tests for long properties * Add changelog description for deserialization fix * Changed placement of TODOs * Regenerate SDK * Update test recordings
1 parent eb263e2 commit 6bf439c

File tree

12 files changed

+403
-26
lines changed

12 files changed

+403
-26
lines changed

Diff for: sdk/batch/azure-compute-batch/CHANGELOG.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
# Release History
22

3-
## 1.0.0-beta.2 (Unreleased)
4-
5-
### Features Added
6-
7-
### Breaking Changes
3+
## 1.0.0-beta.2 (2024-05-22)
84

95
### Bugs Fixed
106

11-
### Other Changes
7+
- Fixed a bug that caused `long` properties on models to be deserialized incorrectly.
128

139
## 1.0.0-beta.1 (2024-05-16)
1410

Diff for: sdk/batch/azure-compute-batch/assets.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "java",
44
"TagPrefix": "java/batch/azure-compute-batch",
5-
"Tag": "java/batch/azure-compute-batch_aff1c3044f"
5+
"Tag": "java/batch/azure-compute-batch_70c93036ed"
66
}

Diff for: sdk/batch/azure-compute-batch/src/main/java/com/azure/compute/batch/models/BatchJobScheduleStatistics.java

+65-6
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
346346
* @throws IllegalStateException If the deserialized JSON object was missing any required properties.
347347
* @throws IOException If an error occurs while reading the BatchJobScheduleStatistics.
348348
*/
349-
@Generated
350349
public static BatchJobScheduleStatistics fromJson(JsonReader jsonReader) throws IOException {
350+
// TODO: Re-add @Generated tag here and re-generate SDK once the 2024-05-01 Batch Service API is released
351351
return jsonReader.readObject(reader -> {
352352
String url = null;
353353
OffsetDateTime startTime = null;
@@ -381,19 +381,78 @@ public static BatchJobScheduleStatistics fromJson(JsonReader jsonReader) throws
381381
} else if ("wallClockTime".equals(fieldName)) {
382382
wallClockTime = reader.getNullable(nonNullReader -> Duration.parse(nonNullReader.getString()));
383383
} else if ("readIOps".equals(fieldName)) {
384-
readIOps = reader.getLong();
384+
if (reader.currentToken() == JsonToken.STRING) {
385+
String readIOpsStr = reader.getString();
386+
try {
387+
readIOps = Long.parseLong(readIOpsStr);
388+
} catch (NumberFormatException e) {
389+
throw new IOException("Expected numeric readIOps, but found: " + readIOpsStr, e);
390+
}
391+
} else if (reader.currentToken() == JsonToken.NUMBER) {
392+
readIOps = reader.getLong();
393+
} else {
394+
throw new IOException("Expected readIOps to be a number or string, but found other type");
395+
}
385396
} else if ("writeIOps".equals(fieldName)) {
386-
writeIOps = reader.getLong();
397+
if (reader.currentToken() == JsonToken.STRING) {
398+
String writeIOpsStr = reader.getString();
399+
try {
400+
writeIOps = Long.parseLong(writeIOpsStr);
401+
} catch (NumberFormatException e) {
402+
throw new IOException("Expected numeric writeIOps, but found: " + writeIOpsStr, e);
403+
}
404+
} else if (reader.currentToken() == JsonToken.NUMBER) {
405+
writeIOps = reader.getLong();
406+
} else {
407+
throw new IOException("Expected writeIOps to be a number or string, but found other type");
408+
}
387409
} else if ("readIOGiB".equals(fieldName)) {
388410
readIOGiB = reader.getDouble();
389411
} else if ("writeIOGiB".equals(fieldName)) {
390412
writeIOGiB = reader.getDouble();
391413
} else if ("numSucceededTasks".equals(fieldName)) {
392-
numSucceededTasks = reader.getLong();
414+
if (reader.currentToken() == JsonToken.STRING) {
415+
String numSucceededTasksStr = reader.getString();
416+
try {
417+
numSucceededTasks = Long.parseLong(numSucceededTasksStr);
418+
} catch (NumberFormatException e) {
419+
throw new IOException(
420+
"Expected numeric numSucceededTasks, but found: " + numSucceededTasksStr, e);
421+
}
422+
} else if (reader.currentToken() == JsonToken.NUMBER) {
423+
numSucceededTasks = reader.getLong();
424+
} else {
425+
throw new IOException(
426+
"Expected numSucceededTasks to be a number or string, but found other type");
427+
}
393428
} else if ("numFailedTasks".equals(fieldName)) {
394-
numFailedTasks = reader.getLong();
429+
if (reader.currentToken() == JsonToken.STRING) {
430+
String numFailedTasksStr = reader.getString();
431+
try {
432+
numFailedTasks = Long.parseLong(numFailedTasksStr);
433+
} catch (NumberFormatException e) {
434+
throw new IOException("Expected numeric numFailedTasks, but found: " + numFailedTasksStr,
435+
e);
436+
}
437+
} else if (reader.currentToken() == JsonToken.NUMBER) {
438+
numFailedTasks = reader.getLong();
439+
} else {
440+
throw new IOException("Expected numFailedTasks to be a number or string, but found other type");
441+
}
395442
} else if ("numTaskRetries".equals(fieldName)) {
396-
numTaskRetries = reader.getLong();
443+
if (reader.currentToken() == JsonToken.STRING) {
444+
String numTaskRetriesStr = reader.getString();
445+
try {
446+
numTaskRetries = Long.parseLong(numTaskRetriesStr);
447+
} catch (NumberFormatException e) {
448+
throw new IOException("Expected numeric numTaskRetries, but found: " + numTaskRetriesStr,
449+
e);
450+
}
451+
} else if (reader.currentToken() == JsonToken.NUMBER) {
452+
numTaskRetries = reader.getLong();
453+
} else {
454+
throw new IOException("Expected numTaskRetries to be a number or string, but found other type");
455+
}
397456
} else if ("waitTime".equals(fieldName)) {
398457
waitTime = reader.getNullable(nonNullReader -> Duration.parse(nonNullReader.getString()));
399458
} else {

Diff for: sdk/batch/azure-compute-batch/src/main/java/com/azure/compute/batch/models/BatchJobStatistics.java

+65-6
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
339339
* @throws IllegalStateException If the deserialized JSON object was missing any required properties.
340340
* @throws IOException If an error occurs while reading the BatchJobStatistics.
341341
*/
342-
@Generated
343342
public static BatchJobStatistics fromJson(JsonReader jsonReader) throws IOException {
343+
// TODO: Re-add @Generated tag here and re-generate SDK once the 2024-05-01 Batch Service API is released
344344
return jsonReader.readObject(reader -> {
345345
String url = null;
346346
OffsetDateTime startTime = null;
@@ -374,19 +374,78 @@ public static BatchJobStatistics fromJson(JsonReader jsonReader) throws IOExcept
374374
} else if ("wallClockTime".equals(fieldName)) {
375375
wallClockTime = reader.getNullable(nonNullReader -> Duration.parse(nonNullReader.getString()));
376376
} else if ("readIOps".equals(fieldName)) {
377-
readIOps = reader.getLong();
377+
if (reader.currentToken() == JsonToken.STRING) {
378+
String readIOpsStr = reader.getString();
379+
try {
380+
readIOps = Long.parseLong(readIOpsStr);
381+
} catch (NumberFormatException e) {
382+
throw new IOException("Expected numeric readIOps, but found: " + readIOpsStr, e);
383+
}
384+
} else if (reader.currentToken() == JsonToken.NUMBER) {
385+
readIOps = reader.getLong();
386+
} else {
387+
throw new IOException("Expected readIOps to be a number or string, but found other type");
388+
}
378389
} else if ("writeIOps".equals(fieldName)) {
379-
writeIOps = reader.getLong();
390+
if (reader.currentToken() == JsonToken.STRING) {
391+
String writeIOpsStr = reader.getString();
392+
try {
393+
writeIOps = Long.parseLong(writeIOpsStr);
394+
} catch (NumberFormatException e) {
395+
throw new IOException("Expected numeric writeIOps, but found: " + writeIOpsStr, e);
396+
}
397+
} else if (reader.currentToken() == JsonToken.NUMBER) {
398+
writeIOps = reader.getLong();
399+
} else {
400+
throw new IOException("Expected writeIOps to be a number or string, but found other type");
401+
}
380402
} else if ("readIOGiB".equals(fieldName)) {
381403
readIOGiB = reader.getDouble();
382404
} else if ("writeIOGiB".equals(fieldName)) {
383405
writeIOGiB = reader.getDouble();
384406
} else if ("numSucceededTasks".equals(fieldName)) {
385-
numSucceededTasks = reader.getLong();
407+
if (reader.currentToken() == JsonToken.STRING) {
408+
String numSucceededTasksStr = reader.getString();
409+
try {
410+
numSucceededTasks = Long.parseLong(numSucceededTasksStr);
411+
} catch (NumberFormatException e) {
412+
throw new IOException(
413+
"Expected numeric numSucceededTasks, but found: " + numSucceededTasksStr, e);
414+
}
415+
} else if (reader.currentToken() == JsonToken.NUMBER) {
416+
numSucceededTasks = reader.getLong();
417+
} else {
418+
throw new IOException(
419+
"Expected numSucceededTasks to be a number or string, but found other type");
420+
}
386421
} else if ("numFailedTasks".equals(fieldName)) {
387-
numFailedTasks = reader.getLong();
422+
if (reader.currentToken() == JsonToken.STRING) {
423+
String numFailedTasksStr = reader.getString();
424+
try {
425+
numFailedTasks = Long.parseLong(numFailedTasksStr);
426+
} catch (NumberFormatException e) {
427+
throw new IOException("Expected numeric numFailedTasks, but found: " + numFailedTasksStr,
428+
e);
429+
}
430+
} else if (reader.currentToken() == JsonToken.NUMBER) {
431+
numFailedTasks = reader.getLong();
432+
} else {
433+
throw new IOException("Expected numFailedTasks to be a number or string, but found other type");
434+
}
388435
} else if ("numTaskRetries".equals(fieldName)) {
389-
numTaskRetries = reader.getLong();
436+
if (reader.currentToken() == JsonToken.STRING) {
437+
String numTaskRetriesStr = reader.getString();
438+
try {
439+
numTaskRetries = Long.parseLong(numTaskRetriesStr);
440+
} catch (NumberFormatException e) {
441+
throw new IOException("Expected numeric numTaskRetries, but found: " + numTaskRetriesStr,
442+
e);
443+
}
444+
} else if (reader.currentToken() == JsonToken.NUMBER) {
445+
numTaskRetries = reader.getLong();
446+
} else {
447+
throw new IOException("Expected numTaskRetries to be a number or string, but found other type");
448+
}
390449
} else if ("waitTime".equals(fieldName)) {
391450
waitTime = reader.getNullable(nonNullReader -> Duration.parse(nonNullReader.getString()));
392451
} else {

Diff for: sdk/batch/azure-compute-batch/src/main/java/com/azure/compute/batch/models/BatchPoolResourceStatistics.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
305305
* @throws IllegalStateException If the deserialized JSON object was missing any required properties.
306306
* @throws IOException If an error occurs while reading the BatchPoolResourceStatistics.
307307
*/
308-
@Generated
309308
public static BatchPoolResourceStatistics fromJson(JsonReader jsonReader) throws IOException {
309+
// TODO: Re-add @Generated tag here and re-generate SDK once the 2024-05-01 Batch Service API is released
310310
return jsonReader.readObject(reader -> {
311311
OffsetDateTime startTime = null;
312312
OffsetDateTime lastUpdateTime = null;
@@ -341,9 +341,31 @@ public static BatchPoolResourceStatistics fromJson(JsonReader jsonReader) throws
341341
} else if ("peakDiskGiB".equals(fieldName)) {
342342
peakDiskGiB = reader.getDouble();
343343
} else if ("diskReadIOps".equals(fieldName)) {
344-
diskReadIOps = reader.getLong();
344+
if (reader.currentToken() == JsonToken.STRING) {
345+
String diskReadIOpsStr = reader.getString();
346+
try {
347+
diskReadIOps = Long.parseLong(diskReadIOpsStr);
348+
} catch (NumberFormatException e) {
349+
throw new IOException("Expected numeric diskReadIOps, but found: " + diskReadIOpsStr, e);
350+
}
351+
} else if (reader.currentToken() == JsonToken.NUMBER) {
352+
diskReadIOps = reader.getLong();
353+
} else {
354+
throw new IOException("Expected diskReadIOps to be a number or string, but found other type");
355+
}
345356
} else if ("diskWriteIOps".equals(fieldName)) {
346-
diskWriteIOps = reader.getLong();
357+
if (reader.currentToken() == JsonToken.STRING) {
358+
String diskWriteIOpsStr = reader.getString();
359+
try {
360+
diskWriteIOps = Long.parseLong(diskWriteIOpsStr);
361+
} catch (NumberFormatException e) {
362+
throw new IOException("Expected numeric diskWriteIOps, but found: " + diskWriteIOpsStr, e);
363+
}
364+
} else if (reader.currentToken() == JsonToken.NUMBER) {
365+
diskWriteIOps = reader.getLong();
366+
} else {
367+
throw new IOException("Expected diskWriteIOps to be a number or string, but found other type");
368+
}
347369
} else if ("diskReadGiB".equals(fieldName)) {
348370
diskReadGiB = reader.getDouble();
349371
} else if ("diskWriteGiB".equals(fieldName)) {

Diff for: sdk/batch/azure-compute-batch/src/main/java/com/azure/compute/batch/models/BatchTaskStatistics.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
274274
* @throws IllegalStateException If the deserialized JSON object was missing any required properties.
275275
* @throws IOException If an error occurs while reading the BatchTaskStatistics.
276276
*/
277-
@Generated
278277
public static BatchTaskStatistics fromJson(JsonReader jsonReader) throws IOException {
278+
// TODO: Re-add @Generated tag here and re-generate SDK once the 2024-05-01 Batch Service API is released
279279
return jsonReader.readObject(reader -> {
280280
String url = null;
281281
OffsetDateTime startTime = null;
@@ -306,9 +306,31 @@ public static BatchTaskStatistics fromJson(JsonReader jsonReader) throws IOExcep
306306
} else if ("wallClockTime".equals(fieldName)) {
307307
wallClockTime = reader.getNullable(nonNullReader -> Duration.parse(nonNullReader.getString()));
308308
} else if ("readIOps".equals(fieldName)) {
309-
readIOps = reader.getLong();
309+
if (reader.currentToken() == JsonToken.STRING) {
310+
String readIOpsStr = reader.getString();
311+
try {
312+
readIOps = Long.parseLong(readIOpsStr);
313+
} catch (NumberFormatException e) {
314+
throw new IOException("Expected numeric readIOps, but found: " + readIOpsStr, e);
315+
}
316+
} else if (reader.currentToken() == JsonToken.NUMBER) {
317+
readIOps = reader.getLong();
318+
} else {
319+
throw new IOException("Expected readIOps to be a number or string, but found other type");
320+
}
310321
} else if ("writeIOps".equals(fieldName)) {
311-
writeIOps = reader.getLong();
322+
if (reader.currentToken() == JsonToken.STRING) {
323+
String writeIOpsStr = reader.getString();
324+
try {
325+
writeIOps = Long.parseLong(writeIOpsStr);
326+
} catch (NumberFormatException e) {
327+
throw new IOException("Expected numeric writeIOps, but found: " + writeIOpsStr, e);
328+
}
329+
} else if (reader.currentToken() == JsonToken.NUMBER) {
330+
writeIOps = reader.getLong();
331+
} else {
332+
throw new IOException("Expected writeIOps to be a number or string, but found other type");
333+
}
312334
} else if ("readIOGiB".equals(fieldName)) {
313335
readIOGiB = reader.getDouble();
314336
} else if ("writeIOGiB".equals(fieldName)) {

Diff for: sdk/batch/azure-compute-batch/src/main/java/com/azure/compute/batch/models/FileProperties.java

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
140140
* @throws IOException If an error occurs while reading the FileProperties.
141141
*/
142142
public static FileProperties fromJson(JsonReader jsonReader) throws IOException {
143+
// TODO: Re-add @Generated tag here and re-generate SDK once the 2024-05-01 Batch Service API is released
143144
return jsonReader.readObject(reader -> {
144145
OffsetDateTime lastModified = null;
145146
long contentLength = 0L;

Diff for: sdk/batch/azure-compute-batch/src/test/java/com/azure/compute/batch/FileTests.java

+29
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
import com.azure.core.http.rest.Response;
99
import com.azure.core.test.TestMode;
1010
import com.azure.core.util.BinaryData;
11+
import com.azure.json.JsonProviders;
12+
import com.azure.json.JsonReader;
1113
import org.junit.Assert;
1214
import org.junit.jupiter.api.Assertions;
1315
import org.junit.jupiter.api.Test;
1416

17+
import java.io.IOException;
18+
import java.io.StringReader;
1519
import java.nio.charset.StandardCharsets;
20+
import java.time.OffsetDateTime;
1621
import java.util.concurrent.TimeoutException;
1722

1823
public class FileTests extends BatchClientTestBase {
@@ -137,4 +142,28 @@ public void canReadFromNode() throws Exception {
137142
}
138143
}
139144
}
145+
146+
@Test
147+
public void testDeserializationOfFileProperties() throws IOException {
148+
String jsonResponse = "{"
149+
+ "\"lastModified\":\"2022-01-01T00:00:00Z\","
150+
+ "\"contentLength\":\"1024\","
151+
+ "\"creationTime\":\"2022-01-01T01:00:00Z\","
152+
+ "\"contentType\":\"application/json\","
153+
+ "\"fileMode\":\"rw-r--r--\""
154+
+ "}";
155+
156+
try (JsonReader jsonReader = JsonProviders.createReader(new StringReader(jsonResponse))) {
157+
FileProperties fileProperties = FileProperties.fromJson(jsonReader);
158+
159+
Assertions.assertNotNull(fileProperties);
160+
Assertions.assertEquals(OffsetDateTime.parse("2022-01-01T00:00:00Z"), fileProperties.getLastModified());
161+
Assertions.assertEquals(1024, fileProperties.getContentLength());
162+
Assertions.assertEquals(OffsetDateTime.parse("2022-01-01T01:00:00Z"), fileProperties.getCreationTime());
163+
Assertions.assertEquals("application/json", fileProperties.getContentType());
164+
Assertions.assertEquals("rw-r--r--", fileProperties.getFileMode());
165+
} catch (IOException e) {
166+
throw new RuntimeException(e);
167+
}
168+
}
140169
}

0 commit comments

Comments
 (0)