Skip to content

Commit c5f5d2c

Browse files
committed
sr: match context-prefixed subjects in CreateSchemaWithIDAndVersion
When using schema contexts, the registry returns subjects with a context prefix (e.g. ":.myctx:subject") in usages. Match against both plain and prefixed forms so CreateSchema succeeds under a schema context.
1 parent 71596db commit c5f5d2c

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

pkg/sr/api.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,20 @@ func (cl *Client) CreateSchemaWithIDAndVersion(ctx context.Context, subject stri
425425
if err != nil {
426426
return SubjectSchema{}, err
427427
}
428+
// When using schema contexts, the registry stores subjects with a
429+
// context prefix (e.g. ":.myctx:subject"). We need to match against
430+
// both the plain subject and the context-prefixed form.
431+
ctxName, _ := ctx.Value(&schemaContextKey).(string)
432+
if ctxName == "" {
433+
ctxName = cl.defContext
434+
}
428435
for _, usage := range usages {
429436
if usage.Subject == subject {
430437
return usage, nil
431438
}
439+
if ctxName != "" && usage.Subject == ":"+ctxName+":"+subject {
440+
return usage, nil
441+
}
432442
}
433443
return SubjectSchema{}, fmt.Errorf("created schema under id %d, but unable to find SubjectSchema", sid)
434444
}

pkg/sr/client_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,74 @@ func TestOptValues(t *testing.T) {
519519
}
520520
})
521521
}
522+
523+
// TestCreateSchemaWithContext tests that CreateSchema correctly matches
524+
// context-prefixed subjects returned by the registry.
525+
func TestCreateSchemaWithContext(t *testing.T) {
526+
dummySchema := Schema{Schema: `{"type":"string"}`}
527+
528+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
529+
path := r.URL.EscapedPath()
530+
var output any
531+
switch {
532+
case path == "/contexts/.myctx/subjects/foo/versions" && r.Method == http.MethodPost:
533+
output = map[string]int{"id": 1}
534+
case path == "/contexts/.myctx/schemas/ids/1/versions" && r.Method == http.MethodGet:
535+
// The real registry returns subjects with context prefix.
536+
output = []map[string]any{{"subject": ":.myctx:foo", "version": 1}}
537+
case path == "/contexts/.myctx/subjects/:.myctx:foo/versions/1" && r.Method == http.MethodGet:
538+
output = SubjectSchema{
539+
Subject: ":.myctx:foo",
540+
Version: 1,
541+
ID: 1,
542+
Schema: dummySchema,
543+
}
544+
default:
545+
http.Error(w, fmt.Sprintf("path not found: %s (method: %s)", path, r.Method), http.StatusNotFound)
546+
return
547+
}
548+
b, err := json.Marshal(output)
549+
if err != nil {
550+
http.Error(w, err.Error(), http.StatusInternalServerError)
551+
return
552+
}
553+
w.Write(b)
554+
}))
555+
t.Cleanup(ts.Close)
556+
557+
ctx := context.Background()
558+
559+
t.Run("WithSchemaContext", func(t *testing.T) {
560+
c, err := NewClient(URLs(ts.URL))
561+
if err != nil {
562+
t.Fatalf("unable to create client: %s", err)
563+
}
564+
ss, err := c.CreateSchema(WithSchemaContext(ctx, ".myctx"), "foo", dummySchema)
565+
if err != nil {
566+
t.Fatalf("CreateSchema with WithSchemaContext failed: %s", err)
567+
}
568+
if ss.Subject != ":.myctx:foo" {
569+
t.Errorf("expected subject %q, got %q", ":.myctx:foo", ss.Subject)
570+
}
571+
if ss.ID != 1 {
572+
t.Errorf("expected ID 1, got %d", ss.ID)
573+
}
574+
})
575+
576+
t.Run("DefaultSchemaContext", func(t *testing.T) {
577+
c, err := NewClient(URLs(ts.URL), DefaultSchemaContext(".myctx"))
578+
if err != nil {
579+
t.Fatalf("unable to create client: %s", err)
580+
}
581+
ss, err := c.CreateSchema(ctx, "foo", dummySchema)
582+
if err != nil {
583+
t.Fatalf("CreateSchema with DefaultSchemaContext failed: %s", err)
584+
}
585+
if ss.Subject != ":.myctx:foo" {
586+
t.Errorf("expected subject %q, got %q", ":.myctx:foo", ss.Subject)
587+
}
588+
if ss.ID != 1 {
589+
t.Errorf("expected ID 1, got %d", ss.ID)
590+
}
591+
})
592+
}

0 commit comments

Comments
 (0)