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:
JounQin
2026-10-10 23:44:37 +08:00
parent 4fb2759d48
commit b28dcd4958
5 changed files with 73 additions and 20 deletions
+11
View File
@@ -159,6 +159,17 @@ func Router(cfg *Config) *gin.Engine {
return
}
// A body without `text` never named a text to translate, which is a
// payload error. `"text": []` names a list that happens to be empty,
// and is answered with an empty list, so it is not caught here.
if !req.Text.Batch && len(req.Text.Texts) == 0 {
c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"message": "Invalid request payload",
})
return
}
sourceLang := req.SourceLang
targetLang := req.TargetLang
texts := req.Text.Texts