diff --git a/schema/handler.go b/schema/handler.go index 0f60259bbcd..3639b214780 100644 --- a/schema/handler.go +++ b/schema/handler.go @@ -90,7 +90,12 @@ func (h *Handler) getByID(w http.ResponseWriter, r *http.Request, ps httprouter. return } - s, err := ss.GetByID(ps.ByName("id")) + id := ps.ByName("id") + if dec, err := base64.RawURLEncoding.DecodeString(ps.ByName("id")); err == nil { + id = string(dec) + } + + s, err := ss.GetByID(id) if err != nil { h.r.Writer().WriteError(w, r, errors.WithStack(herodot.ErrNotFound.WithDebugf("%+v", err))) return diff --git a/schema/handler_test.go b/schema/handler_test.go index 922e4f18381..41afc38d74b 100644 --- a/schema/handler_test.go +++ b/schema/handler_test.go @@ -61,6 +61,11 @@ func TestHandler(t *testing.T) { URL: urlx.ParseOrPanic("file://./stub"), RawURL: "file://./stub", }, + { + ID: "preset://email", + URL: urlx.ParseOrPanic("file://./stub/identity-2.schema.json"), + RawURL: "file://./stub/identity-2.schema.json", + }, } getSchemaById := func(id string) *schema.Schema { @@ -137,6 +142,12 @@ func TestHandler(t *testing.T) { require.JSONEq(t, string(file), string(server)) }) + t.Run("case=get encoded schema", func(t *testing.T) { + server := getFromTSById("cHJlc2V0Oi8vZW1haWw", http.StatusOK) + file := getFromFS("preset://email") + require.JSONEq(t, string(file), string(server)) + }) + t.Run("case=get unreachable schema", func(t *testing.T) { reason := getFromTSById("unreachable", http.StatusInternalServerError) require.Contains(t, string(reason), "could not be found or opened") diff --git a/schema/schema.go b/schema/schema.go index e5cb2a531d9..80d93e96207 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -2,6 +2,7 @@ package schema import ( "context" + "encoding/base64" "io/ioutil" "net/url" "strings" @@ -100,5 +101,5 @@ type Schema struct { } func (s *Schema) SchemaURL(host *url.URL) *url.URL { - return urlx.AppendPaths(host, SchemasPath, s.ID) + return urlx.AppendPaths(host, SchemasPath, base64.RawURLEncoding.EncodeToString([]byte(s.ID))) }