mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-06 08:26:41 -04:00
Serve OpenAPI 3.0 spec at /openapi.v1.json (#37038)
Add a build-time conversion step that transforms the existing Swagger 2.0 spec into an OpenAPI 3.0 spec. The OAS3 spec is served alongside the existing Swagger 2.0 spec, enabling API clients that require OAS3 to generate code directly from Gitea's API. This is not to be an answer to how gitea handles OAS3 long term, but a way to use what we have to move a step forward. --------- Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Claude (Opus 4.7) <noreply@anthropic.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -96,12 +96,12 @@ func doAPIEditRepository(ctx APITestContext, editRepoOption *api.EditRepoOption,
|
||||
|
||||
func doAPIAddCollaborator(ctx APITestContext, username string, mode perm.AccessMode) func(*testing.T) {
|
||||
return func(t *testing.T) {
|
||||
permission := "read"
|
||||
permission := api.RepoWritePermissionRead
|
||||
|
||||
if mode == perm.AccessModeAdmin {
|
||||
permission = "admin"
|
||||
permission = api.RepoWritePermissionAdmin
|
||||
} else if mode > perm.AccessModeRead {
|
||||
permission = "write"
|
||||
permission = api.RepoWritePermissionWrite
|
||||
}
|
||||
addCollaboratorOption := &api.AddCollaboratorOption{
|
||||
Permission: &permission,
|
||||
|
||||
@@ -127,7 +127,7 @@ func testAPIOrgGeneral(t *testing.T) {
|
||||
apiOrgList := DecodeJSON(t, resp, []*api.Organization{})
|
||||
assert.Len(t, apiOrgList, 13)
|
||||
assert.Equal(t, "Limited Org 36", apiOrgList[1].FullName)
|
||||
assert.Equal(t, "limited", apiOrgList[1].Visibility)
|
||||
assert.Equal(t, api.UserVisibilityLimited, apiOrgList[1].Visibility)
|
||||
|
||||
// accessing without a token will return only public orgs
|
||||
req = NewRequest(t, "GET", "/api/v1/orgs")
|
||||
@@ -136,7 +136,7 @@ func testAPIOrgGeneral(t *testing.T) {
|
||||
apiOrgList = DecodeJSON(t, resp, []*api.Organization{})
|
||||
assert.Len(t, apiOrgList, 9)
|
||||
assert.Equal(t, "org 17", apiOrgList[0].FullName)
|
||||
assert.Equal(t, "public", apiOrgList[0].Visibility)
|
||||
assert.Equal(t, api.UserVisibilityPublic, apiOrgList[0].Visibility)
|
||||
})
|
||||
|
||||
t.Run("OrgEdit", func(t *testing.T) {
|
||||
@@ -148,7 +148,7 @@ func testAPIOrgGeneral(t *testing.T) {
|
||||
Description: new("new description"),
|
||||
Website: new("https://org3-new-website.example.com"),
|
||||
Location: new("new location"),
|
||||
Visibility: new("limited"),
|
||||
Visibility: new(api.UserVisibilityLimited),
|
||||
Email: new("org3-new-email@example.com"),
|
||||
}
|
||||
req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &org3Edit).AddTokenAuth(user1Token)
|
||||
@@ -178,7 +178,7 @@ func testAPIOrgGeneral(t *testing.T) {
|
||||
|
||||
t.Run("OrgEditInvalidVisibility", func(t *testing.T) {
|
||||
org := api.EditOrgOption{
|
||||
Visibility: new("invalid-visibility"),
|
||||
Visibility: new(api.UserVisibility("invalid-visibility")),
|
||||
}
|
||||
req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &org).AddTokenAuth(user1Token)
|
||||
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
||||
|
||||
@@ -38,7 +38,7 @@ func TestAPIRepoCollaboratorPermission(t *testing.T) {
|
||||
|
||||
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
|
||||
|
||||
assert.Equal(t, "owner", repoPermission.Permission)
|
||||
assert.Equal(t, api.AccessLevelNameOwner, repoPermission.Permission)
|
||||
})
|
||||
|
||||
t.Run("CollaboratorWithReadAccess", func(t *testing.T) {
|
||||
@@ -50,7 +50,7 @@ func TestAPIRepoCollaboratorPermission(t *testing.T) {
|
||||
|
||||
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
|
||||
|
||||
assert.Equal(t, "read", repoPermission.Permission)
|
||||
assert.Equal(t, api.AccessLevelNameRead, repoPermission.Permission)
|
||||
})
|
||||
|
||||
t.Run("CollaboratorWithWriteAccess", func(t *testing.T) {
|
||||
@@ -62,7 +62,7 @@ func TestAPIRepoCollaboratorPermission(t *testing.T) {
|
||||
|
||||
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
|
||||
|
||||
assert.Equal(t, "write", repoPermission.Permission)
|
||||
assert.Equal(t, api.AccessLevelNameWrite, repoPermission.Permission)
|
||||
})
|
||||
|
||||
t.Run("CollaboratorWithAdminAccess", func(t *testing.T) {
|
||||
@@ -74,7 +74,7 @@ func TestAPIRepoCollaboratorPermission(t *testing.T) {
|
||||
|
||||
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
|
||||
|
||||
assert.Equal(t, "admin", repoPermission.Permission)
|
||||
assert.Equal(t, api.AccessLevelNameAdmin, repoPermission.Permission)
|
||||
})
|
||||
|
||||
t.Run("CollaboratorNotFound", func(t *testing.T) {
|
||||
@@ -101,7 +101,7 @@ func TestAPIRepoCollaboratorPermission(t *testing.T) {
|
||||
|
||||
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
|
||||
|
||||
assert.Equal(t, "read", repoPermission.Permission)
|
||||
assert.Equal(t, api.AccessLevelNameRead, repoPermission.Permission)
|
||||
|
||||
t.Run("CollaboratorCanReadOwnPermission", func(t *testing.T) {
|
||||
session := loginUser(t, user5.Name)
|
||||
@@ -112,7 +112,7 @@ func TestAPIRepoCollaboratorPermission(t *testing.T) {
|
||||
|
||||
repoCollPerm := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
|
||||
|
||||
assert.Equal(t, "read", repoCollPerm.Permission)
|
||||
assert.Equal(t, api.AccessLevelNameRead, repoCollPerm.Permission)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -128,7 +128,7 @@ func TestAPIRepoCollaboratorPermission(t *testing.T) {
|
||||
|
||||
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
|
||||
|
||||
assert.Equal(t, "read", repoPermission.Permission)
|
||||
assert.Equal(t, api.AccessLevelNameRead, repoPermission.Permission)
|
||||
})
|
||||
|
||||
t.Run("RepoAdminCanQueryACollaboratorsPermissions", func(t *testing.T) {
|
||||
@@ -144,6 +144,6 @@ func TestAPIRepoCollaboratorPermission(t *testing.T) {
|
||||
|
||||
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
|
||||
|
||||
assert.Equal(t, "read", repoPermission.Permission)
|
||||
assert.Equal(t, api.AccessLevelNameRead, repoPermission.Permission)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -39,12 +39,12 @@ func TestAPIRepoTeams(t *testing.T) {
|
||||
assert.Equal(t, "Owners", teams[0].Name)
|
||||
assert.True(t, teams[0].CanCreateOrgRepo)
|
||||
assert.True(t, util.SliceSortedEqual(unit.AllUnitKeyNames(), teams[0].Units), "%v == %v", unit.AllUnitKeyNames(), teams[0].Units)
|
||||
assert.Equal(t, "owner", teams[0].Permission)
|
||||
assert.Equal(t, api.AccessLevelNameOwner, teams[0].Permission)
|
||||
|
||||
assert.Equal(t, "test_team", teams[1].Name)
|
||||
assert.False(t, teams[1].CanCreateOrgRepo)
|
||||
assert.Equal(t, []string{"repo.issues"}, teams[1].Units)
|
||||
assert.Equal(t, "write", teams[1].Permission)
|
||||
assert.Equal(t, api.AccessLevelNameWrite, teams[1].Permission)
|
||||
}
|
||||
|
||||
// IsTeam
|
||||
|
||||
@@ -75,9 +75,9 @@ func TestAPITeam(t *testing.T) {
|
||||
resp = MakeRequest(t, req, http.StatusCreated)
|
||||
apiTeam = DecodeJSON(t, resp, &api.Team{})
|
||||
checkTeamResponse(t, "CreateTeam1", apiTeam, teamToCreate.Name, teamToCreate.Description, teamToCreate.IncludesAllRepositories,
|
||||
"none", teamToCreate.Units, nil)
|
||||
api.AccessLevelNameNone, teamToCreate.Units, nil)
|
||||
checkTeamBean(t, apiTeam.ID, teamToCreate.Name, teamToCreate.Description, teamToCreate.IncludesAllRepositories,
|
||||
"none", teamToCreate.Units, nil)
|
||||
api.AccessLevelNameNone, teamToCreate.Units, nil)
|
||||
teamID := apiTeam.ID
|
||||
|
||||
// Edit team.
|
||||
@@ -96,9 +96,9 @@ func TestAPITeam(t *testing.T) {
|
||||
resp = MakeRequest(t, req, http.StatusOK)
|
||||
apiTeam = DecodeJSON(t, resp, &api.Team{})
|
||||
checkTeamResponse(t, "EditTeam1", apiTeam, teamToEdit.Name, *teamToEdit.Description, *teamToEdit.IncludesAllRepositories,
|
||||
teamToEdit.Permission, unit.AllUnitKeyNames(), nil)
|
||||
api.AccessLevelName(teamToEdit.Permission), unit.AllUnitKeyNames(), nil)
|
||||
checkTeamBean(t, apiTeam.ID, teamToEdit.Name, *teamToEdit.Description, *teamToEdit.IncludesAllRepositories,
|
||||
teamToEdit.Permission, unit.AllUnitKeyNames(), nil)
|
||||
api.AccessLevelName(teamToEdit.Permission), unit.AllUnitKeyNames(), nil)
|
||||
|
||||
// Edit team Description only
|
||||
editDescription = "first team"
|
||||
@@ -108,9 +108,9 @@ func TestAPITeam(t *testing.T) {
|
||||
resp = MakeRequest(t, req, http.StatusOK)
|
||||
apiTeam = DecodeJSON(t, resp, &api.Team{})
|
||||
checkTeamResponse(t, "EditTeam1_DescOnly", apiTeam, teamToEdit.Name, *teamToEditDesc.Description, *teamToEdit.IncludesAllRepositories,
|
||||
teamToEdit.Permission, unit.AllUnitKeyNames(), nil)
|
||||
api.AccessLevelName(teamToEdit.Permission), unit.AllUnitKeyNames(), nil)
|
||||
checkTeamBean(t, apiTeam.ID, teamToEdit.Name, *teamToEditDesc.Description, *teamToEdit.IncludesAllRepositories,
|
||||
teamToEdit.Permission, unit.AllUnitKeyNames(), nil)
|
||||
api.AccessLevelName(teamToEdit.Permission), unit.AllUnitKeyNames(), nil)
|
||||
|
||||
// Read team.
|
||||
teamRead := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
|
||||
@@ -120,7 +120,7 @@ func TestAPITeam(t *testing.T) {
|
||||
resp = MakeRequest(t, req, http.StatusOK)
|
||||
apiTeam = DecodeJSON(t, resp, &api.Team{})
|
||||
checkTeamResponse(t, "ReadTeam1", apiTeam, teamRead.Name, *teamToEditDesc.Description, teamRead.IncludesAllRepositories,
|
||||
teamRead.AccessMode.ToString(), teamRead.GetUnitNames(), teamRead.GetUnitsMap())
|
||||
api.AccessLevelName(teamRead.AccessMode.ToString()), teamRead.GetUnitNames(), teamRead.GetUnitsMap())
|
||||
|
||||
// Delete team.
|
||||
req = NewRequestf(t, "DELETE", "/api/v1/teams/%d", teamID).
|
||||
@@ -142,9 +142,9 @@ func TestAPITeam(t *testing.T) {
|
||||
resp = MakeRequest(t, req, http.StatusCreated)
|
||||
apiTeam = DecodeJSON(t, resp, &api.Team{})
|
||||
checkTeamResponse(t, "CreateTeam2", apiTeam, teamToCreate.Name, teamToCreate.Description, teamToCreate.IncludesAllRepositories,
|
||||
"none", nil, teamToCreate.UnitsMap)
|
||||
api.AccessLevelNameNone, nil, teamToCreate.UnitsMap)
|
||||
checkTeamBean(t, apiTeam.ID, teamToCreate.Name, teamToCreate.Description, teamToCreate.IncludesAllRepositories,
|
||||
"none", nil, teamToCreate.UnitsMap)
|
||||
api.AccessLevelNameNone, nil, teamToCreate.UnitsMap)
|
||||
teamID = apiTeam.ID
|
||||
|
||||
// Edit team.
|
||||
@@ -163,9 +163,9 @@ func TestAPITeam(t *testing.T) {
|
||||
resp = MakeRequest(t, req, http.StatusOK)
|
||||
apiTeam = DecodeJSON(t, resp, &api.Team{})
|
||||
checkTeamResponse(t, "EditTeam2", apiTeam, teamToEdit.Name, *teamToEdit.Description, *teamToEdit.IncludesAllRepositories,
|
||||
"none", nil, teamToEdit.UnitsMap)
|
||||
api.AccessLevelNameNone, nil, teamToEdit.UnitsMap)
|
||||
checkTeamBean(t, apiTeam.ID, teamToEdit.Name, *teamToEdit.Description, *teamToEdit.IncludesAllRepositories,
|
||||
"none", nil, teamToEdit.UnitsMap)
|
||||
api.AccessLevelNameNone, nil, teamToEdit.UnitsMap)
|
||||
|
||||
// Edit team Description only
|
||||
editDescription = "second team"
|
||||
@@ -175,9 +175,9 @@ func TestAPITeam(t *testing.T) {
|
||||
resp = MakeRequest(t, req, http.StatusOK)
|
||||
apiTeam = DecodeJSON(t, resp, &api.Team{})
|
||||
checkTeamResponse(t, "EditTeam2_DescOnly", apiTeam, teamToEdit.Name, *teamToEditDesc.Description, *teamToEdit.IncludesAllRepositories,
|
||||
"none", nil, teamToEdit.UnitsMap)
|
||||
api.AccessLevelNameNone, nil, teamToEdit.UnitsMap)
|
||||
checkTeamBean(t, apiTeam.ID, teamToEdit.Name, *teamToEditDesc.Description, *teamToEdit.IncludesAllRepositories,
|
||||
"none", nil, teamToEdit.UnitsMap)
|
||||
api.AccessLevelNameNone, nil, teamToEdit.UnitsMap)
|
||||
|
||||
// Read team.
|
||||
teamRead = unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
|
||||
@@ -187,7 +187,7 @@ func TestAPITeam(t *testing.T) {
|
||||
apiTeam = DecodeJSON(t, resp, &api.Team{})
|
||||
assert.NoError(t, teamRead.LoadUnits(t.Context()))
|
||||
checkTeamResponse(t, "ReadTeam2", apiTeam, teamRead.Name, *teamToEditDesc.Description, teamRead.IncludesAllRepositories,
|
||||
teamRead.AccessMode.ToString(), teamRead.GetUnitNames(), teamRead.GetUnitsMap())
|
||||
api.AccessLevelName(teamRead.AccessMode.ToString()), teamRead.GetUnitNames(), teamRead.GetUnitsMap())
|
||||
|
||||
// Delete team.
|
||||
req = NewRequestf(t, "DELETE", "/api/v1/teams/%d", teamID).
|
||||
@@ -227,7 +227,7 @@ func TestAPITeam(t *testing.T) {
|
||||
unittest.AssertNotExistsBean(t, &organization.Team{ID: teamID})
|
||||
}
|
||||
|
||||
func checkTeamResponse(t *testing.T, testName string, apiTeam *api.Team, name, description string, includesAllRepositories bool, permission string, units []string, unitsMap map[string]string) {
|
||||
func checkTeamResponse(t *testing.T, testName string, apiTeam *api.Team, name, description string, includesAllRepositories bool, permission api.AccessLevelName, units []string, unitsMap map[string]string) {
|
||||
t.Run(testName, func(t *testing.T) {
|
||||
assert.Equal(t, name, apiTeam.Name, "name")
|
||||
assert.Equal(t, description, apiTeam.Description, "description")
|
||||
@@ -244,7 +244,7 @@ func checkTeamResponse(t *testing.T, testName string, apiTeam *api.Team, name, d
|
||||
})
|
||||
}
|
||||
|
||||
func checkTeamBean(t *testing.T, id int64, name, description string, includesAllRepositories bool, permission string, units []string, unitsMap map[string]string) {
|
||||
func checkTeamBean(t *testing.T, id int64, name, description string, includesAllRepositories bool, permission api.AccessLevelName, units []string, unitsMap map[string]string) {
|
||||
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: id})
|
||||
assert.NoError(t, team.LoadUnits(t.Context()), "LoadUnits")
|
||||
apiTeam, err := convert.ToTeam(t.Context(), team)
|
||||
|
||||
@@ -47,7 +47,7 @@ func TestAPIUserSearchLoggedIn(t *testing.T) {
|
||||
for _, user := range results.Data {
|
||||
assert.Contains(t, user.UserName, query)
|
||||
assert.NotEmpty(t, user.Email)
|
||||
assert.Equal(t, "public", user.Visibility)
|
||||
assert.Equal(t, api.UserVisibilityPublic, user.Visibility)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ func TestAPIUserSearchAdminLoggedInUserHidden(t *testing.T) {
|
||||
for _, user := range results.Data {
|
||||
assert.Contains(t, user.UserName, query)
|
||||
assert.NotEmpty(t, user.Email)
|
||||
assert.Equal(t, "private", user.Visibility)
|
||||
assert.Equal(t, api.UserVisibilityPrivate, user.Visibility)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -192,17 +192,21 @@ func testRenameInvalidUsername(t *testing.T) {
|
||||
}
|
||||
|
||||
func testRenameReservedUsername(t *testing.T) {
|
||||
reservedUsernames := []string{
|
||||
// ".", "..", ".well-known", // The names are not only reserved but also invalid
|
||||
// ".", "..", ".well-known" are also reserved but invalid as form input.
|
||||
reservedNames := []string{
|
||||
"api",
|
||||
"openapi3.v1.json",
|
||||
"swagger.v1.json",
|
||||
}
|
||||
patternNotAllowedNames := []string{
|
||||
"name.keys",
|
||||
}
|
||||
|
||||
session := loginUser(t, "user2")
|
||||
locale := translation.NewLocale("en-US")
|
||||
for _, reservedUsername := range reservedUsernames {
|
||||
check := func(name, msgKey string) {
|
||||
req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
|
||||
"name": reservedUsername,
|
||||
"name": name,
|
||||
"email": "user2@example.com",
|
||||
"language": "en-US",
|
||||
})
|
||||
@@ -212,12 +216,14 @@ func testRenameReservedUsername(t *testing.T) {
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
htmlDoc := NewHTMLParser(t, resp.Body)
|
||||
actualMsg := strings.TrimSpace(htmlDoc.doc.Find(".ui.negative.message").Text())
|
||||
expectedMsg := locale.TrString("user.form.name_reserved", reservedUsername)
|
||||
if strings.Contains(reservedUsername, ".") {
|
||||
expectedMsg = locale.TrString("user.form.name_pattern_not_allowed", reservedUsername)
|
||||
}
|
||||
assert.Equal(t, expectedMsg, actualMsg)
|
||||
unittest.AssertNotExistsBean(t, &user_model.User{Name: reservedUsername})
|
||||
assert.Equal(t, locale.TrString(msgKey, name), actualMsg)
|
||||
unittest.AssertNotExistsBean(t, &user_model.User{Name: name})
|
||||
}
|
||||
for _, name := range reservedNames {
|
||||
check(name, "user.form.name_reserved")
|
||||
}
|
||||
for _, name := range patternNotAllowedNames {
|
||||
check(name, "user.form.name_pattern_not_allowed")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user