Skip to content

sr: add schema context support and generic context handling in srfake#1267

Open
c-julin wants to merge 3 commits intotwmb:masterfrom
c-julin:sr-context-middleware
Open

sr: add schema context support and generic context handling in srfake#1267
c-julin wants to merge 3 commits intotwmb:masterfrom
c-julin:sr-context-middleware

Conversation

@c-julin
Copy link

@c-julin c-julin commented Feb 23, 2026

Summary

  • Add WithSchemaContext / DefaultSchemaContext to scope any sr.Client request to a schema registry context by prepending /contexts/{name} to paths
  • Add new API methods: Contexts, DeleteContext
  • Add ContextPrefix, Offset, and Limit query parameters
  • Update srfake with a generic context-stripping middleware, so all existing endpoints work under /contexts/{name}/...

Test plan

  • go test ./... -count=1 -race passes in pkg/sr
  • New tests for previously-unsupported context-prefixed operations (register, list versions, get version, delete subject, get config, get raw schema)
  • New end-to-end test for CreateSchema with WithSchemaContext

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.
@c-julin c-julin force-pushed the sr-context-middleware branch from c5f5d2c to 1e1da70 Compare February 23, 2026 19:02
}

// DeleteContext deletes a context from the schema registry.
func (cl *Client) DeleteContext(ctx context.Context, context_ string) error {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix arg name => context_

}
// When using schema contexts, the registry stores subjects with a
// context prefix (e.g. ":.myctx:subject"). We need to match against
// both the plain subject and the context-prefixed form.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is universal? Meaning if I opt into using contexts, ALL schemas are now prefixed with the context?

// Offset returns a Param that sets the pagination offset.
func Offset(o int) Param { return Param{offset: o} }

// Limit returns a Param that sets the maximum number of results to return.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did I just ... not have this before? even though I had it above in merge / struct def? weird.

}}
}

// DefaultSchemaContext sets a default schema context applied to every request.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sets a default schema registry context (namespacing) applied to every request

trying to be a bit more clear since SR contexts and Go contexts can be confusing when used in the same space


// WithSchemaContext returns a context that scopes schema registry requests
// to the given context name. For client-wide scoping, use [DefaultSchemaContext].
func WithSchemaContext(ctx context.Context, name string) context.Context {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// InContext scopes requests to be issued to the given schema registry context (namespacing).
// For client wide scoping, use [DefaultSchemaContext]. The following will return the subjects
// from the "foo" context:
//
//         cl.Subjects(sr.InContext(ctx, "foo"))

also wdyt of InContext, or WithContext` as the name? Maybe With is better, it's more standard. I think dropping "Schema" though helps to keep it shorter. "In" for some reason to my brain separates it from Go contexts, whereas WithContext kinda just still sounds like with a context.Context (which then means you need the "Schema" => WithSchemaContext).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants