Add "_self" suffix for repository API to avoid conflict

Add "_self" suffix for repository API to avoid conflict

Signed-off-by: Wenkai Yin <yinw@vmware.com>
This commit is contained in:
Wenkai Yin 2020-03-20 17:08:08 +08:00
parent 63cf1fce7f
commit dca06b6ede
3 changed files with 11 additions and 7 deletions

View File

@ -54,7 +54,11 @@ paths:
$ref: '#/responses/404' $ref: '#/responses/404'
'500': '500':
$ref: '#/responses/500' $ref: '#/responses/500'
/projects/{project_name}/repositories/{repository_name}: # the _self suffix here is used to avoid the conflict of repository name and URL path
# e.g. the repository name can be "library/artifacts", we cannot distinguish the URL
# "GET /projects/{project_name}/repositories/library/artifacts" is getting repository
# or listing artifacts
/projects/{project_name}/repositories/{repository_name}/_self:
get: get:
summary: Get repository summary: Get repository
description: Get the repository specified by name description: Get the repository specified by name

View File

@ -25,9 +25,9 @@ import (
var ( var (
defaultRegexps = []*regexp.Regexp{ defaultRegexps = []*regexp.Regexp{
regexp.MustCompile(`^/api/` + api.APIVersion + `/projects/.*/repositories/(.*)/_self/?$`),
regexp.MustCompile(`^/api/` + api.APIVersion + `/projects/.*/repositories/(.*)/artifacts/?$`), regexp.MustCompile(`^/api/` + api.APIVersion + `/projects/.*/repositories/(.*)/artifacts/?$`),
regexp.MustCompile(`^/api/` + api.APIVersion + `/projects/.*/repositories/(.*)/artifacts/.*$`), regexp.MustCompile(`^/api/` + api.APIVersion + `/projects/.*/repositories/(.*)/artifacts/.*$`),
regexp.MustCompile(`^/api/` + api.APIVersion + `/projects/.*/repositories/(.*)/?$`),
} }
) )

View File

@ -55,9 +55,9 @@ func Test_escape(t *testing.T) {
"/api/v2.0/projects/library/repositories", "/api/v2.0/projects/library/repositories",
}, },
{ {
"/api/v2.0/projects/library/repositories/hello/mariadb", "/api/v2.0/projects/library/repositories/hello/mariadb/_self",
args{regexp.MustCompile(`^/api/v2.0/projects/.*/repositories/(.*)`), "/api/v2.0/projects/library/repositories/hello/mariadb"}, args{regexp.MustCompile(`^/api/v2.0/projects/.*/repositories/(.*)/_self`), "/api/v2.0/projects/library/repositories/hello/mariadb/_self"},
"/api/v2.0/projects/library/repositories/hello%2Fmariadb", "/api/v2.0/projects/library/repositories/hello%2Fmariadb/_self",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@ -70,11 +70,11 @@ func Test_escape(t *testing.T) {
} }
func TestEscapeMiddleware(t *testing.T) { func TestEscapeMiddleware(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/api/v2.0/projects/library/repositories/hello/mariadb", nil) r := httptest.NewRequest(http.MethodGet, "/api/v2.0/projects/library/repositories/hello/mariadb/_self", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v2.0/projects/library/repositories/hello%2Fmariadb" { if r.URL.Path != "/api/v2.0/projects/library/repositories/hello%2Fmariadb/_self" {
t.Errorf("escape middleware failed") t.Errorf("escape middleware failed")
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)