Skip to content

Commit 97cdbd6

Browse files
authored
Add test for JSON with definition (#2363)
1 parent d0f8753 commit 97cdbd6

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

test/Confluent.SchemaRegistry.Serdes.UnitTests/JsonSerializeDeserialize.cs

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,12 +577,12 @@ public void CELFieldTransform()
577577
schema.RuleSet = new RuleSet(new List<Rule>(),
578578
new List<Rule>
579579
{
580-
new Rule("testCEL", RuleKind.Transform, RuleMode.Write, "CEL_FIELD", null, null,
580+
new Rule("testCEL", RuleKind.Transform, RuleMode.Write, "CEL_FIELD", null, null,
581581
"typeName == 'STRING' ; value + '-suffix'", null, null, false)
582582
}
583583
);
584584
store[schemaStr] = 1;
585-
subjectStore["topic-value"] = new List<RegisteredSchema> { schema };
585+
subjectStore["topic-value"] = new List<RegisteredSchema> { schema };
586586
var config = new JsonSerializerConfig
587587
{
588588
AutoRegisterSchemas = false,
@@ -607,6 +607,80 @@ public void CELFieldTransform()
607607
Assert.Equal(user.FavoriteNumber, result.FavoriteNumber);
608608
}
609609

610+
[Fact]
611+
public void CELFieldTransformWithDef()
612+
{
613+
var schemaStr = @"{
614+
""$schema"" : ""http://json-schema.org/draft-07/schema#"",
615+
""additionalProperties"" : false,
616+
""definitions"" : {
617+
""Address"" : {
618+
""additionalProperties"" : false,
619+
""properties"" : {
620+
""DoorNumber"" : {
621+
""type"" : ""integer""
622+
},
623+
""DoorPin"" : {
624+
""confluent:tags"" : [ ""PII"" ],
625+
""type"" : ""string""
626+
}
627+
},
628+
""type"" : ""object""
629+
}
630+
},
631+
""properties"" : {
632+
""Address"" : {
633+
""$ref"" : ""#/definitions/Address""
634+
},
635+
""Name"" : {
636+
""confluent:tags"" : [ ""PII"" ],
637+
""type"" : ""string""
638+
}
639+
},
640+
""title"" : ""Sample Event"",
641+
""type"" : ""object""
642+
}";
643+
var schema = new RegisteredSchema("topic-value", 1, 1, schemaStr, SchemaType.Json, null);
644+
schema.RuleSet = new RuleSet(new List<Rule>(),
645+
new List<Rule>
646+
{
647+
new Rule("testCEL", RuleKind.Transform, RuleMode.Write, "CEL_FIELD", new HashSet<string>
648+
{
649+
"PII"
650+
651+
}, null,
652+
"value + '-suffix'", null, null, false)
653+
}
654+
);
655+
store[schemaStr] = 1;
656+
subjectStore["topic-value"] = new List<RegisteredSchema> { schema };
657+
var config = new JsonSerializerConfig
658+
{
659+
AutoRegisterSchemas = false,
660+
UseLatestVersion = true
661+
};
662+
var serializer = new JsonSerializer<JsonPerson>(schemaRegistryClient, config);
663+
var deserializer = new JsonDeserializer<JsonPerson>(schemaRegistryClient);
664+
665+
var address = new Address
666+
{
667+
DoorNumber = 100,
668+
DoorPin = "1234"
669+
};
670+
var person = new JsonPerson()
671+
{
672+
Address = address,
673+
Name = "bob"
674+
};
675+
676+
Headers headers = new Headers();
677+
var bytes = serializer.SerializeAsync(person, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result;
678+
var result = deserializer.DeserializeAsync(bytes, false, new SerializationContext(MessageComponentType.Value, testTopic, headers)).Result;
679+
680+
Assert.Equal("bob-suffix", result.Name);
681+
Assert.Equal("1234-suffix", result.Address.DoorPin);
682+
}
683+
610684
[Fact]
611685
public void CELFieldCondition()
612686
{
@@ -1004,4 +1078,17 @@ class NewerCustomer
10041078
[JsonProperty("title")]
10051079
public string Title { get; set; }
10061080
}
1081+
1082+
class JsonPerson
1083+
{
1084+
public string Name { get; set; }
1085+
public Address Address { get; set; }
1086+
1087+
}
1088+
1089+
class Address
1090+
{
1091+
public int DoorNumber { get; set; }
1092+
public string DoorPin { get; set; }
1093+
}
10071094
}

0 commit comments

Comments
 (0)