Skip to content

Commit bc8bace

Browse files
hive-botclaude
andcommitted
fix: URL-encode the Spoken Terms collection ID in request paths
The ID was inserted into /v3/spoken-terms/{id} paths unescaped, so an ID containing a reserved character such as "/" addressed a different path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ff54764 commit bc8bace

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6767
`setTermLists()`) and sends only the fields set; `createSpokenTerms()` and
6868
`replaceSpokenTermsList()` take optional `CreateSpokenTermsOptions` and
6969
`ReplaceSpokenTermsListOptions`. All three options classes support
70-
`setExtraBodyParameters()`. Terms are passed and returned as a list of strings and are checked
70+
`setExtraBodyParameters()`. Collection IDs are URL-encoded in the request
71+
path. Terms are passed and returned as a list of strings and are checked
7172
like glossary terms, so a term containing a newline throws an
7273
`IllegalArgumentException`.
7374
- Added the `DeepLAdminClient` class for the Admin API. It must be created with

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,10 +1913,14 @@ private static String spokenTermsId(SpokenTerms spokenTerms) throws IllegalArgum
19131913
return spokenTerms.getSpokenTermsId();
19141914
}
19151915

1916-
/** Validates the Spoken Terms collection ID and builds its URL path. */
1916+
/** Validates the Spoken Terms collection ID and builds its URL path with the ID escaped. */
19171917
private String spokenTermsPath(String spokenTermsId) throws DeepLException {
19181918
validateParameter("spokenTermsId", spokenTermsId);
1919-
return "/v3/spoken-terms/" + spokenTermsId;
1919+
try {
1920+
return "/v3/spoken-terms/" + ApiClientHelper.encodeQueryComponent(spokenTermsId);
1921+
} catch (UnsupportedEncodingException exception) {
1922+
throw new DeepLException("Error while URL-encoding request", exception);
1923+
}
19201924
}
19211925

19221926
/** Serializes a Spoken Terms request body, adding the extra body parameters last. */

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor;
77
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
8+
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
89
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
910
import static com.github.tomakehurst.wiremock.client.WireMock.get;
11+
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
1012
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
1113
import static com.github.tomakehurst.wiremock.client.WireMock.patch;
1214
import static com.github.tomakehurst.wiremock.client.WireMock.post;
@@ -105,6 +107,27 @@ void replaceListSendsTheExtraBodyParametersLast() throws Exception {
105107
new ReplaceSpokenTermsListOptions().setExtraBodyParameters(extra));
106108
}
107109

110+
@Test
111+
void urlEncodesTheIdInEveryPath() throws Exception {
112+
String id = "a/b c";
113+
String path = "/v3/spoken-terms/a%2Fb%20c";
114+
wm.stubFor(get(urlEqualTo(path)).willReturn(okJson(SPOKEN_TERMS)));
115+
wm.stubFor(patch(urlEqualTo(path)).willReturn(okJson(SPOKEN_TERMS)));
116+
wm.stubFor(delete(urlEqualTo(path)).willReturn(ok()));
117+
wm.stubFor(
118+
get(urlEqualTo(path + "/entries?lang=en"))
119+
.willReturn(okJson("{\"lang\":\"en\",\"entries\":\"a\"}")));
120+
wm.stubFor(put(urlEqualTo(path + "/term-lists")).willReturn(okJson(TERM_LIST)));
121+
wm.stubFor(delete(urlEqualTo(path + "/term-lists?lang=en")).willReturn(ok()));
122+
123+
client.getSpokenTerms(id);
124+
client.updateSpokenTerms(id, new UpdateSpokenTermsOptions().setName("n"));
125+
client.getSpokenTermsEntries(id, "en");
126+
client.replaceSpokenTermsList(id, new SpokenTermsEntries("en", Collections.singletonList("a")));
127+
client.deleteSpokenTermsList(id, "en");
128+
client.deleteSpokenTerms(id);
129+
}
130+
108131
@Test
109132
void joinsEntriesWithNewlinesWhenSendingAndSplitsThemWhenReading() throws Exception {
110133
wm.stubFor(

0 commit comments

Comments
 (0)