@@ -86,6 +86,14 @@ private Schema getExpectedNestedTestProtoSchemaIntUserId() {
86
86
return getExpectedNestedTestProtoSchema ();
87
87
}
88
88
89
+ private SchemaBuilder getComplexTypeSchemaBuilder () {
90
+ final SchemaBuilder complexTypeBuilder = SchemaBuilder .struct ();
91
+ complexTypeBuilder .field ("one_id" , SchemaBuilder .string ().optional ().build ());
92
+ complexTypeBuilder .field ("other_id" , SchemaBuilder .int32 ().optional ().build ());
93
+ complexTypeBuilder .field ("is_active" , SchemaBuilder .bool ().optional ().build ());
94
+ return complexTypeBuilder ;
95
+ }
96
+
89
97
private Schema getExpectedNestedTestProtoSchema () {
90
98
final SchemaBuilder builder = SchemaBuilder .struct ();
91
99
final SchemaBuilder userIdBuilder = SchemaBuilder .struct ();
@@ -99,11 +107,7 @@ private Schema getExpectedNestedTestProtoSchema() {
99
107
builder .field ("experiments_active" , SchemaBuilder .array (SchemaBuilder .string ().optional ().build ()).optional ().build ());
100
108
builder .field ("updated_at" , org .apache .kafka .connect .data .Timestamp .builder ().optional ().build ());
101
109
builder .field ("status" , SchemaBuilder .string ().optional ().build ());
102
- final SchemaBuilder complexTypeBuilder = SchemaBuilder .struct ();
103
- complexTypeBuilder .field ("one_id" , SchemaBuilder .string ().optional ().build ());
104
- complexTypeBuilder .field ("other_id" , SchemaBuilder .int32 ().optional ().build ());
105
- complexTypeBuilder .field ("is_active" , SchemaBuilder .bool ().optional ().build ());
106
- builder .field ("complex_type" , complexTypeBuilder .optional ().build ());
110
+ builder .field ("complex_type" , getComplexTypeSchemaBuilder ().optional ().build ());
107
111
builder .field ("map_type" , SchemaBuilder .array (SchemaBuilder .struct ().field ("key" , Schema .OPTIONAL_STRING_SCHEMA ).field ("value" , Schema .OPTIONAL_STRING_SCHEMA ).optional ().build ()).optional ().build ());
108
112
return builder .build ();
109
113
}
@@ -161,6 +165,27 @@ private Struct getExpectedNestedTestProtoResultIntUserId() throws ParseException
161
165
return result ;
162
166
}
163
167
168
+ private NestedTestProtoOuterClass .ComplexType createProtoDefaultOneOf () throws ParseException {
169
+ NestedTestProtoOuterClass .ComplexType .Builder complexTypeBuilder = NestedTestProtoOuterClass .ComplexType .newBuilder ();
170
+ complexTypeBuilder .setOtherId (0 );
171
+ return complexTypeBuilder .build ();
172
+ }
173
+
174
+ private NestedTestProtoOuterClass .ComplexType createProtoMultipleSetOneOf () throws ParseException {
175
+ NestedTestProtoOuterClass .ComplexType .Builder complexTypeBuilder = NestedTestProtoOuterClass .ComplexType .newBuilder ();
176
+ complexTypeBuilder .setOneId ("asdf" );
177
+ complexTypeBuilder .setOtherId (0 );
178
+ return complexTypeBuilder .build ();
179
+ }
180
+
181
+ private Struct getExpectedComplexTypeProtoWithDefaultOneOf () {
182
+ Schema schema = getComplexTypeSchemaBuilder ().build ();
183
+ Struct result = new Struct (schema .schema ());
184
+ result .put ("other_id" , 0 );
185
+ result .put ("is_active" , false );
186
+ return result ;
187
+ }
188
+
164
189
private void assertSchemasEqual (Schema expectedSchema , Schema actualSchema ) {
165
190
assertEquals (expectedSchema .type (), actualSchema .type ());
166
191
assertEquals (expectedSchema .isOptional (), actualSchema .isOptional ());
@@ -196,6 +221,26 @@ public void testToConnectDataWithNestedProtobufMessageAndIntUserId() throws Pars
196
221
assertEquals (new SchemaAndValue (getExpectedNestedTestProtoSchemaIntUserId (), getExpectedNestedTestProtoResultIntUserId ()), result );
197
222
}
198
223
224
+ @ Test
225
+ public void testToConnectDataDefaultOneOf () throws ParseException {
226
+ Schema schema = getComplexTypeSchemaBuilder ().build ();
227
+ NestedTestProtoOuterClass .ComplexType message = createProtoDefaultOneOf ();
228
+ ProtobufData protobufData = new ProtobufData (NestedTestProtoOuterClass .ComplexType .class , LEGACY_NAME );
229
+ SchemaAndValue result = protobufData .toConnectData (message .toByteArray ());
230
+ assertSchemasEqual (schema , result .schema ());
231
+ assertEquals (new SchemaAndValue (schema , getExpectedComplexTypeProtoWithDefaultOneOf ()), result );
232
+ }
233
+
234
+ @ Test
235
+ public void testToConnectDataDefaultOneOfCannotHaveTwoOneOfsSet () throws ParseException {
236
+ Schema schema = getComplexTypeSchemaBuilder ().build ();
237
+ NestedTestProtoOuterClass .ComplexType message = createProtoMultipleSetOneOf ();
238
+ ProtobufData protobufData = new ProtobufData (NestedTestProtoOuterClass .ComplexType .class , LEGACY_NAME );
239
+ SchemaAndValue result = protobufData .toConnectData (message .toByteArray ());
240
+ assertSchemasEqual (schema , result .schema ());
241
+ assertEquals (new SchemaAndValue (schema , getExpectedComplexTypeProtoWithDefaultOneOf ()), result );
242
+ }
243
+
199
244
// Data Conversion tests
200
245
@ Test
201
246
public void testToConnectSupportsOptionalValues () {
0 commit comments