mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2026-12-01 23:07:51 +00:00
fix(translate): answer an empty text list with an empty list, not 404
`"text": []` names a list that happens to be empty, and the response can
render that perfectly well, so rejecting it bought nothing: it was the
original "No text to translate" condition surviving for the one shape the
code had no answer to. It is answered with `{"data": []}` now — the same
rule as every other text, applied to none of them.
A body that never named `text` is a different thing, a payload without its
parameter: it answers 400 Invalid request payload instead of the 404 that
stood in for it, which is also what the endpoint answers for a body it
cannot bind. That is the only refusal left.
Refs #244.
This commit is contained in:
+4
-13
@@ -495,16 +495,6 @@ func translationsFromResult(result gjson.Result, count int) ([]string, error) {
|
||||
// to the Pro endpoint; the value is sent verbatim as the Bearer token (i.e.
|
||||
// it must be an OAuth access token, not the legacy dl_session cookie).
|
||||
func TranslateByDLX(sourceLang, targetLang string, texts []string, tagHandling string, proxyURL string, dlSession string) (DLXTranslationResult, error) {
|
||||
// A request that names no text at all is not a translation request; any
|
||||
// text, blank or not, is answered. A missing or null `text` decodes to the
|
||||
// one empty string, so this is the empty array.
|
||||
if len(texts) == 0 {
|
||||
return DLXTranslationResult{
|
||||
Code: http.StatusNotFound,
|
||||
Message: "No text to translate",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// oneshot charges its anonymous cap against the total length of the
|
||||
// `text` array, and a batch is one upstream request, so the cap applies
|
||||
// to the sum of the texts that are actually sent.
|
||||
@@ -533,13 +523,14 @@ func TranslateByDLX(sourceLang, targetLang string, texts []string, tagHandling s
|
||||
}
|
||||
|
||||
// Nothing to translate: every text is blank, so none of them travels and
|
||||
// the request is answered with its own texts, exactly as a blank element
|
||||
// is inside a request that does have something to translate.
|
||||
// the request is answered with its own texts — an empty list in, an empty
|
||||
// list out — exactly as a blank element is inside a request that does have
|
||||
// something to translate.
|
||||
if len(positions) == 0 {
|
||||
return DLXTranslationResult{
|
||||
Code: http.StatusOK,
|
||||
ID: time.Now().UnixMilli(),
|
||||
Data: append([]string(nil), texts...),
|
||||
Data: mergeTranslations(texts, positions, nil),
|
||||
Alternatives: nil, // oneshot does not return alternatives
|
||||
SourceLang: sourceLang,
|
||||
TargetLang: targetLang,
|
||||
|
||||
@@ -118,8 +118,6 @@ func TestTranslateByDLXValidationFailsBeforeUpstream(t *testing.T) {
|
||||
texts []string
|
||||
code int
|
||||
}{
|
||||
{"no texts", nil, http.StatusNotFound},
|
||||
{"empty array", []string{}, http.StatusNotFound},
|
||||
{"over the limit", []string{strings.Repeat("a", maxFreeTextLength+1)}, http.StatusRequestEntityTooLarge},
|
||||
{"batch over the limit", []string{strings.Repeat("a", maxFreeTextLength), "b"}, http.StatusRequestEntityTooLarge},
|
||||
// 751 runes in the batch, but 1502 UTF-16 units: the case a rune count
|
||||
@@ -141,13 +139,16 @@ func TestTranslateByDLXValidationFailsBeforeUpstream(t *testing.T) {
|
||||
}
|
||||
|
||||
// Every text is answered — translated when it carries something, returned as it
|
||||
// is when it is blank — so only a request that names no text at all is an
|
||||
// error, and none of the cases below reaches DeepL.
|
||||
// is when it is blank — so nothing about emptiness is an error, and none of the
|
||||
// cases below reaches DeepL. Data is always allocated, so an empty request
|
||||
// marshals as `[]` rather than `null`.
|
||||
func TestTranslateByDLXAnswersBlankTextsWithThemselves(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
texts []string
|
||||
}{
|
||||
{"no texts", nil},
|
||||
{"empty array", []string{}},
|
||||
{"single empty", []string{""}},
|
||||
{"two empty", []string{"", ""}},
|
||||
{"whitespace only", []string{" \t "}},
|
||||
@@ -163,8 +164,16 @@ func TestTranslateByDLXAnswersBlankTextsWithThemselves(t *testing.T) {
|
||||
if result.Code != http.StatusOK {
|
||||
t.Fatalf("TranslateByDLX() code = %d, want %d", result.Code, http.StatusOK)
|
||||
}
|
||||
if !reflect.DeepEqual(result.Data, tt.texts) {
|
||||
t.Fatalf("TranslateByDLX() data = %q, want %q", result.Data, tt.texts)
|
||||
if result.Data == nil {
|
||||
t.Fatal("TranslateByDLX() data = nil, want an allocated slice so it marshals as []")
|
||||
}
|
||||
if len(result.Data) != len(tt.texts) {
|
||||
t.Fatalf("TranslateByDLX() data = %q, want %d element(s)", result.Data, len(tt.texts))
|
||||
}
|
||||
for i, text := range tt.texts {
|
||||
if result.Data[i] != text {
|
||||
t.Fatalf("TranslateByDLX() data[%d] = %q, want %q", i, result.Data[i], text)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user