You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/02/12 17:17:00 UTC

[jira] [Commented] (KAFKA-5550) Struct.put() should include the field name if validation fails

    [ https://issues.apache.org/jira/browse/KAFKA-5550?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16361101#comment-16361101 ] 

ASF GitHub Bot commented on KAFKA-5550:
---------------------------------------

hachikuji closed pull request #3507: KAFKA-5550 
URL: https://github.com/apache/kafka/pull/3507
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/connect/api/src/main/java/org/apache/kafka/connect/data/Struct.java b/connect/api/src/main/java/org/apache/kafka/connect/data/Struct.java
index 6e7b5d23bbd..5e4de21b632 100644
--- a/connect/api/src/main/java/org/apache/kafka/connect/data/Struct.java
+++ b/connect/api/src/main/java/org/apache/kafka/connect/data/Struct.java
@@ -211,7 +211,9 @@ public Struct put(String fieldName, Object value) {
      * @return the Struct, to allow chaining of {@link #put(String, Object)} calls
      */
     public Struct put(Field field, Object value) {
-        ConnectSchema.validateValue(field.schema(), value);
+        if (null == field)
+            throw new DataException("field cannot be null.");
+        ConnectSchema.validateValue(field.name(), field.schema(), value);
         values[field.index()] = value;
         return this;
     }
diff --git a/connect/api/src/test/java/org/apache/kafka/connect/data/StructTest.java b/connect/api/src/test/java/org/apache/kafka/connect/data/StructTest.java
index 42345b1986c..e91960576a1 100644
--- a/connect/api/src/test/java/org/apache/kafka/connect/data/StructTest.java
+++ b/connect/api/src/test/java/org/apache/kafka/connect/data/StructTest.java
@@ -266,4 +266,28 @@ public void testValidateFieldWithInvalidValueType() {
         thrown.expectMessage("Invalid Java object for schema type INT8: class java.lang.Object for field: \"field\"");
         ConnectSchema.validateValue(fieldName, Schema.INT8_SCHEMA, new Object());
     }
+
+    @Test
+    public void testPutNullField() {
+        final String fieldName = "fieldName";
+        Schema testSchema = SchemaBuilder.struct()
+            .field(fieldName, Schema.STRING_SCHEMA);
+        Struct struct = new Struct(testSchema);
+
+        thrown.expect(DataException.class);
+        Field field = null;
+        struct.put(field, "valid");
+    }
+
+    @Test
+    public void testInvalidPutIncludesFieldName() {
+        final String fieldName = "fieldName";
+        Schema testSchema = SchemaBuilder.struct()
+            .field(fieldName, Schema.STRING_SCHEMA);
+        Struct struct = new Struct(testSchema);
+
+        thrown.expect(DataException.class);
+        thrown.expectMessage("Invalid value: null used for required field: \"fieldName\", schema type: STRING");
+        struct.put(fieldName, null);
+    }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Struct.put() should include the field name if validation fails
> --------------------------------------------------------------
>
>                 Key: KAFKA-5550
>                 URL: https://issues.apache.org/jira/browse/KAFKA-5550
>             Project: Kafka
>          Issue Type: Bug
>          Components: KafkaConnect
>            Reporter: Jeremy Custenborder
>            Assignee: Jeremy Custenborder
>            Priority: Minor
>
> When calling struct.put() with an invalid value, the error message should include the field name.
> {code:java}
> @Test
> public void testPutIncludesFieldName() {
>     final String fieldName = "fieldName";
>     Schema testSchema = SchemaBuilder.struct()
>         .field(fieldName, Schema.STRING_SCHEMA);
>     Struct struct = new Struct(testSchema);
>     try {
>         struct.put(fieldName, null);
>     } catch (DataException ex) {
>         assertEquals(
>             "Invalid value: null used for required field: \"fieldName\", schema type: STRING",
>             ex.getMessage()
>         );
>     }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)