Skip to content

Commit

Permalink
API: Null check for auto-unboxed field id in builder
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcweeks committed Feb 3, 2025
1 parent 507e2a9 commit fa0ef6b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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");
}
}

0 comments on commit fa0ef6b

Please sign in to comment.