Details
-
Bug
-
Status: Open
-
Major
-
Resolution: Unresolved
-
1.11.3
-
None
-
None
Description
I have a schema with an optional timestamp which is to be rendered as millis and the generated Java class fails to get the conversions array and method added, which means that at runtime I get a ClassCastException when an "Instant" object is cast to a "Long" because there was no registered converter:
The element with an optional timestamp in my schema:
{{{}}
"name": "DecisionStartTime",
"type": [
"null",
{
"type": "long",
"logicalType": "timestamp-millis"
{{ }}}
],
"default": null
}
The problem seems to be here:
Which only renders the conversion part if any of the fields has logicalType set and the actual method:
Does not take into account that a field which is a union does not have a logical type, however one of the union types might.
Proposed solution - replace method
public boolean hasLogicalTypeField(Schema schema) {
for (Schema.Field field : schema.getFields()) {
if (field.schema().getLogicalType() != null) {
return true;
{{ }}}
{{ }}}
return false;
{{ }}}
With a method like this:
public boolean hasLogicalTypeField(Schema schema) {
for (Schema.Field field : schema.getFields()) {
if (field.schema().getLogicalType() != null) {
return true;
} else if (field.schema().type == Type.UNION) {
for (Schema type : field.schema().getTypes()) {
if (type.getLogicalType() != null) {
return true;
{{ }}}
{{ }}}
{{ }}}
{{ }}}
return false;
{{ }}}
The conversionInstance() method might also need tweaking in order to identify that the given type has a logical type as part of the union.
Attachments
Attachments
Issue Links
- relates to
-
AVRO-3536 Union type not inheriting type conversions
- Resolved