bug: ListContents fails when path is a file (object vs array response) #73

Closed
opened 2026-05-11 13:26:49 +00:00 by rodin · 0 comments
Owner

Problem

When ListContents() is called with a path that points to a file (not a directory), Gitea returns a single JSON object:

{"name":"README.md","path":"README.md","type":"file",...}

But the code tries to unmarshal it as an array:

var entries []ContentEntry
if err := json.Unmarshal(body, &entries); err != nil {
    return nil, fmt.Errorf("parse contents JSON: %w", err)
}

This fails with: json: cannot unmarshal object into Go value of type []gitea.ContentEntry

How to Trigger

  1. Set patterns-files: README.md (or let it default to that per #71)
  2. review-bot calls ListContents(ctx, owner, repo, "README.md")
  3. Gitea returns a single object, not an array
  4. JSON unmarshal fails

Fix

Detect single-object response and handle it:

var entries []ContentEntry
if err := json.Unmarshal(body, &entries); err != nil {
    // Maybe it is a single file, not a directory
    var single ContentEntry
    if err2 := json.Unmarshal(body, &single); err2 == nil {
        entries = []ContentEntry{single}
    } else {
        return nil, fmt.Errorf("parse contents JSON: %w", err)
    }
}

Or check the response structure before unmarshaling.

Related

  • #70: . path normalization
  • #71: bad default README.md
## Problem When `ListContents()` is called with a path that points to a **file** (not a directory), Gitea returns a single JSON object: ```json {"name":"README.md","path":"README.md","type":"file",...} ``` But the code tries to unmarshal it as an array: ```go var entries []ContentEntry if err := json.Unmarshal(body, &entries); err != nil { return nil, fmt.Errorf("parse contents JSON: %w", err) } ``` This fails with: `json: cannot unmarshal object into Go value of type []gitea.ContentEntry` ## How to Trigger 1. Set `patterns-files: README.md` (or let it default to that per #71) 2. review-bot calls `ListContents(ctx, owner, repo, "README.md")` 3. Gitea returns a single object, not an array 4. JSON unmarshal fails ## Fix Detect single-object response and handle it: ```go var entries []ContentEntry if err := json.Unmarshal(body, &entries); err != nil { // Maybe it is a single file, not a directory var single ContentEntry if err2 := json.Unmarshal(body, &single); err2 == nil { entries = []ContentEntry{single} } else { return nil, fmt.Errorf("parse contents JSON: %w", err) } } ``` Or check the response structure before unmarshaling. ## Related - #70: `.` path normalization - #71: bad default `README.md`
rodin added the wip label 2026-05-11 14:17:31 +00:00
rodin removed the wip label 2026-05-11 14:22:46 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: rodin/review-bot#73