Skip to content

Commit 3c25fdf

Browse files
committed
KAFKA-10675 fix after review
1 parent ea28af7 commit 3c25fdf

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

connect/api/src/main/java/org/apache/kafka/connect/data/ConnectSchema.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,15 @@ public static void validateValue(String name, Schema schema, Object value) {
231231
}
232232

233233
if (!foundMatch) {
234-
throw new DataException(String.format("Invalid Java object for %s: %s for field: \"%s\"",
235-
buildSchemaInfo(schema), value.getClass(), name)
236-
);
234+
StringBuilder exceptionMessage = new StringBuilder("Invalid Java object for schema");
235+
if (schema.name() != null) {
236+
exceptionMessage.append(" \"").append(schema.name()).append("\"");
237+
}
238+
exceptionMessage.append(" with type ").append(schema.type()).append(": ").append(value.getClass());
239+
if (name != null) {
240+
exceptionMessage.append(" for field: \"").append(name).append("\"");
241+
}
242+
throw new DataException(exceptionMessage.toString());
237243
}
238244

239245
switch (schema.type()) {
@@ -265,15 +271,6 @@ private static List<Class<?>> expectedClassesFor(Schema schema) {
265271
return expectedClasses;
266272
}
267273

268-
private static String buildSchemaInfo(Schema schema) {
269-
StringBuilder schemaInfo = new StringBuilder("schema");
270-
if (schema.name() != null) {
271-
schemaInfo.append(" \"").append(schema.name()).append("\"");
272-
}
273-
schemaInfo.append(" with type ").append(schema.type());
274-
return schemaInfo.toString();
275-
}
276-
277274
/**
278275
* Validate that the value can be used for this schema, i.e. that its type matches the schema type and optional
279276
* requirements. Throws a DataException if the value is invalid.

connect/api/src/test/java/org/apache/kafka/connect/data/StructTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,17 @@ public void testValidateFieldWithInvalidValueType() {
318318
Schema.INT8_SCHEMA, new Object()));
319319
assertEquals("Invalid Java object for schema with type INT8: class java.lang.Object for field: \"field\"",
320320
e.getMessage());
321+
322+
e = assertThrows(DataException.class, () -> ConnectSchema.validateValue(Schema.INT8_SCHEMA, new Object()));
323+
assertEquals("Invalid Java object for schema with type INT8: class java.lang.Object", e.getMessage());
321324
}
322325

323326
@Test
324327
public void testValidateFieldWithInvalidValueMismatchTimestamp() {
325328
String fieldName = "field";
326329
long longValue = 1000L;
327330

328-
// Does not throw
331+
// Does not throw
329332
ConnectSchema.validateValue(fieldName, Schema.INT64_SCHEMA, longValue);
330333

331334
Exception e = assertThrows(DataException.class, () -> ConnectSchema.validateValue(fieldName,

0 commit comments

Comments
 (0)