Skip to content

Commit 7df2cc3

Browse files
fix(api): return empty array for pins instead of null (#4971)
1 parent 941f4fc commit 7df2cc3

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

pkg/api/pin_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ func TestPinHandlers(t *testing.T) {
100100
checkPinHandlers(t, client, rootHash, true)
101101
})
102102

103+
t.Run("no pins", func(t *testing.T) {
104+
jsonhttptest.Request(t, client, http.MethodGet, "/pins", http.StatusOK,
105+
jsonhttptest.WithExpectedJSONResponse(struct {
106+
References []swarm.Address `json:"references"`
107+
}{
108+
References: make([]swarm.Address, 0),
109+
}),
110+
)
111+
})
112+
103113
t.Run("bytes missing", func(t *testing.T) {
104114
jsonhttptest.Request(t, client, http.MethodPost, "/pins/"+swarm.RandAddress(t).String(), http.StatusNotFound)
105115
})

pkg/storer/internal/pinning/pinning.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ type collectionPutter struct {
8686
// Put adds a chunk to the pin collection.
8787
// The user of the putter MUST mutex lock the call to prevent data-races across multiple upload sessions.
8888
func (c *collectionPutter) Put(ctx context.Context, st transaction.Store, ch swarm.Chunk) error {
89-
9089
// do not allow any Puts after putter was closed
9190
if c.closed {
9291
return errPutterAlreadyClosed
@@ -129,7 +128,6 @@ func (c *collectionPutter) Close(st storage.IndexStore, root swarm.Address) erro
129128

130129
collection := &pinCollectionItem{Addr: root}
131130
has, err := st.Has(collection)
132-
133131
if err != nil {
134132
return fmt.Errorf("pin store: check previous root: %w", err)
135133
}
@@ -176,7 +174,6 @@ func (c *collectionPutter) Cleanup(st transaction.Storage) error {
176174

177175
// CleanupDirty will iterate over all the dirty collections and delete them.
178176
func CleanupDirty(st transaction.Storage) error {
179-
180177
dirtyCollections := make([]*dirtyCollection, 0)
181178
err := st.IndexStore().Iterate(
182179
storage.Query{
@@ -212,7 +209,7 @@ func HasPin(st storage.Reader, root swarm.Address) (bool, error) {
212209

213210
// Pins lists all the added pinning collections.
214211
func Pins(st storage.Reader) ([]swarm.Address, error) {
215-
var pins []swarm.Address
212+
pins := make([]swarm.Address, 0)
216213
err := st.Iterate(storage.Query{
217214
Factory: func() storage.Item { return new(pinCollectionItem) },
218215
ItemProperty: storage.QueryItemID,
@@ -258,7 +255,6 @@ func deleteCollectionChunks(ctx context.Context, st transaction.Storage, collect
258255
)
259256
})
260257
})
261-
262258
}(item)
263259
}
264260

pkg/storer/internal/pinning/pinning_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,20 @@ func TestPinStore(t *testing.T) {
359359
t.Fatalf("unexpected error on close, want: %v, got: %v", pinstore.ErrCollectionRootAddressIsZero, err)
360360
}
361361
})
362+
363+
t.Run("have 0 pins", func(t *testing.T) {
364+
pins, err := pinstore.Pins(internal.NewInmemStorage().IndexStore())
365+
if err != nil {
366+
t.Fatal(err)
367+
}
368+
if len(pins) != 0 {
369+
t.Fatalf("expected 0 pins, found %d", len(pins))
370+
}
371+
372+
if pins == nil {
373+
t.Fatal("pins is nil")
374+
}
375+
})
362376
}
363377

364378
func TestCleanup(t *testing.T) {

0 commit comments

Comments
 (0)