Skip to content

Commit

Permalink
Only include schemaType and References when needed (#98)
Browse files Browse the repository at this point in the history
* Only use schemaType and refs in request when needed

* Improve unit tests for request marshalling

* add comment
  • Loading branch information
adamtabrams authored Mar 16, 2023
1 parent 0878dd9 commit 96a09e7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
10 changes: 8 additions & 2 deletions schemaRegistryClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ const (
)

func (s SchemaType) String() string {
// Avro is the default schemaType.
// Returning "" omits schemaType from the schemaRequest JSON for compatibility with older API versions.
if s == Avro {
return ""
}

return string(s)
}

Expand Down Expand Up @@ -130,8 +136,8 @@ type credentials struct {

type schemaRequest struct {
Schema string `json:"schema"`
SchemaType string `json:"schemaType"`
References []Reference `json:"references"`
SchemaType string `json:"schemaType,omitempty"`
References []Reference `json:"references,omitempty"`
}

type schemaResponse struct {
Expand Down
74 changes: 74 additions & 0 deletions schemaRegistryClient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,80 @@ func TestNewSchema(t *testing.T) {
}
}

func TestSchemaRequestMarshal(t *testing.T) {
tests := map[string]struct{
schema string
schemaType SchemaType
references []Reference
expected string
}{
"avro": {
schema: `test2`,
schemaType: Avro,
expected: `{"schema":"test2"}`,
},
"protobuf": {
schema: `test2`,
schemaType: Protobuf,
expected: `{"schema":"test2","schemaType":"PROTOBUF"}`,
},
"json": {
schema: `test2`,
schemaType: Json,
expected: `{"schema":"test2","schemaType":"JSON"}`,
},
"avro-empty-ref": {
schema: `test2`,
schemaType: Avro,
references: make([]Reference, 0),
expected: `{"schema":"test2"}`,
},
"protobuf-empty-ref": {
schema: `test2`,
schemaType: Protobuf,
references: make([]Reference, 0),
expected: `{"schema":"test2","schemaType":"PROTOBUF"}`,
},
"json-empty-ref": {
schema: `test2`,
schemaType: Json,
references: make([]Reference, 0),
expected: `{"schema":"test2","schemaType":"JSON"}`,
},
"avro-ref": {
schema: `test2`,
schemaType: Avro,
references: []Reference{{Name: "name1", Subject: "subject1", Version: 1}},
expected: `{"schema":"test2","references":[{"name":"name1","subject":"subject1","version":1}]}`,
},
"protobuf-ref": {
schema: `test2`,
schemaType: Protobuf,
references: []Reference{{Name: "name1", Subject: "subject1", Version: 1}},
expected: `{"schema":"test2","schemaType":"PROTOBUF","references":[{"name":"name1","subject":"subject1","version":1}]}`,
},
"json-ref": {
schema: `test2`,
schemaType: Json,
references: []Reference{{Name: "name1", Subject: "subject1", Version: 1}},
expected: `{"schema":"test2","schemaType":"JSON","references":[{"name":"name1","subject":"subject1","version":1}]}`,
},
}

for name, testData := range tests {
t.Run(name, func(t *testing.T) {
schemaReq := schemaRequest{
Schema: testData.schema,
SchemaType: testData.schemaType.String(),
References: testData.references,
}
actual, err := json.Marshal(schemaReq)
assert.NoError(t, err)
assert.Equal(t, testData.expected, string(actual))
})
}
}

func mockServerFromSubjectVersionPairWithSchemaResponse(t *testing.T, subject, version string, schemaResponse schemaResponse) (*httptest.Server, *int) {
return mockServerWithSchemaResponse(t, fmt.Sprintf("/subjects/%s/versions/%s", subject, version), schemaResponse)
}
Expand Down

0 comments on commit 96a09e7

Please sign in to comment.