Skip to content

Commit 82f3efc

Browse files
authored
DGS-19771 Support Protobuf oneof fields in Data Controct rules (#261)
1 parent 26710e6 commit 82f3efc

File tree

15 files changed

+57
-16
lines changed

15 files changed

+57
-16
lines changed

proto/test/schemaregistry/serde/example.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ message Author {
1414
(confluent.field_meta).tags = "PII"
1515
];
1616
repeated string works = 4;
17+
oneof pii_oneof {
18+
Pizza oneof_message = 5;
19+
string oneof_string = 6 [(.confluent.field_meta).tags = "PII"];
20+
}
1721
}
1822

1923
message Pizza {

schemaregistry/confluent/meta_pb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @generated by protoc-gen-es v2.0.0 with parameter "target=ts"
1+
// @generated by protoc-gen-es v2.2.3 with parameter "target=ts"
22
// @generated from file confluent/meta.proto (package confluent, syntax proto3)
33
/* eslint-disable */
44

schemaregistry/confluent/types/decimal_pb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @generated by protoc-gen-es v2.0.0 with parameter "target=ts"
1+
// @generated by protoc-gen-es v2.2.3 with parameter "target=ts"
22
// @generated from file confluent/types/decimal.proto (package confluent.type, syntax proto3)
33
/* eslint-disable */
44

schemaregistry/rules/encryption/tink/proto/aes_gcm_pb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//
1515
////////////////////////////////////////////////////////////////////////////////
1616

17-
// @generated by protoc-gen-es v2.0.0 with parameter "target=ts"
17+
// @generated by protoc-gen-es v2.2.3 with parameter "target=ts"
1818
// @generated from file rules/encryption/tink/proto/aes_gcm.proto (package google.crypto.tink, syntax proto3)
1919
/* eslint-disable */
2020

schemaregistry/rules/encryption/tink/proto/aes_siv_pb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//
1515
////////////////////////////////////////////////////////////////////////////////
1616

17-
// @generated by protoc-gen-es v2.0.0 with parameter "target=ts"
17+
// @generated by protoc-gen-es v2.2.3 with parameter "target=ts"
1818
// @generated from file rules/encryption/tink/proto/aes_siv.proto (package google.crypto.tink, syntax proto3)
1919
/* eslint-disable */
2020

schemaregistry/serde/protobuf.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,14 +525,29 @@ async function transformField(ctx: RuleContext, fd: DescField, desc: DescMessage
525525
getType(fd),
526526
getInlineTags(fd)
527527
)
528-
const value = msg[fd.name]
528+
let value = null;
529+
if (fd.oneof != null) {
530+
let oneof = msg[fd.oneof.localName]
531+
if (oneof != null && oneof.case === fd.localName) {
532+
value = oneof.value
533+
} else {
534+
// skip oneof fields that are not set
535+
return
536+
}
537+
} else {
538+
value = msg[fd.localName]
539+
}
529540
const newValue = await transform(ctx, desc, value, fieldTransform)
530541
if (ctx.rule.kind === 'CONDITION') {
531542
if (newValue === false) {
532543
throw new RuleConditionError(ctx.rule)
533544
}
534545
} else {
535-
msg[fd.name] = newValue
546+
if (fd.oneof != null) {
547+
msg[fd.oneof.localName] = { case: fd.localName, value: newValue }
548+
} else {
549+
msg[fd.localName] = newValue
550+
}
536551
}
537552
} finally {
538553
ctx.leaveField()

schemaregistry/test/serde/protobuf.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,18 @@ describe('ProtobufSerializer', () => {
208208
name: 'Kafka',
209209
id: 123,
210210
picture: Buffer.from([1, 2]),
211-
works: ['The Castle', 'The Trial']
211+
works: ['The Castle', 'The Trial'],
212+
piiOneof: {
213+
case: 'oneofString',
214+
value: 'oneof'
215+
}
212216
})
213217
let bytes = await ser.serialize(topic, obj)
214218

215219
// reset encrypted field
216220
obj.name = 'Kafka'
217221
obj.picture = Buffer.from([1, 2])
222+
obj.piiOneof = { case: 'oneofString', value: 'oneof' }
218223

219224
let deserConfig: ProtobufDeserializerConfig = {
220225
ruleConfig: {

schemaregistry/test/serde/test/cycle_pb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @generated by protoc-gen-es v2.0.0 with parameter "target=ts"
1+
// @generated by protoc-gen-es v2.2.3 with parameter "target=ts"
22
// @generated from file test/schemaregistry/serde/cycle.proto (package test, syntax proto3)
33
/* eslint-disable */
44

schemaregistry/test/serde/test/dep_pb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @generated by protoc-gen-es v2.0.0 with parameter "target=ts"
1+
// @generated by protoc-gen-es v2.2.3 with parameter "target=ts"
22
// @generated from file test/schemaregistry/serde/dep.proto (package test, syntax proto3)
33
/* eslint-disable */
44

schemaregistry/test/serde/test/example_pb.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @generated by protoc-gen-es v2.0.0 with parameter "target=ts"
1+
// @generated by protoc-gen-es v2.2.3 with parameter "target=ts"
22
// @generated from file test/schemaregistry/serde/example.proto (package test, syntax proto3)
33
/* eslint-disable */
44

@@ -11,7 +11,7 @@ import type { Message } from "@bufbuild/protobuf";
1111
* Describes the file test/schemaregistry/serde/example.proto.
1212
*/
1313
export const file_test_schemaregistry_serde_example: GenFile = /*@__PURE__*/
14-
fileDesc("Cid0ZXN0L3NjaGVtYXJlZ2lzdHJ5L3NlcmRlL2V4YW1wbGUucHJvdG8SBHRlc3QiVgoGQXV0aG9yEhYKBG5hbWUYASABKAlCCIJEBRoDUElJEgoKAmlkGAIgASgFEhkKB3BpY3R1cmUYAyABKAxCCIJEBRoDUElJEg0KBXdvcmtzGAQgAygJIicKBVBpenphEgwKBHNpemUYASABKAkSEAoIdG9wcGluZ3MYAiADKAlCCVoHLi4vdGVzdGIGcHJvdG8z", [file_confluent_meta]);
14+
fileDesc("Cid0ZXN0L3NjaGVtYXJlZ2lzdHJ5L3NlcmRlL2V4YW1wbGUucHJvdG8SBHRlc3QiqwEKBkF1dGhvchIWCgRuYW1lGAEgASgJQgiCRAUaA1BJSRIKCgJpZBgCIAEoBRIZCgdwaWN0dXJlGAMgASgMQgiCRAUaA1BJSRINCgV3b3JrcxgEIAMoCRIkCg1vbmVvZl9tZXNzYWdlGAUgASgLMgsudGVzdC5QaXp6YUgAEiAKDG9uZW9mX3N0cmluZxgGIAEoCUIIgkQFGgNQSUlIAEILCglwaWlfb25lb2YiJwoFUGl6emESDAoEc2l6ZRgBIAEoCRIQCgh0b3BwaW5ncxgCIAMoCUIJWgcuLi90ZXN0YgZwcm90bzM", [file_confluent_meta]);
1515

1616
/**
1717
* @generated from message test.Author
@@ -36,6 +36,23 @@ export type Author = Message<"test.Author"> & {
3636
* @generated from field: repeated string works = 4;
3737
*/
3838
works: string[];
39+
40+
/**
41+
* @generated from oneof test.Author.pii_oneof
42+
*/
43+
piiOneof: {
44+
/**
45+
* @generated from field: test.Pizza oneof_message = 5;
46+
*/
47+
value: Pizza;
48+
case: "oneofMessage";
49+
} | {
50+
/**
51+
* @generated from field: string oneof_string = 6;
52+
*/
53+
value: string;
54+
case: "oneofString";
55+
} | { case: undefined; value?: undefined };
3956
};
4057

4158
/**

0 commit comments

Comments
 (0)