diff --git a/pkg/sr/api.go b/pkg/sr/api.go index 3f4356c4..8b4698e3 100644 --- a/pkg/sr/api.go +++ b/pkg/sr/api.go @@ -352,18 +352,32 @@ func (cl *Client) Schemas(ctx context.Context, subject string) ([]SubjectSchema, // // This supports param [Normalize]. func (cl *Client) CreateSchema(ctx context.Context, subject string, s Schema) (SubjectSchema, error) { + return cl.CreateSchemaWithID(ctx, subject, s, -1) +} + +// CreateSchemaWithID attempts to create a schema with a fixed ID in the given +// subject. If the id is set to -1, this method is equivalent to CreateSchema(). +// +// This supports param [Normalize]. +func (cl *Client) CreateSchemaWithID(ctx context.Context, subject string, s Schema, id int) (SubjectSchema, error) { // POST /subjects/{subject}/versions => returns ID // Newer SR returns the full SubjectSchema, but old does not, so we // re-request to find the full information. path := pathSubjectWithVersion(subject) - var id struct { + var into struct { ID int `json:"id"` } - if err := cl.post(ctx, path, s, &id); err != nil { - return SubjectSchema{}, err + if id == -1 { + if err := cl.post(ctx, path, s, &into); err != nil { + return SubjectSchema{}, err + } + } else { + if err := cl.post(ctx, path, SubjectSchema{Schema: s, ID: id}, &into); err != nil { + return SubjectSchema{}, err + } } - usages, err := cl.SchemaUsagesByID(ctx, id.ID) + usages, err := cl.SchemaUsagesByID(ctx, into.ID) if err != nil { return SubjectSchema{}, err } @@ -372,7 +386,7 @@ func (cl *Client) CreateSchema(ctx context.Context, subject string, s Schema) (S return usage, nil } } - return SubjectSchema{}, fmt.Errorf("created schema under id %d, but unable to find SubjectSchema", id.ID) + return SubjectSchema{}, fmt.Errorf("created schema under id %d, but unable to find SubjectSchema", into.ID) } // LookupSchema checks to see if a schema is already registered and if so,