diff --git a/modules/structs/repo_wiki.go b/modules/structs/repo_wiki.go index 4c5cd365a8b..8e8f6f19095 100644 --- a/modules/structs/repo_wiki.go +++ b/modules/structs/repo_wiki.go @@ -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 diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 78f662d5da5..8b16ed9a8d5 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -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 { diff --git a/tests/integration/api_wiki_test.go b/tests/integration/api_wiki_test.go index 2b09906a0f1..7e67f9d1725 100644 --- a/tests/integration/api_wiki_test.go +++ b/tests/integration/api_wiki_test.go @@ -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) } }