mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2026-12-01 23:07:51 +00:00
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:
@@ -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`},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user