Skip to content

[Java] Fix default values of fields with UNION schemas where the NULL schema comes first#3660

Open
lw-mcno wants to merge 1 commit intoapache:mainfrom
lw-mcno:fix-java-default-values-on-union-schemas-with-null-first
Open

[Java] Fix default values of fields with UNION schemas where the NULL schema comes first#3660
lw-mcno wants to merge 1 commit intoapache:mainfrom
lw-mcno:fix-java-default-values-on-union-schemas-with-null-first

Conversation

@lw-mcno
Copy link

@lw-mcno lw-mcno commented Feb 19, 2026

The assumption in JacksonUtils#toObject(JsonNode, Schema), that the actual data schema in a UNION schema (of some data with NULL), always comes first, is incorrect.

For example, a field with this schema will have its default value converted incorrectly:

{
  "name": "exampleDateTime",
  "type": ["null", { "type": "long", "logicalType": "local-timestamp-millis" }],
  "default": 1746088255000
}

Here the "null" schema explicitly comes first and the assumption is no longer correct (JacksonUtils tries to interpret the value 1746088255000 using the NULL schema).

Another example using Avro IDL (nullable field, containing an array of nullable items):

record SomeRecord {
  union{array<int?>, null} optArrayOptItemVal = [4];
}

In this case, the "optArrayOptItemVal" field fulfills the assumption, but the array item's schema does not. On my machine, with Avro 1.12.1, the item's schema is the following:

"type": ["null", { "type": "int" }]

which also fails the non-NULL schema first assumption.

All of the above examples are successfully parsed or converted through the Avro IDL library to a Schema object.

What is the purpose of the change

Fix the retrieval of schema fields' default values (JacksonUtils) when the schema(s) consist of UNIONs with NULLs (as first type in the UNION).

Verifying this change

This change added tests and can be verified as follows:

  • Added test statements in TestJacksonUtils.java, which check the behavior with UNION schemas, that contain NULL schema as its first type.

Documentation

  • Does this pull request introduce a new feature? no

@github-actions github-actions bot added the Java Pull Requests for Java binding label Feb 19, 2026
… schema comes first

The assumption in JacksonUtils#toObject(JsonNode, Schema), that the actual data schema in a UNION schema (of some data with NULL), always comes first, is incorrect.

For example, a field with this schema will have its default value converted incorrectly:

```
{
  "name": "exampleDateTime",
  "type": ["null", { "type": "long", "logicalType": "local-timestamp-millis" }],
  "default": 1746088255000
}
```

Here the "null" schema explicitly comes first and the assumption is no longer correct (JacksonUtils tries to interpret the value 1746088255000 using the NULL schema).

Another example using Avro IDL (nullable field, containing an array of nullable items):

```
record SomeRecord {
  union{array<int?>, null} optArrayOptItemVal = [4];
}
```

In this case, the "optArrayOptItemVal" field fulfills the assumption, but the array item's schema does not. On my machine, with Avro 1.12.1, the item's schema is the following:

```
"type": ["null", { "type": "int" }]
```

which also fails the non-NULL schema first assumption.
@lw-mcno lw-mcno force-pushed the fix-java-default-values-on-union-schemas-with-null-first branch from 97004ed to e15f7cb Compare February 20, 2026 09:54
@lw-mcno
Copy link
Author

lw-mcno commented Feb 20, 2026

I've updated the PR with fixed formatting on TestJacksonUtils.java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java Pull Requests for Java binding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments