Skip to content

Commit 193542c

Browse files
hive-botclaude
andcommitted
fix: trim Spoken Terms entries before validating and sending them
Each term was validated on a trimmed copy but sent untrimmed, so a term with leading or trailing whitespace reached the API, which rejects it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent bc8bace commit 193542c

4 files changed

Lines changed: 36 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6868
`replaceSpokenTermsList()` take optional `CreateSpokenTermsOptions` and
6969
`ReplaceSpokenTermsListOptions`. All three options classes support
7070
`setExtraBodyParameters()`. Collection IDs are URL-encoded in the request
71-
path. Terms are passed and returned as a list of strings and are checked
72-
like glossary terms, so a term containing a newline throws an
73-
`IllegalArgumentException`.
71+
path. Terms are passed and returned as a list of strings; each term is
72+
trimmed of leading and trailing whitespace and then checked like a glossary
73+
term, so a term containing a newline throws an `IllegalArgumentException`.
7474
- Added the `DeepLAdminClient` class for the Admin API. It must be created with
7575
an admin API key and supports managing developer keys
7676
(`createDeveloperKey()`, `getDeveloperKeys()`, `deactivateDeveloperKey()`,

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,9 @@ collections can be stored with your account, each with a user-specified name
748748
and a uniquely-assigned ID.
749749

750750
Each method that takes a collection accepts either its ID or a `SpokenTerms`
751-
object. Terms are checked like glossary terms before any request is sent: a
752-
term that is empty or contains a control character such as a newline throws an
751+
object. Each term is trimmed of leading and trailing whitespace and then
752+
checked like a glossary term before any request is sent: a term that is empty
753+
or contains a control character such as a newline throws an
753754
`IllegalArgumentException`. Other invalid term lists are rejected by the API
754755
with a `DeepLException`.
755756

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,18 +1975,34 @@ private static String langQueryString(String lang) {
19751975

19761976
/**
19771977
* Builds the JSON object of one term list; the API expects the terms newline-separated, so each
1978-
* term is checked like a glossary term first: a term containing a newline would split.
1978+
* term is trimmed and then checked like a glossary term: a term containing a newline would split.
19791979
*/
19801980
private static Map<String, Object> createSpokenTermsListData(SpokenTermsEntries entries) {
1981+
List<String> terms = new ArrayList<>();
19811982
for (String term : entries.getEntries()) {
1982-
GlossaryEntries.validateGlossaryTerm(term);
1983+
String trimmed = trimWhitespace(term);
1984+
GlossaryEntries.validateGlossaryTerm(trimmed);
1985+
terms.add(trimmed);
19831986
}
19841987
Map<String, Object> data = new LinkedHashMap<>();
19851988
data.put("lang", entries.getLang());
1986-
data.put("entries", String.join("\n", entries.getEntries()));
1989+
data.put("entries", String.join("\n", terms));
19871990
return data;
19881991
}
19891992

1993+
/** Removes leading and trailing whitespace as defined by {@link Character#isWhitespace}. */
1994+
private static String trimWhitespace(String term) {
1995+
int start = 0;
1996+
int end = term.length();
1997+
while (start < end && Character.isWhitespace(term.charAt(start))) {
1998+
start++;
1999+
}
2000+
while (end > start && Character.isWhitespace(term.charAt(end - 1))) {
2001+
end--;
2002+
}
2003+
return term.substring(start, end);
2004+
}
2005+
19902006
private static List<Map<String, Object>> createSpokenTermsListsData(
19912007
List<SpokenTermsEntries> termLists) {
19922008
List<Map<String, Object>> data = new ArrayList<>();

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ void urlEncodesTheIdInEveryPath() throws Exception {
128128
client.deleteSpokenTerms(id);
129129
}
130130

131+
@Test
132+
void trimsEachTermBeforeSendingIt() throws Exception {
133+
wm.stubFor(
134+
put(urlEqualTo("/v3/spoken-terms/st1/term-lists"))
135+
.withRequestBody(equalToJson("{\"lang\":\"en\",\"entries\":\"foo\\nbar baz\"}"))
136+
.willReturn(okJson(TERM_LIST)));
137+
138+
client.replaceSpokenTermsList(
139+
"st1", new SpokenTermsEntries("en", Arrays.asList(" foo", "bar baz\t")));
140+
}
141+
131142
@Test
132143
void joinsEntriesWithNewlinesWhenSendingAndSplitsThemWhenReading() throws Exception {
133144
wm.stubFor(

0 commit comments

Comments
 (0)