fix(service): treat a null text as no text, and read that from the field

UnmarshalJSON sent `null` down the string branch, where json.Unmarshal into a
string is a documented no-op, so `{"text":null}` decoded to `[""]` and was
indistinguishable from `{"text":""}` for the rest of the request. On main both
were an error, but this branch answers an empty text with itself, so null
quietly became a 200 instead.

A JSON null is a present key with no value, so it leaves the zero value now —
the same Texts == nil a body with no `text` key has — and the handler refuses
both with 400 Invalid request payload, testing the field directly instead of
re-deriving the decoder with `!Batch && len(Texts) == 0`. Every shape that
names text, including `"text": []`, still decodes to a non-nil Texts and is
answered rather than refused.

Refs #244.
This commit is contained in:
JounQin
2026-10-11 00:40:57 +08:00
parent b28dcd4958
commit ee1114252e
3 changed files with 12 additions and 7 deletions
+1 -1
View File
@@ -90,7 +90,7 @@ curl -X POST http://localhost:1188/translate \
Batch limits and failure modes: Batch limits and failure modes:
- The anonymous limit is **1500 characters for the whole request**, counted in UTF-16 code units — what JavaScript's `String.prototype.length` reports, so a CJK character costs 1 and an astral one (emoji, historic scripts) costs 2 — across every text in the array, not 1500 per text. A batch over that total is rejected with `413` and `text exceeds maximum length: N characters (anonymous oneshot limit is 1500)`, so a large batch has to be chunked by total length rather than by segment count. There is no separate cap on the number of texts. - The anonymous limit is **1500 characters for the whole request**, counted in UTF-16 code units — what JavaScript's `String.prototype.length` reports, so a CJK character costs 1 and an astral one (emoji, historic scripts) costs 2 — across every text in the array, not 1500 per text. A batch over that total is rejected with `413` and `text exceeds maximum length: N characters (anonymous oneshot limit is 1500)`, so a large batch has to be chunked by total length rather than by segment count. There is no separate cap on the number of texts.
- A blank text — empty, or nothing but whitespace — is answered with itself and never sent, so a blank line in a segmented document keeps its place instead of failing the request. `"text": []` is answered with `{"data": []}`; a body with no `text` field at all is rejected with `400 {"code":400,"message":"Invalid request payload"}`. - A blank text — empty, or nothing but whitespace — is answered with itself and never sent, so a blank line in a segmented document keeps its place instead of failing the request. `"text": []` is answered with `{"data": []}`; a body with no `text` field at all, or `"text": null`, is rejected with `400 {"code":400,"message":"Invalid request payload"}`.
- A batch is all-or-nothing: if DeepL returns a different number of translations or leaves one of them empty, the request fails with `503 {"code":503,"message":"Translation failed"}` instead of answering with placeholders or a shorter, misaligned array. - A batch is all-or-nothing: if DeepL returns a different number of translations or leaves one of them empty, the request fails with `503 {"code":503,"message":"Translation failed"}` instead of answering with placeholders or a shorter, misaligned array.
## Discussion Group ## Discussion Group
+9 -4
View File
@@ -91,8 +91,7 @@ type PayloadTranslate struct {
// Texts, and Batch records which shape the caller used so the response can // Texts, and Batch records which shape the caller used so the response can
// mirror it. Any other shape (a number, an object, an array holding // mirror it. Any other shape (a number, an object, an array holding
// non-strings) fails the payload decode, which the handler reports as // non-strings) fails the payload decode, which the handler reports as
// 400 Invalid request payload; null decodes to the empty string exactly as it // 400 Invalid request payload; null names no text, like a body without the key.
// did when text was a plain string.
type PayloadText struct { type PayloadText struct {
Texts []string Texts []string
Batch bool Batch bool
@@ -100,7 +99,8 @@ type PayloadText struct {
// UnmarshalJSON implements json.Unmarshaler. // UnmarshalJSON implements json.Unmarshaler.
func (p *PayloadText) UnmarshalJSON(data []byte) error { func (p *PayloadText) UnmarshalJSON(data []byte) error {
if trimmed := bytes.TrimSpace(data); len(trimmed) > 0 && trimmed[0] == '[' { trimmed := bytes.TrimSpace(data)
if len(trimmed) > 0 && trimmed[0] == '[' {
var texts []string var texts []string
if err := json.Unmarshal(trimmed, &texts); err != nil { if err := json.Unmarshal(trimmed, &texts); err != nil {
return err return err
@@ -109,6 +109,11 @@ func (p *PayloadText) UnmarshalJSON(data []byte) error {
return nil return nil
} }
// A JSON null names no text: leave the zero value, so Texts stays nil.
if string(trimmed) == "null" {
return nil
}
var text string var text string
if err := json.Unmarshal(data, &text); err != nil { if err := json.Unmarshal(data, &text); err != nil {
return err return err
@@ -162,7 +167,7 @@ func Router(cfg *Config) *gin.Engine {
// A body without `text` never named a text to translate, which is a // A body without `text` never named a text to translate, which is a
// payload error. `"text": []` names a list that happens to be empty, // payload error. `"text": []` names a list that happens to be empty,
// and is answered with an empty list, so it is not caught here. // and is answered with an empty list, so it is not caught here.
if !req.Text.Batch && len(req.Text.Texts) == 0 { if req.Text.Texts == nil {
c.JSON(http.StatusBadRequest, gin.H{ c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest, "code": http.StatusBadRequest,
"message": "Invalid request payload", "message": "Invalid request payload",
+2 -2
View File
@@ -26,7 +26,7 @@ func TestPayloadTextUnmarshalJSON(t *testing.T) {
{name: "array", payload: `{"text":["Hello","Good morning"]}`, wantTexts: []string{"Hello", "Good morning"}, wantBatch: true}, {name: "array", payload: `{"text":["Hello","Good morning"]}`, wantTexts: []string{"Hello", "Good morning"}, wantBatch: true},
{name: "single-element array", payload: `{"text":["Hello"]}`, wantTexts: []string{"Hello"}, wantBatch: true}, {name: "single-element array", payload: `{"text":["Hello"]}`, wantTexts: []string{"Hello"}, wantBatch: true},
{name: "empty array", payload: `{"text":[]}`, wantTexts: []string{}, wantBatch: true}, {name: "empty array", payload: `{"text":[]}`, wantTexts: []string{}, wantBatch: true},
{name: "null", payload: `{"text":null}`, wantTexts: []string{""}}, {name: "null", payload: `{"text":null}`, wantTexts: nil},
{name: "no text field", payload: `{}`, wantTexts: nil}, {name: "no text field", payload: `{}`, wantTexts: nil},
{name: "number", payload: `{"text":1}`, wantErr: true}, {name: "number", payload: `{"text":1}`, wantErr: true},
{name: "object", payload: `{"text":{}}`, wantErr: true}, {name: "object", payload: `{"text":{}}`, wantErr: true},
@@ -68,7 +68,7 @@ func TestTranslateHandlerAnswersEmptyText(t *testing.T) {
}{ }{
{"empty array", `{"text":[],"target_lang":"EN"}`, http.StatusOK, `"data":[]`}, {"empty array", `{"text":[],"target_lang":"EN"}`, http.StatusOK, `"data":[]`},
{"empty string", `{"text":"","target_lang":"EN"}`, http.StatusOK, `"data":""`}, {"empty string", `{"text":"","target_lang":"EN"}`, http.StatusOK, `"data":""`},
{"null", `{"text":null,"target_lang":"EN"}`, http.StatusOK, `"data":""`}, {"null", `{"text":null,"target_lang":"EN"}`, http.StatusBadRequest, `Invalid request payload`},
{"array of empty", `{"text":[""],"target_lang":"EN"}`, http.StatusOK, `"data":[""]`}, {"array of empty", `{"text":[""],"target_lang":"EN"}`, http.StatusOK, `"data":[""]`},
{"no text field", `{"target_lang":"EN"}`, http.StatusBadRequest, `Invalid request payload`}, {"no text field", `{"target_lang":"EN"}`, http.StatusBadRequest, `Invalid request payload`},
} }