Skip to content

Commit 6320a5f

Browse files
hive-botclaude
andcommitted
fix: return null for absent developer key labels and usage limits
DeveloperKey.getLabel() and KeyUsage.getApiKeyLabel() returned "" when the API sent no label, and DeveloperKey.getUsageLimits() returned a DeveloperKeyUsageLimits with all limits null (unlimited) when the API sent no usage limits. They now return null. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 898f2e5 commit 6320a5f

6 files changed

Lines changed: 68 additions & 26 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9797
The analytics functions take `LocalDate`, `"YYYY-MM-DD"` strings, or `Date`
9898
and `Instant` values, of which the UTC day is sent; both the start and the
9999
end day are included. Report dates are `Date` values at midnight UTC.
100+
`DeveloperKey.getLabel()`, `DeveloperKey.getUsageLimits()` and
101+
`KeyUsage.getApiKeyLabel()` return `null` when the API sends no value.
100102
`setDeveloperKeyUsageLimits()` takes a `DeveloperKeyUsageLimitsUpdate` and
101103
changes only the limits set in it, including the speech-to-speech limit.
102104
- Added the API Pro fields of `GET /v2/usage` to `Usage`:

‎README.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,9 @@ class AdminExample { // Continuing class AdminExample from above
15401540
`deactivateDeveloperKey()`, `renameDeveloperKey()` and
15411541
`setDeveloperKeyUsageLimits()` accept either a key ID or a `DeveloperKey`
15421542
object and return the updated `DeveloperKey`. The API validates labels and
1543-
limit values.
1543+
limit values. `DeveloperKey.getLabel()` and `getUsageLimits()` return `null`
1544+
if the API sent no label or no usage limits; within `DeveloperKeyUsageLimits`
1545+
a `null` limit means unlimited.
15441546

15451547
`createDeveloperKey()` takes a `CreateDeveloperKeyOptions` (`setLabel()`), and
15461548
`deactivateDeveloperKey()`, `renameDeveloperKey()` and
@@ -1566,7 +1568,8 @@ Pass a `groupBy` argument to break the usage down by key
15661568
(`AdminUsageGroupBy.Key`, see `getKeyUsages()`) or by key and day
15671569
(`AdminUsageGroupBy.KeyAndDay`, see `getKeyAndDayUsages()`); a string is sent
15681570
as given, for values not yet in the enum. These lists are
1569-
empty if the report uses a different grouping.
1571+
empty if the report uses a different grouping. `KeyUsage.getApiKeyLabel()` is
1572+
`null` if the API sent no label.
15701573
`getGroupBy()` returns the grouping as the API sends it, for example `"key"` or
15711574
`"keyAndDay"`. Each usage breakdown has the characters used in total and per
15721575
product, `getSpeechToTextMinutes()` and `getSpeechToSpeechMinutes()`.

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class DeveloperKey {
1313
private final String keyId;
1414

1515
@SerializedName(value = "label")
16-
private final String label;
16+
private final @Nullable String label;
1717

1818
@SerializedName(value = "creation_time")
1919
private final @Nullable Date creationTime;
@@ -31,15 +31,16 @@ public class DeveloperKey {
3131
* Initializes a new {@link DeveloperKey} containing information about a developer key.
3232
*
3333
* @param keyId Unique ID of the key.
34-
* @param label Label assigned to the key.
34+
* @param label Label assigned to the key, or <code>null</code> if the API sent none.
3535
* @param creationTime Timestamp when the key was created.
3636
* @param deactivatedTime Timestamp when the key was deactivated, or <code>null</code> if active.
3737
* @param isDeactivated True if the key is deactivated.
38-
* @param usageLimits Usage limits configured for the key.
38+
* @param usageLimits Usage limits configured for the key, or <code>null</code> if the API sent
39+
* none.
3940
*/
4041
public DeveloperKey(
4142
String keyId,
42-
String label,
43+
@Nullable String label,
4344
@Nullable Date creationTime,
4445
@Nullable Date deactivatedTime,
4546
boolean isDeactivated,
@@ -57,8 +58,8 @@ public String getKeyId() {
5758
return keyId;
5859
}
5960

60-
/** @return Label assigned to the key. */
61-
public String getLabel() {
61+
/** @return Label assigned to the key, or <code>null</code> if the API sent none. */
62+
public @Nullable String getLabel() {
6263
return label;
6364
}
6465

@@ -77,9 +78,12 @@ public boolean isDeactivated() {
7778
return isDeactivated;
7879
}
7980

80-
/** @return Usage limits configured for the key; a <code>null</code> limit means unlimited. */
81-
public DeveloperKeyUsageLimits getUsageLimits() {
82-
return usageLimits != null ? usageLimits : new DeveloperKeyUsageLimits(null, null, null);
81+
/**
82+
* @return Usage limits configured for the key, in which a <code>null</code> limit means
83+
* unlimited; or <code>null</code> if the API sent no usage limits.
84+
*/
85+
public @Nullable DeveloperKeyUsageLimits getUsageLimits() {
86+
return usageLimits;
8387
}
8488

8589
@Override

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class KeyUsage {
1313
private final String apiKey;
1414

1515
@SerializedName(value = "api_key_label")
16-
private final String apiKeyLabel;
16+
private final @Nullable String apiKeyLabel;
1717

1818
@SerializedName(value = "usage")
1919
private final AdminUsageBreakdown usage;
@@ -25,12 +25,15 @@ public class KeyUsage {
2525
* Initializes a new {@link KeyUsage}.
2626
*
2727
* @param apiKey Masked API key.
28-
* @param apiKeyLabel Label of the API key.
28+
* @param apiKeyLabel Label of the API key, or <code>null</code> if the API sent none.
2929
* @param usage Usage of the API key.
3030
* @param usageDate Day of the usage as midnight UTC, present when grouped by key and day.
3131
*/
3232
public KeyUsage(
33-
String apiKey, String apiKeyLabel, AdminUsageBreakdown usage, @Nullable Date usageDate) {
33+
String apiKey,
34+
@Nullable String apiKeyLabel,
35+
AdminUsageBreakdown usage,
36+
@Nullable Date usageDate) {
3437
this.apiKey = apiKey;
3538
this.apiKeyLabel = apiKeyLabel;
3639
this.usage = usage;
@@ -42,8 +45,8 @@ public String getApiKey() {
4245
return apiKey;
4346
}
4447

45-
/** @return Label of the API key. */
46-
public String getApiKeyLabel() {
48+
/** @return Label of the API key, or <code>null</code> if the API sent none. */
49+
public @Nullable String getApiKeyLabel() {
4750
return apiKeyLabel;
4851
}
4952

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
/**
1414
* Parses Admin API responses tolerantly: entries that are not objects or have no key_id are
15-
* skipped, and missing or malformed fields get neutral defaults. A response of the wrong overall
16-
* shape, or a single key without key_id, is an error.
15+
* skipped, missing optional fields (labels, usage limits, dates) are null, and other missing or
16+
* malformed fields get neutral defaults. A response of the wrong overall shape, or a single key
17+
* without key_id, is an error.
1718
*
1819
* <p>This class is internal; you should not use this class directly.
1920
*/
@@ -32,17 +33,23 @@ DeveloperKey parseDeveloperKey(@Nullable JsonElement json) {
3233
}
3334

3435
private DeveloperKey parseKey(JsonObject object) {
35-
JsonObject limits = asObject(object.get("usage_limits"));
36+
JsonElement limitsElement = object.get("usage_limits");
37+
JsonObject limits =
38+
limitsElement != null && limitsElement.isJsonObject()
39+
? limitsElement.getAsJsonObject()
40+
: null;
3641
return new DeveloperKey(
3742
string(object, "key_id"),
38-
string(object, "label"),
43+
stringOrNull(object, "label"),
3944
date(object, "creation_time"),
4045
date(object, "deactivated_time"),
4146
bool(object, "is_deactivated"),
42-
new DeveloperKeyUsageLimits(
43-
longOrNull(limits, "characters"),
44-
longOrNull(limits, "speech_to_text_milliseconds"),
45-
longOrNull(limits, "speech_to_speech_milliseconds")));
47+
limits == null
48+
? null
49+
: new DeveloperKeyUsageLimits(
50+
longOrNull(limits, "characters"),
51+
longOrNull(limits, "speech_to_text_milliseconds"),
52+
longOrNull(limits, "speech_to_speech_milliseconds")));
4653
}
4754

4855
List<DeveloperKey> parseDeveloperKeys(@Nullable JsonElement json) {
@@ -113,7 +120,7 @@ private List<KeyUsage> parseKeyUsages(@Nullable JsonElement json) {
113120
usages.add(
114121
new KeyUsage(
115122
string(entry, "api_key"),
116-
string(entry, "api_key_label"),
123+
stringOrNull(entry, "api_key_label"),
117124
parseBreakdown(entry.get("usage")),
118125
utcDay(entry, "usage_date")));
119126
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void parsesMalformedValuesTolerantly() throws Exception {
113113
assertEquals("k", keys.get(0).getKeyId());
114114
assertNull(keys.get(0).getCreationTime());
115115
assertTrue(keys.get(0).isDeactivated());
116-
assertNull(keys.get(0).getUsageLimits().getCharacters());
116+
assertNull(keys.get(0).getUsageLimits());
117117

118118
LocalDate start = LocalDate.of(2025, 9, 29);
119119
LocalDate end = LocalDate.of(2025, 10, 1);
@@ -590,6 +590,29 @@ void sendsTheUtcDayOfDateAndInstantArguments() throws Exception {
590590
IllegalArgumentException.class, () -> client.getUsageAnalytics((Date) null, new Date()));
591591
}
592592

593+
@Test
594+
void returnsNullForAbsentLabelsAndUsageLimits() throws Exception {
595+
wm.stubFor(
596+
get("/v2/admin/developer-keys")
597+
.willReturn(okJson("[{\"key_id\":\"k\",\"is_deactivated\":false}]")));
598+
wm.stubFor(
599+
get(urlPathEqualTo("/v2/admin/analytics"))
600+
.willReturn(
601+
okJson(
602+
"{\"usage_report\":{\"key_usages\":[{\"api_key\":\"a\","
603+
+ "\"usage\":{\"total_characters\":1}}]}}")));
604+
605+
DeveloperKey key = client.listDeveloperKeys().get(0);
606+
assertNull(key.getLabel());
607+
assertNull(key.getUsageLimits());
608+
KeyUsage usage =
609+
client
610+
.getUsageAnalytics(LocalDate.of(2026, 10, 19), LocalDate.of(2026, 10, 21))
611+
.getKeyUsages()
612+
.get(0);
613+
assertNull(usage.getApiKeyLabel());
614+
}
615+
593616
@Test
594617
void parsesReportDatesAsUtcMidnight() throws Exception {
595618
wm.stubFor(

0 commit comments

Comments
 (0)