Skip to content

Commit 0625f64

Browse files
fstachurarjarry
authored andcommitted
api: fix related field API incompatibility in /patches
In Patchwork 3.x, in API versions above 1.1, the related field of /patches is always an array. It is not present at all in API version 1.1. In Patchwork 4.0, the field is null in 1.1, and is an array in later API versions. Adding the omitempty tag causes the field to disappear from 1.1, but then it's either an array or not present if empty in 1.4. Make the API more compatible with Patchwork 3.x by changing the field into a pointer to a slice, adding the omitzero tag, and always explicitly allocating an empty slice (&nil != &[]string{}) for /patches responses. Signed-off-by: Franciszek Stachura <fbstachura@gmail.com> Acked-by: Robin Jarry <robin@jarry.cc>
1 parent 63f07d0 commit 0625f64

2 files changed

Lines changed: 4 additions & 6 deletions

File tree

‎pkg/api/patches.go‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ func applyPatchFilters(q *bun.SelectQuery, input *ListPatchesInput) *bun.SelectQ
364364
}
365365

366366
func patchToListResponse(p *db.Patch, base string) PatchListResponse {
367+
related := make([]PatchEmbedded, 0)
367368
r := PatchListResponse{
368369
ID: p.ID,
369370
URL: fmt.Sprintf("%s/patches/%d", base, p.ID),
@@ -378,7 +379,7 @@ func patchToListResponse(p *db.Patch, base string) PatchListResponse {
378379
Checks: fmt.Sprintf("%s/patches/%d/checks", base, p.ID),
379380
Tags: p.Tags,
380381
Series: []SeriesEmbedded{},
381-
Related: []PatchEmbedded{},
382+
Related: &related,
382383
}
383384
if p.Hash != nil {
384385
r.Hash = *p.Hash
@@ -405,7 +406,7 @@ func patchToListResponse(p *db.Patch, base string) PatchListResponse {
405406
r.Tags = map[string]int{}
406407
}
407408
for _, ref := range p.Related {
408-
r.Related = append(r.Related, PatchEmbedded{
409+
related = append(related, PatchEmbedded{
409410
ID: ref.ID,
410411
URL: fmt.Sprintf("%s/patches/%d", base, ref.ID),
411412
Name: ref.Name,
@@ -419,9 +420,6 @@ func patchToListResponse(p *db.Patch, base string) PatchListResponse {
419420
Mbox: fmt.Sprintf("%s/series/%d/mbox", base, ref.ID),
420421
})
421422
}
422-
if r.Related == nil {
423-
r.Related = []PatchEmbedded{}
424-
}
425423
if r.Series == nil {
426424
r.Series = []SeriesEmbedded{}
427425
}

‎pkg/api/types.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ type PatchListResponse struct {
128128
Check string `json:"check" enum:"pending,success,warning,fail"`
129129
Checks string `json:"checks" format:"uri"`
130130
Tags map[string]int `json:"tags"`
131-
Related []PatchEmbedded `json:"related" since:"1.2"`
131+
Related *[]PatchEmbedded `json:"related,omitzero" since:"1.2"`
132132
}
133133

134134
type PatchDetailResponse struct {

0 commit comments

Comments
 (0)