mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-05 00:13:24 +01:00
fix(api): keep wiki page name when editing with an empty title (#40153)
Match `PATCH /repos/{owner}/{repo}/wiki/page/{pageName}` document "leave
empty to keep unchanged"
Fixes #39179
---------
Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
1 parent
5c68ba3e0a
commit
f26a314636
3 files changed
+28
-12
No files matched your search
@@ -43,7 +43,7 @@ type WikiPageMetaData struct {
|
||||
// CreateWikiPageOptions form for creating wiki
|
||||
type CreateWikiPageOptions struct {
|
||||
// page title. leave empty to keep unchanged
|
||||
Title string `json:"title" binding:"TrimSpace;Required"`
|
||||
Title string `json:"title" binding:"TrimSpace"`
|
||||
// content must be base64 encoded
|
||||
ContentBase64 string `json:"content_base64"`
|
||||
// optional commit message summarizing the change
|
||||
|
||||
@@ -49,6 +49,11 @@ func NewWikiPage(ctx *context.APIContext) {
|
||||
// "$ref": "#/responses/error"
|
||||
|
||||
form := web.GetForm[*api.CreateWikiPageOptions](ctx)
|
||||
// EditWikiPage shares the form but allows an empty title
|
||||
if form.Title == "" {
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "title is required")
|
||||
return
|
||||
}
|
||||
wikiName := wiki_service.UserTitleToWebPath("", form.Title)
|
||||
|
||||
if len(form.Message) == 0 {
|
||||
@@ -117,10 +122,9 @@ func EditWikiPage(ctx *context.APIContext) {
|
||||
form := web.GetForm[*api.CreateWikiPageOptions](ctx)
|
||||
|
||||
oldWikiName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("pageName"))
|
||||
newWikiName := wiki_service.UserTitleToWebPath("", form.Title)
|
||||
|
||||
if len(newWikiName) == 0 {
|
||||
newWikiName = oldWikiName
|
||||
newWikiName := oldWikiName
|
||||
if form.Title != "" {
|
||||
newWikiName = wiki_service.UserTitleToWebPath("", form.Title)
|
||||
}
|
||||
|
||||
if len(form.Message) == 0 {
|
||||
|
||||
@@ -172,6 +172,17 @@ func TestAPIGetWikiPage(t *testing.T) {
|
||||
session := loginUser(t, username)
|
||||
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
|
||||
|
||||
for _, title := range []string{"", " "} {
|
||||
content := base64.StdEncoding.EncodeToString(fmt.Appendf(nil, "Edited with title %q", title))
|
||||
req := NewRequestWithJSON(t, "PATCH", "/api/v1/repos/user2/repo1/wiki/page/Home", &api.CreateWikiPageOptions{
|
||||
Title: title,
|
||||
ContentBase64: content,
|
||||
}).AddTokenAuth(token)
|
||||
page := DecodeJSON(t, MakeRequest(t, req, http.StatusOK), &api.WikiPage{})
|
||||
assert.Equal(t, "Home", page.Title)
|
||||
assert.Equal(t, content, page.ContentBase64)
|
||||
}
|
||||
|
||||
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/wiki/page/Page-With-Spaced-Name", username, "repo1")
|
||||
|
||||
req := NewRequestWithJSON(t, "PATCH", urlStr, &api.CreateWikiPageOptions{
|
||||
@@ -197,13 +208,14 @@ func testAPICreateWikiPage(t *testing.T, session *TestSession, userName, repoNam
|
||||
}
|
||||
|
||||
func TestAPINewWikiPage(t *testing.T) {
|
||||
for _, title := range []string{
|
||||
"New page",
|
||||
"&&&&",
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
session := loginUser(t, "user2")
|
||||
for title, status := range map[string]int{
|
||||
"New page": http.StatusCreated,
|
||||
"&&&&": http.StatusCreated,
|
||||
"": http.StatusUnprocessableEntity,
|
||||
" ": http.StatusUnprocessableEntity,
|
||||
} {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
username := "user2"
|
||||
session := loginUser(t, username)
|
||||
testAPICreateWikiPage(t, session, username, "repo1", title, http.StatusCreated)
|
||||
testAPICreateWikiPage(t, session, "user2", "repo1", title, status)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user