@@ -93,13 +93,13 @@ type (
93
93
// a Kafka topic, and whether this is for a key or value. For example,
94
94
// "foo-key" would be the subject for the foo topic for serializing the
95
95
// key field of a record.
96
- Subject string `json:"subject"`
96
+ Subject string `json:"subject,omitempty "`
97
97
98
98
// Version is the version of this subject.
99
- Version int `json:"version"`
99
+ Version int `json:"version,omitempty "`
100
100
101
101
// ID is the globally unique ID of the schema.
102
- ID int `json:"id"`
102
+ ID int `json:"id,omitempty "`
103
103
104
104
Schema
105
105
}
@@ -352,18 +352,38 @@ func (cl *Client) Schemas(ctx context.Context, subject string) ([]SubjectSchema,
352
352
//
353
353
// This supports param [Normalize].
354
354
func (cl * Client ) CreateSchema (ctx context.Context , subject string , s Schema ) (SubjectSchema , error ) {
355
+ return cl .CreateSchemaWithIDAndVersion (ctx , subject , s , - 1 , - 1 )
356
+ }
357
+
358
+ // CreateSchemaWithIDAndVersion attempts to create a schema with a fixed ID and
359
+ // version ID in the given subject. If the id is set to -1 or 0, this method is
360
+ // equivalent to CreateSchema(). If the versionID is set to -1 or 0, it will be
361
+ // omitted when creating the schema.
362
+ //
363
+ // This supports param [Normalize].
364
+ func (cl * Client ) CreateSchemaWithIDAndVersion (ctx context.Context , subject string , s Schema , id , versionID int ) (SubjectSchema , error ) {
355
365
// POST /subjects/{subject}/versions => returns ID
356
366
// Newer SR returns the full SubjectSchema, but old does not, so we
357
367
// re-request to find the full information.
358
368
path := pathSubjectWithVersion (subject )
359
- var id struct {
369
+ var into struct {
360
370
ID int `json:"id"`
361
371
}
362
- if err := cl .post (ctx , path , s , & id ); err != nil {
363
- return SubjectSchema {}, err
372
+ if id == - 1 {
373
+ if err := cl .post (ctx , path , s , & into ); err != nil {
374
+ return SubjectSchema {}, err
375
+ }
376
+ } else {
377
+ ss := SubjectSchema {Schema : s , ID : id }
378
+ if versionID != - 1 {
379
+ ss .Version = versionID
380
+ }
381
+ if err := cl .post (ctx , path , ss , & into ); err != nil {
382
+ return SubjectSchema {}, err
383
+ }
364
384
}
365
385
366
- usages , err := cl .SchemaUsagesByID (ctx , id .ID )
386
+ usages , err := cl .SchemaUsagesByID (ctx , into .ID )
367
387
if err != nil {
368
388
return SubjectSchema {}, err
369
389
}
@@ -372,7 +392,7 @@ func (cl *Client) CreateSchema(ctx context.Context, subject string, s Schema) (S
372
392
return usage , nil
373
393
}
374
394
}
375
- return SubjectSchema {}, fmt .Errorf ("created schema under id %d, but unable to find SubjectSchema" , id .ID )
395
+ return SubjectSchema {}, fmt .Errorf ("created schema under id %d, but unable to find SubjectSchema" , into .ID )
376
396
}
377
397
378
398
// LookupSchema checks to see if a schema is already registered and if so,
0 commit comments