Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Null check for auto-unboxed field id in builder #12165

Merged
merged 4 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/src/main/java/org/apache/iceberg/types/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,8 @@ public Builder withWriteDefault(Object fieldWriteDefault) {
}

public NestedField build() {
// the constructor validates the fields
Preconditions.checkNotNull(id, "Id cannot be null");
// the constructor validates the other fields
return new NestedField(isOptional, id, name, type, doc, initialDefault, writeDefault);
}
}
Expand Down
13 changes: 13 additions & 0 deletions api/src/test/java/org/apache/iceberg/types/TestTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.iceberg.types;

import static org.apache.iceberg.types.Types.NestedField.optional;
import static org.apache.iceberg.types.Types.NestedField.required;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

Expand Down Expand Up @@ -47,4 +49,15 @@ public void fromPrimitiveString() {
.isThrownBy(() -> Types.fromPrimitiveString("abcdefghij"))
.withMessage("Cannot parse type string to primitive: abcdefghij");
}

@Test
public void testNestedFieldBuilderIdCheck() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> optional("field").ofType(Types.StringType.get()).build())
.withMessage("Id cannot be null");

assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> required("field").ofType(Types.StringType.get()).build())
.withMessage("Id cannot be null");
}
}