Skip to content

Commit ebc4683

Browse files
hive-botclaude
andcommitted
fix: raise DeepLException for a style profile without its timestamps
creation_time and updated_time are required in the API response, but a missing or malformed value was parsed as null. StyleProfile.getCreationTime() and getUpdatedTime() are now never null, and such a response raises a DeepLException. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ad9356e commit ebc4683

5 files changed

Lines changed: 51 additions & 20 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6161
`setTranslationMemoryIds()` take a list of IDs or the `IGlossary`,
6262
`StyleRuleInfo` or `TranslationMemoryInfo` objects. Options left `null` are
6363
not sent; an empty string clears the description or industry and an empty
64-
list unlinks all components of that kind.
64+
list unlinks all components of that kind. `StyleProfile.getCreationTime()`
65+
and `getUpdatedTime()` are never `null`; a response without a valid
66+
`creation_time` or `updated_time` raises a `DeepLException`.
6567
- Added Spoken Terms support to `DeepLClient` (`/v3/spoken-terms`):
6668
`createSpokenTerms()`, `listSpokenTerms()`, `getSpokenTerms()`,
6769
`updateSpokenTerms()`, `getSpokenTermsEntries()`, `replaceSpokenTermsList()`,

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ class Example { // Continuing class Example from above
10491049
optional parameters `page` (page number, 0-indexed, default 0) and `pageSize`
10501050
(items per page, default 10; the API reduces values above 100 to 100). Optional
10511051
fields that a style profile does not have, such as the description and industry,
1052-
are `null`.
1052+
are `null`; the creation and update times are always set.
10531053

10541054
```java
10551055
class Example { // Continuing class Example from above

‎deepl-java/src/main/java/com/deepl/api/StyleProfile.java‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public class StyleProfile {
3939
private final boolean glossaryPriorityOrderEnabled;
4040

4141
@SerializedName(value = "creation_time")
42-
private final @Nullable Date creationTime;
42+
private final Date creationTime;
4343

4444
@SerializedName(value = "updated_time")
45-
private final @Nullable Date updatedTime;
45+
private final Date updatedTime;
4646

4747
/**
4848
* Initializes a new {@link StyleProfile} object.
@@ -55,8 +55,8 @@ public class StyleProfile {
5555
* @param styleRuleIds IDs of the linked style rules.
5656
* @param translationMemoryIds IDs of the linked translation memories.
5757
* @param glossaryPriorityOrderEnabled Whether the linked glossaries are applied in list order.
58-
* @param creationTime Time the style profile was created, or <code>null</code>.
59-
* @param updatedTime Time the style profile was last updated, or <code>null</code>.
58+
* @param creationTime Time the style profile was created.
59+
* @param updatedTime Time the style profile was last updated.
6060
*/
6161
public StyleProfile(
6262
String styleProfileId,
@@ -67,8 +67,8 @@ public StyleProfile(
6767
@Nullable List<String> styleRuleIds,
6868
@Nullable List<String> translationMemoryIds,
6969
boolean glossaryPriorityOrderEnabled,
70-
@Nullable Date creationTime,
71-
@Nullable Date updatedTime) {
70+
Date creationTime,
71+
Date updatedTime) {
7272
this.styleProfileId = styleProfileId;
7373
this.name = name;
7474
this.description = description;
@@ -123,13 +123,13 @@ public boolean isGlossaryPriorityOrderEnabled() {
123123
return glossaryPriorityOrderEnabled;
124124
}
125125

126-
/** @return Time the style profile was created, or <code>null</code> if unavailable. */
127-
public @Nullable Date getCreationTime() {
126+
/** @return Time the style profile was created. */
127+
public Date getCreationTime() {
128128
return creationTime;
129129
}
130130

131-
/** @return Time the style profile was last updated, or <code>null</code> if unavailable. */
132-
public @Nullable Date getUpdatedTime() {
131+
/** @return Time the style profile was last updated. */
132+
public Date getUpdatedTime() {
133133
return updatedTime;
134134
}
135135

‎deepl-java/src/main/java/com/deepl/api/parsing/StyleProfileDeserializer.java‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import java.util.*;
1010

1111
/**
12-
* Parses style profiles tolerantly: a malformed timestamp or ID list entry does not fail the call.
12+
* Parses style profiles. The required timestamps must be present and valid; a malformed ID list
13+
* entry is skipped.
1314
*
1415
* <p>This class is internal; you should not use this class directly.
1516
*/
@@ -36,8 +37,17 @@ public StyleProfile deserialize(
3637
getStringList(jsonObject, "style_rule_ids"),
3738
getStringList(jsonObject, "translation_memory_ids"),
3839
glossaryPriorityOrderEnabled,
39-
Parser.getAsDateOrNull(jsonObject, "creation_time", context),
40-
Parser.getAsDateOrNull(jsonObject, "updated_time", context));
40+
getRequiredDate(jsonObject, "creation_time", context),
41+
getRequiredDate(jsonObject, "updated_time", context));
42+
}
43+
44+
private static Date getRequiredDate(
45+
JsonObject jsonObject, String parameterName, JsonDeserializationContext context) {
46+
Date date = Parser.getAsDateOrNull(jsonObject, parameterName, context);
47+
if (date == null) {
48+
throw new JsonParseException("Style profile response is missing a valid " + parameterName);
49+
}
50+
return date;
4151
}
4252

4353
private static List<String> getStringList(JsonObject jsonObject, String parameterName) {

‎deepl-java/src/offlineTest/java/com/deepl/api/StyleProfilesTest.java‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
1717
import static org.junit.jupiter.api.Assertions.assertEquals;
1818
import static org.junit.jupiter.api.Assertions.assertFalse;
19-
import static org.junit.jupiter.api.Assertions.assertNotNull;
2019
import static org.junit.jupiter.api.Assertions.assertNull;
2120
import static org.junit.jupiter.api.Assertions.assertThrows;
2221
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -293,22 +292,20 @@ void parsesAProfileWithoutOptionalFieldsAndIgnoresUnknownFields() throws Excepti
293292
}
294293

295294
@Test
296-
void toleratesAMalformedTimestampAndNonStringIds() throws Exception {
295+
void toleratesNonStringIds() throws Exception {
297296
wm.stubFor(
298297
get(urlEqualTo("/v3/style_profiles/" + ID))
299298
.willReturn(
300299
okJson(
301300
"{\"style_profile_id\":\""
302301
+ ID
303302
+ "\",\"name\":\"n\","
304-
+ "\"creation_time\":\"not a timestamp\","
303+
+ "\"creation_time\":\"2026-04-01T16:34:25.223Z\","
305304
+ "\"updated_time\":\"2026-08-06T09:12:44.108Z\","
306305
+ "\"glossary_ids\":[\"g1\",42,null,{}],\"style_rule_ids\":null}")));
307306

308307
StyleProfile profile = client.getStyleProfile(ID);
309308

310-
assertNull(profile.getCreationTime());
311-
assertNotNull(profile.getUpdatedTime());
312309
assertEquals(ID, profile.getStyleProfileId());
313310
assertEquals("n", profile.getName());
314311
assertEquals(Collections.singletonList("g1"), profile.getGlossaryIds());
@@ -317,6 +314,28 @@ void toleratesAMalformedTimestampAndNonStringIds() throws Exception {
317314
assertFalse(profile.isGlossaryPriorityOrderEnabled());
318315
}
319316

317+
@Test
318+
void throwsDeepLExceptionWhenATimestampIsMissingOrMalformed() {
319+
String head = "{\"style_profile_id\":\"" + ID + "\",\"name\":\"n\",";
320+
wm.stubFor(
321+
get(urlEqualTo("/v3/style_profiles/missing"))
322+
.willReturn(okJson(head + "\"updated_time\":\"2026-08-06T09:12:44.108Z\"}")));
323+
wm.stubFor(
324+
get(urlEqualTo("/v3/style_profiles/malformed"))
325+
.willReturn(
326+
okJson(
327+
head
328+
+ "\"creation_time\":\"2026-04-01T16:34:25.223Z\","
329+
+ "\"updated_time\":\"not a timestamp\"}")));
330+
331+
DeepLException missing =
332+
assertThrows(DeepLException.class, () -> client.getStyleProfile("missing"));
333+
assertTrue(missing.getMessage().contains("creation_time"));
334+
DeepLException malformed =
335+
assertThrows(DeepLException.class, () -> client.getStyleProfile("malformed"));
336+
assertTrue(malformed.getMessage().contains("updated_time"));
337+
}
338+
320339
@Test
321340
void throwsNotFoundExceptionOn404() {
322341
wm.stubFor(

0 commit comments

Comments
 (0)