@@ -234,6 +234,75 @@ const (
234
234
"title" : "Sample Event",
235
235
"type" : "object"
236
236
}
237
+ `
238
+ messageSchema = `
239
+ {
240
+
241
+ "type": "object",
242
+ "properties": {
243
+ "messageType": {
244
+ "type": "string"
245
+ },
246
+ "version": {
247
+ "type": "string"
248
+ },
249
+ "payload": {
250
+ "type": "object",
251
+ "oneOf": [
252
+ {
253
+ "$ref": "#/$defs/authentication_request"
254
+ },
255
+ {
256
+ "$ref": "#/$defs/authentication_status"
257
+ }
258
+ ]
259
+ }
260
+ },
261
+ "required": [
262
+ "payload",
263
+ "messageType",
264
+ "version"
265
+ ],
266
+ "$defs": {
267
+ "authentication_request": {
268
+ "properties": {
269
+ "messageId": {
270
+ "type": "string",
271
+ "confluent:tags": ["PII"]
272
+ },
273
+ "timestamp": {
274
+ "type": "integer",
275
+ "minimum": 0
276
+ },
277
+ "requestId": {
278
+ "type": "string"
279
+ }
280
+ },
281
+ "required": [
282
+ "messageId",
283
+ "timestamp"
284
+ ]
285
+ },
286
+ "authentication_status": {
287
+ "properties": {
288
+ "messageId": {
289
+ "type": "string",
290
+ "confluent:tags": ["PII"]
291
+ },
292
+ "authType": {
293
+ "type": [
294
+ "string",
295
+ "null"
296
+ ]
297
+ }
298
+ },
299
+ "required": [
300
+ "messageId",
301
+ "authType"
302
+ ]
303
+ }
304
+ }
305
+ }
237
306
`
238
307
)
239
308
@@ -756,6 +825,70 @@ func TestJSONSchemaSerdeWithCELFieldTransformWithNullable(t *testing.T) {
756
825
serde .MaybeFail ("deserialization" , err , serde .Expect (& newobj , & obj2 ))
757
826
}
758
827
828
+ func TestJSONSchemaSerdeWithCELFieldTransformWithUnionOfRefs (t * testing.T ) {
829
+ serde .MaybeFail = serde .InitFailFunc (t )
830
+ var err error
831
+
832
+ conf := schemaregistry .NewConfig ("mock://" )
833
+
834
+ client , err := schemaregistry .NewClient (conf )
835
+ serde .MaybeFail ("Schema Registry configuration" , err )
836
+
837
+ serConfig := NewSerializerConfig ()
838
+ serConfig .AutoRegisterSchemas = false
839
+ serConfig .UseLatestVersion = true
840
+ ser , err := NewSerializer (client , serde .ValueSerde , serConfig )
841
+ serde .MaybeFail ("Serializer configuration" , err )
842
+
843
+ encRule := schemaregistry.Rule {
844
+ Name : "test-cel" ,
845
+ Kind : "TRANSFORM" ,
846
+ Mode : "WRITE" ,
847
+ Type : "CEL_FIELD" ,
848
+ Expr : "name == 'messageId' ; value + '-suffix'" ,
849
+ }
850
+ ruleSet := schemaregistry.RuleSet {
851
+ DomainRules : []schemaregistry.Rule {encRule },
852
+ }
853
+
854
+ info := schemaregistry.SchemaInfo {
855
+ Schema : messageSchema ,
856
+ SchemaType : "JSON" ,
857
+ RuleSet : & ruleSet ,
858
+ }
859
+
860
+ id , err := client .Register ("topic1-value" , info , false )
861
+ serde .MaybeFail ("Schema registration" , err )
862
+ if id <= 0 {
863
+ t .Errorf ("Expected valid schema id, found %d" , id )
864
+ }
865
+
866
+ payload := Payload {}
867
+ payload .MessageID = "12345"
868
+ payload .Timestamp = 12345
869
+ obj := Message {}
870
+ obj .MessageType = "authentication_request"
871
+ obj .Version = "1.0"
872
+ obj .Payload = payload
873
+
874
+ bytes , err := ser .Serialize ("topic1" , & obj )
875
+ serde .MaybeFail ("serialization" , err )
876
+
877
+ deserConfig := NewDeserializerConfig ()
878
+ deser , err := NewDeserializer (client , serde .ValueSerde , deserConfig )
879
+ serde .MaybeFail ("Deserializer configuration" , err )
880
+ deser .Client = ser .Client
881
+
882
+ payload2 := Payload {}
883
+ payload2 .MessageID = "12345-suffix"
884
+ payload2 .Timestamp = 12345
885
+ obj .Payload = payload2
886
+
887
+ var newobj Message
888
+ err = deser .DeserializeInto ("topic1" , bytes , & newobj )
889
+ serde .MaybeFail ("deserialization" , err , serde .Expect (& newobj , & obj ))
890
+ }
891
+
759
892
func TestJSONSchemaSerdeWithCELFieldTransformWithDef (t * testing.T ) {
760
893
serde .MaybeFail = serde .InitFailFunc (t )
761
894
var err error
@@ -1896,3 +2029,17 @@ type JSONPerson struct {
1896
2029
1897
2030
Address Address `json:"address"`
1898
2031
}
2032
+
2033
+ type Message struct {
2034
+ MessageType string `json:"messageType"`
2035
+
2036
+ Version string `json:"version"`
2037
+
2038
+ Payload Payload `json:"payload"`
2039
+ }
2040
+
2041
+ type Payload struct {
2042
+ MessageID string `json:"messageId"`
2043
+
2044
+ Timestamp int `json:"timestamp"`
2045
+ }
0 commit comments