[
https://issues.apache.org/jira/browse/AVRO-2278?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17093671#comment-17093671
]
Ryan Skraba commented on AVRO-2278:
-----------------------------------
It *would* be more consistent:
* For get/put by index, with an invalid index, a GenericRecord will throw an
{{IndexOutOfBoundsException}}
* For get/put by index, with an invalid index, a SpecificRecord will throw anĀ
{{AvroRuntimeException("Bad index")}}
* For get/put by name, with an invalid name, a SpecificRecord will throw a
{{NullPointerException}} (!!)
* As noted, put by name with an invalid name will throw an
{{AvroRuntimeException("Not a valid schema field: " + key)}}
It would even be more consistent if they all threw {{AvroRuntimeException}}
Expecting *{{get("badFieldName")}}* to return null successfully (as if it were
a Java Map) is probably smelly code. Any opinions?
Unfortunately, the behaviour of these methods is currently undocumented, and a
change could cause working code to break.
The correct way to determine whether a key exists today is probably still
{{record.getSchema().getField(key) != null}}.
> GenericData.Record field getter not correct
> -------------------------------------------
>
> Key: AVRO-2278
> URL: https://issues.apache.org/jira/browse/AVRO-2278
> Project: Apache Avro
> Issue Type: Bug
> Components: java
> Affects Versions: 1.8.2
> Reporter: Zoltan Farkas
> Priority: Major
>
> Currently the get field implementation is not correct in GenericData.Record:
> at:
> https://github.com/apache/avro/blob/master/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java#L209
> {code}
> @Override public Object get(String key) {
> Field field = schema.getField(key);
> if (field == null) return null;
> return values[field.pos()];
> }
> {code}
> The method returns null when a field is not present, making it impossible to
> distinguish between:
> field value = null
> and
> field does not exist.
> A more "correct" implementation would be:
> {code}
> @Override public Object get(String key) {
> Field field = schema.getField(key);
> if (field == null) {
> throw new IllegalArgumentException("Invalid field " + key);
> }
> return values[field.pos()];
> }
> {code}
> this will make the behavior consistent with put which will throw a exception
> when setting a non existent field.
> when I make this change in my fork, some bugs in unit tests showed up....
--
This message was sent by Atlassian Jira
(v8.3.4#803005)