diff --git a/README.md b/README.md index f6b362f..335ec2d 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ curl -X POST http://localhost:1188/translate \ 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. -- 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. ## Discussion Group diff --git a/service/service.go b/service/service.go index 842309c..c7eb78a 100644 --- a/service/service.go +++ b/service/service.go @@ -91,8 +91,7 @@ type PayloadTranslate struct { // 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 // non-strings) fails the payload decode, which the handler reports as -// 400 Invalid request payload; null decodes to the empty string exactly as it -// did when text was a plain string. +// 400 Invalid request payload; null names no text, like a body without the key. type PayloadText struct { Texts []string Batch bool @@ -100,7 +99,8 @@ type PayloadText struct { // UnmarshalJSON implements json.Unmarshaler. 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 if err := json.Unmarshal(trimmed, &texts); err != nil { return err @@ -109,6 +109,11 @@ func (p *PayloadText) UnmarshalJSON(data []byte) error { 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 if err := json.Unmarshal(data, &text); err != nil { 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 // 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 { + if req.Text.Texts == nil { c.JSON(http.StatusBadRequest, gin.H{ "code": http.StatusBadRequest, "message": "Invalid request payload", diff --git a/service/service_test.go b/service/service_test.go index c3f0b38..a8aa210 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -26,7 +26,7 @@ func TestPayloadTextUnmarshalJSON(t *testing.T) { {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: "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: "number", payload: `{"text":1}`, 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 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":[""]`}, {"no text field", `{"target_lang":"EN"}`, http.StatusBadRequest, `Invalid request payload`}, }