Skip to content

fix(FieldBuilders): validate schema is not null in build()#605

Open
mliem2k wants to merge 1 commit into
linkedin:masterfrom
mliem2k:fix/field-builders-null-schema-check
Open

fix(FieldBuilders): validate schema is not null in build()#605
mliem2k wants to merge 1 commit into
linkedin:masterfrom
mliem2k:fix/field-builders-null-schema-check

Conversation

@mliem2k

@mliem2k mliem2k commented Jul 15, 2026

Copy link
Copy Markdown

Summary

Closes #435.

All FieldBuilder implementations, from FieldBuilder14 through FieldBuilder111, build a Schema.Field with a call like this:

Schema.Field result = new Schema.Field(_name, _schema, _doc, avroFriendlyDefault, _order);

None of them ever checked that _schema had actually been set via setSchema(...) before calling into Avro's constructor. Under older Avro versions this silently produced a Field object backed by a null schema, which would only surface as a confusing failure later, somewhere downstream of build().

Avro 1.11.1 added eager default value validation inside the Schema.Field constructor itself. Because that validation needs the field's schema, a null schema now causes the constructor to throw directly, with a stack trace that points into Avro internals rather than at the actual root cause, a missing setSchema() call by the caller of FieldBuilder.

Fix

Added an explicit guard at the top of build() in every FieldBuilder implementation:

if (_schema == null) {
  throw new IllegalStateException("schema not set");
}

This fails fast with a clear, actionable message before Avro's constructor is ever invoked, and is consistent with existing validation elsewhere in the codebase, for example AbstractSchemaBuilder#build() rejecting a missing _type.

Testing

Added testBuildFailsOnNullSchema to FieldBuilderTest (helper-tests-allavro), which asserts that calling build() without ever calling setSchema() throws IllegalStateException. This test runs against every supported Avro version through the module's testAvro* tasks.

Verified locally with JDK 11 (Gradle 7.6.1, matching this repo's CI):

  • ./gradlew :helper:tests:helper-tests-allavro:testAvro14 through testAvro111 (all 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.10, 1.11), all FieldBuilderTest cases pass, including the new regression test.
  • ./gradlew :helper:helper-common:build :helper:impls:helper-impl-14:build ... :helper:impls:helper-impl-111:build (all FieldBuilder* impl modules), including checkstyle, all green.

Test plan

  • testBuildFailsOnNullSchema passes across all Avro versions (1.4 through 1.11.4)
  • Existing FieldBuilderTest cases continue to pass across all Avro versions
  • helper-impl-* module builds (including checkstyle) pass for all affected modules

FieldBuilder implementations (avro 1.4 through 1.11) constructed a
Schema.Field via new Schema.Field(name, schema, doc, default, order)
without ever checking that schema had been set. Under older Avro
versions this silently produced a Field backed by a null schema,
which failed later in confusing ways. Avro 1.11.1+ tightened default
value validation inside the Schema.Field constructor itself, so the
same missing check now surfaces as an exception thrown from deep
inside Avro's constructor instead of a clear error at the call site.

Add an explicit null check for the schema at the top of build() in
every FieldBuilder implementation, throwing an IllegalStateException
with a clear message before Avro's own constructor is invoked. This
matches the existing validation style used elsewhere in the codebase,
for example AbstractSchemaBuilder#build() rejecting a missing type.

Also add a regression test, testBuildFailsOnNullSchema, to
FieldBuilderTest so the behavior is verified across all supported
Avro versions via the helper-tests-allavro module.

Closes linkedin#435

Signed-off-by: mliem2k <michael.liem2k@gmail.com>
Copilot AI review requested due to automatic review settings July 15, 2026 16:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

make FieldBuilders validate that schema is != null on build()

2 participants