Skip to content

Commit 5d8a52c

Browse files
committed
Ensure things are json marshalable
1 parent 17445bb commit 5d8a52c

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

models/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Issue struct {
4747
IsClosed bool `xorm:"INDEX"`
4848
IsRead bool `xorm:"-"`
4949
IsPull bool `xorm:"INDEX"` // Indicates whether is a pull request or not.
50-
PullRequest *PullRequest `xorm:"-"`
50+
PullRequest *PullRequest `xorm:"-" json:"-"`
5151
NumComments int
5252
Ref string
5353

models/repo_unit.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package models
66

77
import (
88
"encoding/json"
9+
"fmt"
10+
"reflect"
911

1012
"code.gitea.io/gitea/modules/timeutil"
1113

@@ -169,6 +171,57 @@ func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
169171
return r.Config.(*ExternalTrackerConfig)
170172
}
171173

174+
// MarshalJSON implements json.Marshaler
175+
func (r *RepoUnit) MarshalJSON() ([]byte, error) {
176+
tmp := map[string]interface{}{}
177+
tmp["ID"] = r.ID
178+
tmp["RepoID"] = r.RepoID
179+
var err error
180+
tmp["Config"], err = r.Config.ToDB()
181+
if err != nil {
182+
return nil, err
183+
}
184+
tmp["CreatedUnix"] = r.CreatedUnix
185+
bs, err := json.Marshal(tmp)
186+
return bs, err
187+
}
188+
189+
// UnmarshalJSON implements json.UnMarshaler
190+
func (r *RepoUnit) UnmarshalJSON(bs []byte) (err error) {
191+
tmp := struct {
192+
ID int64
193+
RepoID int64
194+
Type UnitType
195+
Config []byte
196+
CreatedUnix timeutil.TimeStamp
197+
}{}
198+
err = json.Unmarshal(bs, &tmp)
199+
if err != nil {
200+
return err
201+
}
202+
203+
r.ID = tmp.ID
204+
r.RepoID = tmp.RepoID
205+
r.Type = tmp.Type
206+
if r.Type != 0 {
207+
defer func() {
208+
panicked := recover()
209+
if panicked == nil {
210+
return
211+
}
212+
// Panicing is not very nice...
213+
err = fmt.Errorf("%v", panicked)
214+
r.Config = new(UnitConfig)
215+
}()
216+
typeInt64 := int64(r.Type)
217+
typeInterface := reflect.ValueOf(typeInt64).Interface()
218+
r.BeforeSet("type", xorm.Cell(&typeInterface))
219+
return json.Unmarshal(tmp.Config, &(r.Config))
220+
}
221+
r.Config = new(UnitConfig)
222+
return nil
223+
}
224+
172225
func getUnitsByRepoID(e Engine, repoID int64) (units []*RepoUnit, err error) {
173226
var tmpUnits []*RepoUnit
174227
if err := e.Where("repo_id = ?", repoID).Find(&tmpUnits); err != nil {

models/user.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ type User struct {
110110
LoginSource int64 `xorm:"NOT NULL DEFAULT 0"`
111111
LoginName string
112112
Type UserType
113-
OwnedOrgs []*User `xorm:"-"`
114-
Orgs []*User `xorm:"-"`
115-
Repos []*Repository `xorm:"-"`
113+
OwnedOrgs []*User `xorm:"-" json:"-"`
114+
Orgs []*User `xorm:"-" json:"-"`
115+
Repos []*Repository `xorm:"-" json:"-"`
116116
Location string
117117
Website string
118118
Rands string `xorm:"VARCHAR(10)"`
@@ -152,9 +152,9 @@ type User struct {
152152
// For organization
153153
NumTeams int
154154
NumMembers int
155-
Teams []*Team `xorm:"-"`
156-
Members UserList `xorm:"-"`
157-
MembersIsPublic map[int64]bool `xorm:"-"`
155+
Teams []*Team `xorm:"-" json:"-"`
156+
Members UserList `xorm:"-" json:"-"`
157+
MembersIsPublic map[int64]bool `xorm:"-" json:"-"`
158158
Visibility structs.VisibleType `xorm:"NOT NULL DEFAULT 0"`
159159
RepoAdminChangeTeamAccess bool `xorm:"NOT NULL DEFAULT false"`
160160

modules/convert/pull.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
2626
err error
2727
)
2828

29+
if pr.Issue.PullRequest == nil {
30+
pr.Issue.PullRequest = pr
31+
}
32+
2933
if err = pr.Issue.LoadRepo(); err != nil {
3034
log.Error("pr.Issue.LoadRepo[%d]: %v", pr.ID, err)
3135
return nil

routers/api/v1/repo/issue_comment.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
413413
ctx.Error(http.StatusInternalServerError, "UpdateComment", err)
414414
return
415415
}
416+
_ = comment.LoadAssigneeUser()
417+
_ = comment.LoadPoster()
416418

417419
ctx.JSON(http.StatusOK, comment.APIFormat())
418420
}

0 commit comments

Comments
 (0)