Skip to content

Commit fed30be

Browse files
Fix json validation during serialization (#1101)
* chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * Fix JSON validation during serialization --------- Co-authored-by: Confluent Jenkins Bot <[email protected]>
1 parent 5f4c973 commit fed30be

File tree

6 files changed

+72
-12
lines changed

6 files changed

+72
-12
lines changed

schemaregistry/serde/avro/avro_generic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (s *GenericSerializer) Serialize(topic string, msg interface{}) ([]byte, er
6767
info := schemaregistry.SchemaInfo{
6868
Schema: avroType.String(),
6969
}
70-
id, err := s.GetID(topic, msg, info)
70+
id, err := s.GetID(topic, msg, &info)
7171
if err != nil {
7272
return nil, err
7373
}

schemaregistry/serde/avro/avro_specific.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (s *SpecificSerializer) Serialize(topic string, msg interface{}) ([]byte, e
7676
info := schemaregistry.SchemaInfo{
7777
Schema: avroMsg.Schema(),
7878
}
79-
id, err := s.GetID(topic, avroMsg, info)
79+
id, err := s.GetID(topic, avroMsg, &info)
8080
if err != nil {
8181
return nil, err
8282
}

schemaregistry/serde/jsonschema/json_schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (s *Serializer) Serialize(topic string, msg interface{}) ([]byte, error) {
6868
Schema: string(raw),
6969
SchemaType: "JSON",
7070
}
71-
id, err := s.GetID(topic, msg, info)
71+
id, err := s.GetID(topic, msg, &info)
7272
if err != nil {
7373
return nil, err
7474
}

schemaregistry/serde/jsonschema/json_schema_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package jsonschema
1818

1919
import (
20+
"encoding/json"
21+
"github.com/invopop/jsonschema"
22+
"strings"
2023
"testing"
2124

2225
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry"
@@ -85,6 +88,63 @@ func TestJSONSchemaSerdeWithNested(t *testing.T) {
8588
serde.MaybeFail("deserialization", err, serde.Expect(newobj, obj))
8689
}
8790

91+
func TestFailingJSONSchemaValidationWithSimple(t *testing.T) {
92+
serde.MaybeFail = serde.InitFailFunc(t)
93+
var err error
94+
conf := schemaregistry.NewConfig("mock://")
95+
96+
client, err := schemaregistry.NewClient(conf)
97+
serde.MaybeFail("Schema Registry configuration", err)
98+
99+
serConfig := NewSerializerConfig()
100+
serConfig.EnableValidation = true
101+
// We don't want to risk registering one instead of using the already registered one
102+
serConfig.AutoRegisterSchemas = false
103+
serConfig.UseLatestVersion = true
104+
ser, err := NewSerializer(client, serde.ValueSerde, serConfig)
105+
serde.MaybeFail("Serializer configuration", err)
106+
107+
obj := JSONDemoSchema{}
108+
jschema := jsonschema.Reflect(obj)
109+
raw, err := json.Marshal(jschema)
110+
serde.MaybeFail("Schema marshalling", err)
111+
info := schemaregistry.SchemaInfo{
112+
Schema: string(raw),
113+
SchemaType: "JSON",
114+
}
115+
116+
id, err := client.Register("topic1-value", info, false)
117+
serde.MaybeFail("Schema registration", err)
118+
if id <= 0 {
119+
t.Errorf("Expected valid schema id, found %d", id)
120+
}
121+
122+
_, err = ser.Serialize("topic1", &obj)
123+
if err != nil {
124+
t.Errorf("Expected no validation error, found %s", err)
125+
}
126+
127+
diffObj := DifferentJSONDemoSchema{}
128+
_, err = ser.Serialize("topic1", &diffObj)
129+
if err == nil || !strings.Contains(err.Error(), "jsonschema") {
130+
t.Errorf("Expected validation error, found %s", err)
131+
}
132+
}
133+
134+
type DifferentJSONDemoSchema struct {
135+
IntField int32 `json:"IntField"`
136+
137+
ExtraStringField string `json:"ExtraStringField"`
138+
139+
DoubleField float64 `json:"DoubleField"`
140+
141+
StringField string `json:"StringField"`
142+
143+
BoolFieldThatsActuallyString string `json:"BoolField"`
144+
145+
BytesField test.Bytes `json:"BytesField"`
146+
}
147+
88148
type JSONDemoSchema struct {
89149
IntField int32 `json:"IntField"`
90150

schemaregistry/serde/protobuf/protobuf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func (s *Serializer) Serialize(topic string, msg interface{}) ([]byte, error) {
177177
SchemaType: metadata.SchemaType,
178178
References: metadata.References,
179179
}
180-
id, err := s.GetID(topic, protoMsg, info)
180+
id, err := s.GetID(topic, protoMsg, &info)
181181
if err != nil {
182182
return nil, err
183183
}

schemaregistry/serde/serde.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,28 @@ func TopicNameStrategy(topic string, serdeType Type, schema schemaregistry.Schem
128128
}
129129

130130
// GetID returns a schema ID for the given schema
131-
func (s *BaseSerializer) GetID(topic string, msg interface{}, info schemaregistry.SchemaInfo) (int, error) {
131+
func (s *BaseSerializer) GetID(topic string, msg interface{}, info *schemaregistry.SchemaInfo) (int, error) {
132132
autoRegister := s.Conf.AutoRegisterSchemas
133133
useSchemaID := s.Conf.UseSchemaID
134134
useLatest := s.Conf.UseLatestVersion
135135
normalizeSchema := s.Conf.NormalizeSchemas
136136

137137
var id = -1
138-
subject, err := s.SubjectNameStrategy(topic, s.SerdeType, info)
138+
subject, err := s.SubjectNameStrategy(topic, s.SerdeType, *info)
139139
if err != nil {
140140
return -1, err
141141
}
142142
if autoRegister {
143-
id, err = s.Client.Register(subject, info, normalizeSchema)
143+
id, err = s.Client.Register(subject, *info, normalizeSchema)
144144
if err != nil {
145145
return -1, err
146146
}
147147
} else if useSchemaID >= 0 {
148-
info, err = s.Client.GetBySubjectAndID(subject, useSchemaID)
148+
*info, err = s.Client.GetBySubjectAndID(subject, useSchemaID)
149149
if err != nil {
150150
return -1, err
151151
}
152-
id, err = s.Client.GetID(subject, info, false)
152+
id, err = s.Client.GetID(subject, *info, false)
153153
if err != nil {
154154
return -1, err
155155
}
@@ -161,17 +161,17 @@ func (s *BaseSerializer) GetID(topic string, msg interface{}, info schemaregistr
161161
if err != nil {
162162
return -1, err
163163
}
164-
info = schemaregistry.SchemaInfo{
164+
*info = schemaregistry.SchemaInfo{
165165
Schema: metadata.Schema,
166166
SchemaType: metadata.SchemaType,
167167
References: metadata.References,
168168
}
169-
id, err = s.Client.GetID(subject, info, false)
169+
id, err = s.Client.GetID(subject, *info, false)
170170
if err != nil {
171171
return -1, err
172172
}
173173
} else {
174-
id, err = s.Client.GetID(subject, info, normalizeSchema)
174+
id, err = s.Client.GetID(subject, *info, normalizeSchema)
175175
if err != nil {
176176
return -1, err
177177
}

0 commit comments

Comments
 (0)