Skip to content

Commit

Permalink
an IllegalArgumentException will be thrown if toAvro json contains un…
Browse files Browse the repository at this point in the history
…known fields
  • Loading branch information
michal-harish committed Nov 21, 2018
1 parent 0eedddc commit 6f3def9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ object AvroJsonConverter {
case Schema.Type.BYTES => ByteBuffer.wrap(json.getTextValue.getBytes(StandardCharsets.UTF_8))
case Schema.Type.FIXED => new GenericData.Fixed(schema, json.getTextValue.getBytes(StandardCharsets.UTF_8))
case Schema.Type.RECORD if json.isObject =>
json.getFieldNames.asScala.foreach { fn =>
if (schema.getField(fn) == null) {
throw new IllegalArgumentException(s"No such field '$fn' in ${schema.getFullName}")
}
}
val builder = new GenericRecordBuilder(schema)
schema.getFields.asScala foreach { field =>
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,11 @@ class AvroJsonConverterSpec extends FlatSpec with Matchers {
AvroJsonConverter.toAvro[AvroEnums]("{}") should equal(AvroEnums())
}

it should "not allow passing unknown json fields" in {
val thrown = intercept[RuntimeException] {
AvroJsonConverter.toAvro[ListSet]("{\"hello\": \"there\"}")
}
thrown.getCause.isInstanceOf[IllegalArgumentException] should be (true)
}

}

0 comments on commit 6f3def9

Please sign in to comment.