Merge pull request #858 from samcharles93/fix/staterpc-nil-collection-fields
fix(staterpc): never decode a collection field to a nil slice
This commit is contained in:
3 files changed
+111
-3
No files matched your search
@@ -436,12 +436,20 @@ func (c *Client) TaskLog(ctx context.Context, taskID int64, attempt int, q loggi
|
||||
Components: []string{},
|
||||
}, nil
|
||||
}
|
||||
// Both collections are normalised: protobuf decodes an empty `repeated`
|
||||
// field to nil, and a nil slice marshals to JSON null rather than an empty
|
||||
// list, so the field's type would otherwise depend on whether it happened
|
||||
// to have contents. mapValues owns the entries side.
|
||||
components := r.Components
|
||||
if components == nil {
|
||||
components = []string{}
|
||||
}
|
||||
return logging.TaskLogPage{
|
||||
Entries: mapValues(r.Entries, taskLogEntryValue),
|
||||
Truncated: r.Truncated,
|
||||
File: r.File,
|
||||
Found: true,
|
||||
Components: r.Components,
|
||||
Components: components,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
)
|
||||
|
||||
// A decoded task-log page must never carry a nil slice for a collection field.
|
||||
// Go marshals a nil slice to JSON null, and null is not an empty list: a
|
||||
// client that reads the field as an array sees a different type than the same
|
||||
// route serves when the collection is non-empty.
|
||||
//
|
||||
// This is the nil-slice class the contract-conformance audit deferred (its
|
||||
// named revisit condition is a handler that can produce nil). The local reader
|
||||
// already normalises both fields; the wire mapping did not, because protobuf
|
||||
// decodes an empty `repeated` field to nil, so the grpc path regressed what the
|
||||
// local path got right. Asserting the JSON shape rather than the slice catches
|
||||
// it on both paths without depending on which one is under test.
|
||||
func TestTaskLogPageCarriesNoNilCollectionFields(t *testing.T) {
|
||||
// Both collections must be exercised empty. A log with entries but no
|
||||
// structured "component" field leaves only Components empty; an attempt
|
||||
// whose log file exists but holds no lines leaves only Entries empty, and
|
||||
// that second shape is what catches an empty-slice mapping regressing on
|
||||
// the entries side.
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
write bool
|
||||
}{
|
||||
{name: "entries present, components empty", write: true},
|
||||
{name: "entries empty, components empty", write: false},
|
||||
} {
|
||||
for _, mode := range []string{"local", "grpc"} {
|
||||
t.Run(tc.name+"/"+mode, func(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
baseDir := filepath.Join(t.TempDir(), "logs", "tasks")
|
||||
reader := logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{})
|
||||
|
||||
const taskID = 7
|
||||
if err := reader.Open(taskID, 0); err != nil {
|
||||
t.Fatalf("open sink: %v", err)
|
||||
}
|
||||
if tc.write {
|
||||
// No "component" field at all: Components stays empty while
|
||||
// Entries does not.
|
||||
reader.Write(taskID, logging.Entry{Level: "INFO", Message: "starting"})
|
||||
}
|
||||
if err := reader.Close(taskID); err != nil {
|
||||
t.Fatalf("close sink: %v", err)
|
||||
}
|
||||
|
||||
var c taskLogContract = reader
|
||||
if mode == "grpc" {
|
||||
c = remoteTaskStore(t, store.OpenTest(t), reader)
|
||||
}
|
||||
|
||||
page, err := c.TaskLog(ctx, taskID, 0, logging.Query{})
|
||||
if err != nil {
|
||||
t.Fatalf("TaskLog: %v", err)
|
||||
}
|
||||
if !page.Found {
|
||||
t.Fatal("Found = false for an attempt whose log file exists")
|
||||
}
|
||||
if tc.write && len(page.Entries) != 1 {
|
||||
t.Fatalf("entries = %d, want the line the sink wrote", len(page.Entries))
|
||||
}
|
||||
if !tc.write && len(page.Entries) != 0 {
|
||||
t.Fatalf("entries = %d, want none for a log with no lines", len(page.Entries))
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(page)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal page: %v", err)
|
||||
}
|
||||
var decoded map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatalf("unmarshal page: %v", err)
|
||||
}
|
||||
for _, field := range []string{"entries", "components"} {
|
||||
if got := string(decoded[field]); got == "null" {
|
||||
t.Errorf("%s marshalled to null, want an empty list: the field's type must not depend on whether it has contents",
|
||||
field)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,9 +34,18 @@ func timeValue(t *timestamppb.Timestamp) time.Time {
|
||||
return t.AsTime()
|
||||
}
|
||||
|
||||
// mapValues maps a slice of proto values to domain values, always returning a
|
||||
// non-nil result for empty input.
|
||||
//
|
||||
// Protobuf decodes an empty `repeated` field to nil, and Go marshals a nil
|
||||
// slice to JSON null rather than []. Returning nil here made every decoded
|
||||
// collection field's JSON type depend on whether it happened to have contents,
|
||||
// and made the wire path disagree with the local path, which normalises its own
|
||||
// slices. []B{} for empty input keeps the shape stable at every call site
|
||||
// without making each one remember to guard.
|
||||
func mapValues[A, B any](in []A, f func(A) B) []B {
|
||||
if in == nil {
|
||||
return nil
|
||||
if len(in) == 0 {
|
||||
return []B{}
|
||||
}
|
||||
out := make([]B, len(in))
|
||||
for i, v := range in {
|
||||
|
||||
Reference in new issue
Block a user