Skip to content

Commit 4f9e1b3

Browse files
fix(boxsdkgen): Fix parsing OffsetDateTime from String (box/box-codegen#887) (#1581)
1 parent 7c360f0 commit 4f9e1b3

File tree

3 files changed

+18
-33
lines changed

3 files changed

+18
-33
lines changed

.codegen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "engineHash": "a17cbb6", "specHash": "e7ce024", "version": "5.0.0" }
1+
{ "engineHash": "35c5d34", "specHash": "e7ce024", "version": "5.0.0" }

src/intTest/java/com/box/sdkgen/aistudio/AiStudioITest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ public void testUseAiAgentReferenceInAiAsk() {
8282
.createAiAsk(
8383
new AiAsk.Builder(
8484
AiAskModeField.SINGLE_ITEM_QA,
85-
"which direction sun rises",
85+
"Which direction does the Sun rise?",
8686
Arrays.asList(
8787
new AiItemAsk.Builder(fileToAsk.getId(), AiItemAskTypeField.FILE)
88-
.content("Sun rises in the East")
88+
.content("The Sun rises in the east.")
8989
.build()))
9090
.aiAgent(new AiAgentReference.Builder().id(createdAgent.getId()).build())
9191
.build());
92-
assert response.getAnswer().contains("East");
92+
assert response.getAnswer().contains("east");
9393
assert response.getCompletionReason().equals("done");
9494
assert response.getAiAgentInfo().getModels().size() > 0;
9595
client.getFiles().deleteFileById(fileToAsk.getId());

src/main/java/com/box/sdkgen/internal/utils/UtilsManager.java

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import java.time.OffsetDateTime;
2424
import java.time.ZoneOffset;
2525
import java.time.format.DateTimeFormatter;
26+
import java.time.format.DateTimeFormatterBuilder;
2627
import java.time.format.DateTimeParseException;
28+
import java.time.temporal.ChronoField;
2729
import java.util.Arrays;
2830
import java.util.Base64;
2931
import java.util.HashMap;
@@ -47,11 +49,15 @@ public class UtilsManager {
4749
private static final int BUFFER_SIZE = 8192;
4850

4951
private static final DateTimeFormatter OFFSET_DATE_TIME_FORMAT =
50-
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
52+
new DateTimeFormatterBuilder()
53+
.appendPattern("yyyy-MM-dd'T'HH:mm:ss")
54+
.optionalStart()
55+
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true)
56+
.optionalEnd()
57+
.appendOffsetId()
58+
.toFormatter();
5159
private static final DateTimeFormatter OFFSET_DATE_TIME_FORMAT_WITH_MILLIS =
5260
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
53-
private static final DateTimeFormatter OFFSET_DATE_TIME_FORMAT_WITH_MICROS =
54-
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSX");
5561
private static final DateTimeFormatter OFFSET_DATE_FORMAT =
5662
DateTimeFormatter.ofPattern("yyyy-MM-dd");
5763

@@ -381,20 +387,11 @@ public static Map<String, String> sanitizeMap(
381387
}
382388

383389
public static OffsetDateTime dateTimeFromString(String dateString) {
384-
DateTimeFormatter[] formatters = {
385-
OFFSET_DATE_TIME_FORMAT,
386-
OFFSET_DATE_TIME_FORMAT_WITH_MILLIS,
387-
OFFSET_DATE_TIME_FORMAT_WITH_MICROS
388-
};
389-
390-
for (DateTimeFormatter formatter : formatters) {
391-
try {
392-
return OffsetDateTime.parse(dateString, formatter);
393-
} catch (DateTimeParseException e) {
394-
// Ignore and try the next format
395-
}
390+
try {
391+
return OffsetDateTime.parse(dateString, OFFSET_DATE_TIME_FORMAT);
392+
} catch (DateTimeParseException e) {
393+
return null;
396394
}
397-
return null;
398395
}
399396

400397
public static String dateTimeToString(OffsetDateTime dateTime) {
@@ -408,19 +405,7 @@ public static OffsetDateTime dateFromString(String dateString) {
408405
return OffsetDateTime.parse(dateString + "T00:00:00Z");
409406
}
410407
// Otherwise try to parse as full OffsetDateTime
411-
DateTimeFormatter[] formatters = {
412-
OFFSET_DATE_TIME_FORMAT,
413-
OFFSET_DATE_TIME_FORMAT_WITH_MILLIS,
414-
OFFSET_DATE_TIME_FORMAT_WITH_MICROS
415-
};
416-
for (DateTimeFormatter formatter : formatters) {
417-
try {
418-
return OffsetDateTime.parse(dateString, formatter);
419-
} catch (DateTimeParseException e) {
420-
// Ignore and try the next format
421-
}
422-
}
423-
return null;
408+
return dateTimeFromString(dateString);
424409
} catch (DateTimeParseException e) {
425410
return null;
426411
}

0 commit comments

Comments
 (0)