fix(16.2): WR-06 hand a translatable Decode only strings

liftMLValues validated only object-shaped submissions, so a scalar
submitted for a translatable plugin field reached Decode as nil, a
float64 or a bool although pact promises a string; a type that asserts
a string panicked into an opaque 500. decodePluginFields now passes a
string through, turns null into the empty string and refuses any other
value with a 422 on the field before Decode runs.
This commit is contained in:
2026-10-10 16:45:05 +02:00
parent c532529d18
commit f388d27604
5 changed files with 60 additions and 3 deletions

View File

@@ -474,6 +474,16 @@ func decodePluginFields(ctx context.Context, in pluginDecodeInput) (pluginDecode
return pluginDecodeResult{}, err
}
if value, ok := in.Projected[field.Name]; ok {
// pact.FormFieldValue promises a translatable type a string: a
// scalar submitted instead of a locale map is text, null is
// empty, anything else is refused before Decode runs (WR-06).
switch value.(type) {
case string:
case nil:
value = ""
default:
return pluginDecodeResult{}, &ValidationError{Details: fieldDetail(field.Name, "The "+field.Name+" field must be text or an object of locale codes to text.")}
}
stored, keep, err := decodeOne(ctx, in, field, def, true, value, &out)
if err != nil {
return pluginDecodeResult{}, err

View File

@@ -759,6 +759,51 @@ func TestFieldTypeDecode(t *testing.T) {
}
})
// WR-06: pact promises a translatable type's Decode a string.
t.Run("a translatable Decode only ever receives a string", func(t *testing.T) {
var got []any
probe := ftNotesMLType()
probe.Decode = func(_ context.Context, in pact.FormFieldValue) (pact.FormFieldDecoded, error) {
got = append(got, in.Value)
text := in.Value.(string) // panics if the contract is broken
return pact.FormFieldDecoded{Value: text}, nil
}
occ := ftOnly(t, ftNotesField, ftNotesType(), probe)
for _, tc := range []struct {
name string
body any
want string
}{
{"a string", "plain", "plain"},
{"null", nil, ""},
} {
got = nil
res, projected, _, err := ftSave(t, occ, ftWriter(), &ftNote{ID: 1}, map[string]any{"body": tc.body}, false)
if err != nil {
t.Fatalf("%s: err=%v", tc.name, err)
}
if len(got) != 1 || got[0] != tc.want || projected["body"] != tc.want || len(res.writes["body"]) != 1 {
t.Fatalf("%s: Decode got %#v, projected %#v, writes %v", tc.name, got, projected["body"], res.writes)
}
}
for name, body := range map[string]any{"a number": 3.0, "a boolean": true, "an integer": 7} {
got = nil
_, _, _, err := ftSave(t, occ, ftWriter(), &ftNote{ID: 1}, map[string]any{"body": body}, false)
var invalid *ValidationError
if !errors.As(err, &invalid) || fmt.Sprint(invalid.Details["body"]) != "[The body field must be text or an object of locale codes to text.]" {
t.Fatalf("%s: err=%v", name, err)
}
if len(got) != 0 {
t.Fatalf("%s: Decode ran with %#v", name, got)
}
rec := httptest.NewRecorder()
writeCRUDError(rec, err)
if rec.Code != http.StatusUnprocessableEntity {
t.Fatalf("%s: status=%d body=%s", name, rec.Code, rec.Body.String())
}
}
})
t.Run("Decode receives the save's record, options and locale", func(t *testing.T) {
var got []pact.FormFieldValue
probe := ftNotesMLType()