fix(gitea): handle single-object response in ListContents #74
@@ -458,6 +458,10 @@ func (c *Client) ListContents(ctx context.Context, owner, repo, path string) ([]
|
|||||||
if err2 := json.Unmarshal(body, &single); err2 != nil {
|
if err2 := json.Unmarshal(body, &single); err2 != nil {
|
||||||
|
|
|||||||
return nil, fmt.Errorf("parse contents JSON: %w", err)
|
return nil, fmt.Errorf("parse contents JSON: %w", err)
|
||||||
}
|
}
|
||||||
|
sonnet-review-bot
commented
[NIT] The guard condition **[NIT]** The guard condition `single.Name == "" && single.Path == ""` uses AND — a response where Name is populated but Path is empty (or vice versa) would pass the guard and produce a partially-valid entry. Using OR (`single.Name == "" || single.Path == ""`) would be more defensive, though in practice Gitea always returns both fields for a valid file entry.
|
|||||||
|
// Guard against empty/malformed responses
|
||||||
|
if single.Name == "" && single.Path == "" {
|
||||||
|
return nil, fmt.Errorf("parse contents JSON: empty response for path %q", path)
|
||||||
|
}
|
||||||
entries = []ContentEntry{single}
|
entries = []ContentEntry{single}
|
||||||
}
|
}
|
||||||
return entries, nil
|
return entries, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user
[MINOR] The fallback tries to unmarshal into a zero-value
ContentEntry. If Gitea returns an empty JSON object{}, the unmarshal will succeed and return an entry with all zero/empty fields (Name: "", Path: "", Type: ""), which would be silently added to the results. This is an edge case but could cause confusing downstream behavior. A guard likeif single.Name == "" && single.Path == ""before wrapping would be more defensive, though arguably over-engineering for this specific Gitea API.[NIT] The variable
err2shadows the outererrin a way that's slightly awkward —err(from the array unmarshal) is preserved for the outer error return whileerr2is used only for the single-object attempt. The naming is fine, but a comment noting whyerr(noterr2) is returned in the failure case would aid future readers: the original error is preferred because it describes the primary expected shape.