Skip to content

Commit 2a828e2

Browse files
authored
Clarify path param naming (#32969)
In history (from some legacy frameworks), both `:name` and `name` are supported as path path name, `:name` is an alias to `name`. To make code consistent, now we should only use `name` but not `:name`. Also added panic check in related functions to make sure the name won't be abused in case some downstreams still use them.
1 parent b8b690f commit 2a828e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+461
-429
lines changed

modules/setting/setting.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,9 @@ func checkOverlappedPath(name, path string) {
235235
}
236236
configuredPaths[path] = name
237237
}
238+
239+
func PanicInDevOrTesting(msg string, a ...any) {
240+
if !IsProd || IsInTesting {
241+
panic(fmt.Sprintf(msg, a...))
242+
}
243+
}

modules/templates/helper.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,5 @@ func QueryBuild(a ...any) template.URL {
331331
}
332332

333333
func panicIfDevOrTesting() {
334-
if !setting.IsProd || setting.IsInTesting {
335-
panic("legacy template functions are for backward compatibility only, do not use them in new code")
336-
}
334+
setting.PanicInDevOrTesting("legacy template functions are for backward compatibility only, do not use them in new code")
337335
}

routers/api/v1/admin/adopt.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ func AdoptRepository(ctx *context.APIContext) {
8080
// "$ref": "#/responses/notFound"
8181
// "403":
8282
// "$ref": "#/responses/forbidden"
83-
ownerName := ctx.PathParam(":username")
84-
repoName := ctx.PathParam(":reponame")
83+
ownerName := ctx.PathParam("username")
84+
repoName := ctx.PathParam("reponame")
8585

8686
ctxUser, err := user_model.GetUserByName(ctx, ownerName)
8787
if err != nil {
@@ -142,8 +142,8 @@ func DeleteUnadoptedRepository(ctx *context.APIContext) {
142142
// "$ref": "#/responses/empty"
143143
// "403":
144144
// "$ref": "#/responses/forbidden"
145-
ownerName := ctx.PathParam(":username")
146-
repoName := ctx.PathParam(":reponame")
145+
ownerName := ctx.PathParam("username")
146+
repoName := ctx.PathParam("reponame")
147147

148148
ctxUser, err := user_model.GetUserByName(ctx, ownerName)
149149
if err != nil {

routers/api/v1/admin/cron.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func PostCronTask(ctx *context.APIContext) {
7474
// "$ref": "#/responses/empty"
7575
// "404":
7676
// "$ref": "#/responses/notFound"
77-
task := cron.GetTask(ctx.PathParam(":task"))
77+
task := cron.GetTask(ctx.PathParam("task"))
7878
if task == nil {
7979
ctx.NotFound()
8080
return

routers/api/v1/admin/email.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func GetAllEmails(ctx *context.APIContext) {
3838
listOptions := utils.GetListOptions(ctx)
3939

4040
emails, maxResults, err := user_model.SearchEmails(ctx, &user_model.SearchEmailOptions{
41-
Keyword: ctx.PathParam(":email"),
41+
Keyword: ctx.PathParam("email"),
4242
ListOptions: listOptions,
4343
})
4444
if err != nil {
@@ -82,6 +82,6 @@ func SearchEmail(ctx *context.APIContext) {
8282
// "403":
8383
// "$ref": "#/responses/forbidden"
8484

85-
ctx.SetPathParam(":email", ctx.FormTrim("q"))
85+
ctx.SetPathParam("email", ctx.FormTrim("q"))
8686
GetAllEmails(ctx)
8787
}

routers/api/v1/admin/hooks.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func GetHook(ctx *context.APIContext) {
7373
// "200":
7474
// "$ref": "#/responses/Hook"
7575

76-
hookID := ctx.PathParamInt64(":id")
76+
hookID := ctx.PathParamInt64("id")
7777
hook, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID)
7878
if err != nil {
7979
if errors.Is(err, util.ErrNotExist) {
@@ -142,7 +142,7 @@ func EditHook(ctx *context.APIContext) {
142142
form := web.GetForm(ctx).(*api.EditHookOption)
143143

144144
// TODO in body params
145-
hookID := ctx.PathParamInt64(":id")
145+
hookID := ctx.PathParamInt64("id")
146146
utils.EditSystemHook(ctx, form, hookID)
147147
}
148148

@@ -164,7 +164,7 @@ func DeleteHook(ctx *context.APIContext) {
164164
// "204":
165165
// "$ref": "#/responses/empty"
166166

167-
hookID := ctx.PathParamInt64(":id")
167+
hookID := ctx.PathParamInt64("id")
168168
if err := webhook.DeleteDefaultSystemWebhook(ctx, hookID); err != nil {
169169
if errors.Is(err, util.ErrNotExist) {
170170
ctx.NotFound()

routers/api/v1/admin/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
375375
// "404":
376376
// "$ref": "#/responses/notFound"
377377

378-
if err := asymkey_service.DeletePublicKey(ctx, ctx.ContextUser, ctx.PathParamInt64(":id")); err != nil {
378+
if err := asymkey_service.DeletePublicKey(ctx, ctx.ContextUser, ctx.PathParamInt64("id")); err != nil {
379379
if asymkey_model.IsErrKeyNotExist(err) {
380380
ctx.NotFound()
381381
} else if asymkey_model.IsErrKeyAccessDenied(err) {

routers/api/v1/api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,12 @@ func orgAssignment(args ...bool) func(ctx *context.APIContext) {
596596

597597
var err error
598598
if assignOrg {
599-
ctx.Org.Organization, err = organization.GetOrgByName(ctx, ctx.PathParam(":org"))
599+
ctx.Org.Organization, err = organization.GetOrgByName(ctx, ctx.PathParam("org"))
600600
if err != nil {
601601
if organization.IsErrOrgNotExist(err) {
602-
redirectUserID, err := user_model.LookupUserRedirect(ctx, ctx.PathParam(":org"))
602+
redirectUserID, err := user_model.LookupUserRedirect(ctx, ctx.PathParam("org"))
603603
if err == nil {
604-
context.RedirectToUser(ctx.Base, ctx.PathParam(":org"), redirectUserID)
604+
context.RedirectToUser(ctx.Base, ctx.PathParam("org"), redirectUserID)
605605
} else if user_model.IsErrUserRedirectNotExist(err) {
606606
ctx.NotFound("GetOrgByName", err)
607607
} else {
@@ -616,7 +616,7 @@ func orgAssignment(args ...bool) func(ctx *context.APIContext) {
616616
}
617617

618618
if assignTeam {
619-
ctx.Org.Team, err = organization.GetTeamByID(ctx, ctx.PathParamInt64(":teamid"))
619+
ctx.Org.Team, err = organization.GetTeamByID(ctx, ctx.PathParamInt64("teamid"))
620620
if err != nil {
621621
if organization.IsErrTeamNotExist(err) {
622622
ctx.NotFound()

routers/api/v1/notify/threads.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func ReadThread(ctx *context.APIContext) {
101101
}
102102

103103
func getThread(ctx *context.APIContext) *activities_model.Notification {
104-
n, err := activities_model.GetNotificationByID(ctx, ctx.PathParamInt64(":id"))
104+
n, err := activities_model.GetNotificationByID(ctx, ctx.PathParamInt64("id"))
105105
if err != nil {
106106
if db.IsErrNotExist(err) {
107107
ctx.Error(http.StatusNotFound, "GetNotificationByID", err)

routers/api/v1/org/label.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func GetLabel(ctx *context.APIContext) {
139139
label *issues_model.Label
140140
err error
141141
)
142-
strID := ctx.PathParam(":id")
142+
strID := ctx.PathParam("id")
143143
if intID, err2 := strconv.ParseInt(strID, 10, 64); err2 != nil {
144144
label, err = issues_model.GetLabelInOrgByName(ctx, ctx.Org.Organization.ID, strID)
145145
} else {
@@ -190,7 +190,7 @@ func EditLabel(ctx *context.APIContext) {
190190
// "422":
191191
// "$ref": "#/responses/validationError"
192192
form := web.GetForm(ctx).(*api.EditLabelOption)
193-
l, err := issues_model.GetLabelInOrgByID(ctx, ctx.Org.Organization.ID, ctx.PathParamInt64(":id"))
193+
l, err := issues_model.GetLabelInOrgByID(ctx, ctx.Org.Organization.ID, ctx.PathParamInt64("id"))
194194
if err != nil {
195195
if issues_model.IsErrOrgLabelNotExist(err) {
196196
ctx.NotFound()
@@ -249,7 +249,7 @@ func DeleteLabel(ctx *context.APIContext) {
249249
// "404":
250250
// "$ref": "#/responses/notFound"
251251

252-
if err := issues_model.DeleteLabel(ctx, ctx.Org.Organization.ID, ctx.PathParamInt64(":id")); err != nil {
252+
if err := issues_model.DeleteLabel(ctx, ctx.Org.Organization.ID, ctx.PathParamInt64("id")); err != nil {
253253
ctx.Error(http.StatusInternalServerError, "DeleteLabel", err)
254254
return
255255
}

0 commit comments

Comments
 (0)