Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 0f7254a

Browse files
3456091Ben
andauthored
Handle JSON containing incomplete "deals" arrays (#27)
* Add test for Client.Status unmarshalling incomplete deals Occassionally, the /status and /user/uploads endpoints return JSON whose "deals" array contains elements missing time.Time fields such as _activation_, _created_, and _updated_. This results in Client.Status() and Client.List() returning errors to the effect of "parsing time "" as "2006-01-02T15:04:05.999Z07:00": cannot parse "" as "2006"". As per web3-storage/web3.storage#1512 (comment) , consumers of these endpoints should accept such data gracefully. This commit comprises a failing test illustrating the problem. * status: Ignore empty _created_ and _updated_ fields in "deals" array Occassionally, the /status and /user/uploads endpoints return JSON whose "deals" array contains elements missing time.Time fields such as _activation_, _created_, and _updated_. This results in Client.Status() and Client.List() returning errors to the effect of "parsing time "" as "2006-01-02T15:04:05.999Z07:00": cannot parse "" as "2006"". As per web3-storage/web3.storage#1512 (comment) , consumers of these endpoints should accept such data gracefully. This commit fixes the failing test introduced in 110a25a. Co-authored-by: Ben <[email protected]>
1 parent a768df7 commit 0f7254a

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

status.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,17 @@ func (d *Deal) UnmarshalJSON(b []byte) error {
168168
return err
169169
}
170170
}
171-
d.Created, err = time.Parse(iso8601, raw.Created)
172-
if err != nil {
173-
return err
171+
if raw.Created != "" {
172+
d.Created, err = time.Parse(iso8601, raw.Created)
173+
if err != nil {
174+
return err
175+
}
174176
}
175-
d.Updated, err = time.Parse(iso8601, raw.Updated)
176-
if err != nil {
177-
return err
177+
if raw.Updated != "" {
178+
d.Updated, err = time.Parse(iso8601, raw.Updated)
179+
if err != nil {
180+
return err
181+
}
178182
}
179183
return nil
180184
}

status_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,69 @@ func TestStatusHappyPath(t *testing.T) {
6363
t.Fatalf("got dagsize %d, wanted %d", st.DagSize, 208)
6464
}
6565
}
66+
67+
var statusIncompleteDealHandler = func(w http.ResponseWriter, r *http.Request) {
68+
carbytes, err := hex.DecodeString(helloCarHex)
69+
if err != nil {
70+
fmt.Printf("DecodeString: %v\n", err)
71+
w.WriteHeader(http.StatusInternalServerError)
72+
return
73+
}
74+
75+
status := map[string]interface{}{
76+
"cid": helloRoot,
77+
"dagSize": len(carbytes),
78+
"created": "2022-02-20T15:04:05.999Z",
79+
"pins": []interface{}{},
80+
"deals": []map[string]interface{}{
81+
{
82+
"dealId": 0,
83+
"storageProvider": "",
84+
"status": "Queued",
85+
"pieceCid": "baga6ea4seaql2leesalhfwmb5xbvhrotu4x76mxn25wcpile6qpbn777omdmqfa",
86+
"dataCid": "bafybeieesr6dknelbduxqmmtt7q2nernm4mmka4k66t7f4d6sgsq3ixc4u",
87+
"dataModelSelector": "Links/144/Hash/Links/2/Hash/Links/0/Hash",
88+
"activation": "",
89+
"expiration": "",
90+
"created": "",
91+
"updated": "",
92+
},
93+
},
94+
}
95+
96+
w.Header().Set("Content-Type", "application/json")
97+
98+
w.WriteHeader(http.StatusOK)
99+
json.NewEncoder(w).Encode(status)
100+
}
101+
102+
func TestStatusIncompleteDeal(t *testing.T) {
103+
routes := routeMap{
104+
"/status/" + helloRoot: {
105+
http.MethodGet: statusIncompleteDealHandler,
106+
},
107+
}
108+
109+
hc, cleanup := startTestServer(t, routes)
110+
defer cleanup()
111+
112+
client, err := NewClient(WithHTTPClient(hc), WithToken("validtoken"))
113+
if err != nil {
114+
t.Fatalf("failed to create client: %v", err)
115+
}
116+
117+
c, _ := cid.Parse(helloRoot)
118+
119+
st, err := client.Status(context.Background(), c)
120+
if err != nil {
121+
t.Fatalf("failed to send request: %v", err)
122+
}
123+
124+
if st.Cid.String() != helloRoot {
125+
t.Fatalf("got cid %s, wanted %s", st.Cid.String(), helloRoot)
126+
}
127+
128+
if st.DagSize != 208 {
129+
t.Fatalf("got dagsize %d, wanted %d", st.DagSize, 208)
130+
}
131+
}

0 commit comments

Comments
 (0)