Skip to content

fix(postgresql): Fix NullPointerException comparing or writing a null… - #934

Merged
jeffjensen merged 1 commit into
mainfrom
677-postgresql-null-datatypes
Aug 11, 2026
Merged

fix(postgresql): Fix NullPointerException comparing or writing a null…#934
jeffjensen merged 1 commit into
mainfrom
677-postgresql-null-datatypes

Conversation

@jeffjensen

@jeffjensen jeffjensen commented Aug 11, 2026

Copy link
Copy Markdown
Member

… enum, uuid, inet, or citext value

GenericEnumType.typeCast() had no null guard, so AbstractDataType.compare() threw NullPointerException instead of reporting a mismatch whenever exactly one side of a compared pair was null. UuidType, InetType, and CitextType shared the same two-part gap: an unguarded typeCast(), plus a setSqlValue() override that bypasses typeCast() entirely to call a private PGobject- building helper that also dereferences the value unconditionally.

  • Return null from typeCast() for a null input in all four classes, matching every other DataType implementation.
  • Bind sql NULL from setSqlValue() before ever reaching the PGobject- building helper, matching the pattern JsonType already established.
  • Add unit coverage per class: typeCast(null) returns null, compare(null, nonNullValue) no longer throws (the literal reported crash), and a Mockito-based setSqlValue(null, ...) proof that sql NULL is bound instead of thrown.
  • Add PostgresqlNullableOtherTypesIT, round-tripping a null uuid/inet/citext row through CLEAN_INSERT against a live PostgreSQL 16 container. GenericEnumType is proven at the unit level only: a separate, pre-existing defect (issue 933, filed but not fixed here) leaves it unreachable for a real table column regardless of null handling, discovered while writing this test.
  • Document the issue 933 caveat in postgresql.adoc's Known Quirks.

Refs: 677
Refs: 930

Claude-Session: https://claude.ai/code/session_01XBjSJ2bgVtwqyxGv2nvwP9

Summary by Sourcery

Handle PostgreSQL enum, UUID, inet, and citext null values safely to avoid NullPointerExceptions and verify behavior with unit and integration tests.

Bug Fixes:

  • Return null from GenericEnumType, UuidType, InetType, and CitextType typeCast() when given null and bind SQL NULL from setSqlValue() instead of throwing NullPointerException when handling nulls.

Documentation:

  • Document the null-handling fix and GenericEnumType limitation in the PostgreSQL changelog and database documentation.

Tests:

  • Add unit tests for GenericEnumType, UuidType, InetType, and CitextType to cover null typeCast(), compare() with null vs non-null values, and setSqlValue() binding SQL NULL without throwing.
  • Add PostgresqlNullableOtherTypesIT to verify CLEAN_INSERT round-trips null uuid/inet/citext columns against a live PostgreSQL database without errors.

Summary by CodeRabbit

  • Bug Fixes

    • PostgreSQL UUID, network address, case-insensitive text, and enum values now handle NULL safely.
    • NULL values are correctly written to PostgreSQL without errors.
    • Comparisons and type conversions no longer throw exceptions for null values.
  • Documentation

    • Expanded guidance on PostgreSQL enum handling and documented a known metadata-related limitation.
  • Tests

    • Added unit and integration coverage for nullable PostgreSQL values and round-trip behavior.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@sourcery-ai

sourcery-ai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Reviewer's Guide

Ensure PostgreSQL-specific DataTypes (GenericEnumType, UuidType, InetType, CitextType) safely handle null values in both typeCast() and setSqlValue(), and add unit/integration tests plus documentation and changelog entries to prove and describe the behavior and associated quirks.

Sequence diagram for AbstractDataType.compare with null and PostgreSQL DataTypes

sequenceDiagram
    participant Test as GenericEnumTypeTest
    participant ADT as AbstractDataType
    participant EnumType as GenericEnumType

    Test->>ADT: compare(null, nonNullValue)
    ADT->>EnumType: typeCast(null)
    EnumType-->>ADT: null
    ADT->>EnumType: typeCast(nonNullValue)
    EnumType-->>ADT: "nonNullValue"
    ADT-->>Test: comparison result (no NullPointerException)
Loading

Sequence diagram for setSqlValue(null) on PostgreSQL DataTypes

sequenceDiagram
    participant Test as PostgresqlNullableOtherTypesIT
    participant UuidType as UuidType
    participant PS as PreparedStatement

    Test->>UuidType: setSqlValue(null, column, PS)
    alt value is null
        UuidType->>PS: setNull(column, Types.OTHER)
    else value is non-null
        UuidType->>PS: setObject(column, getUUID(uuid, PS.getConnection()))
    end
    PS-->>Test: CLEAN_INSERT succeeds without NullPointerException
Loading

File-Level Changes

Change Details Files
Add null-handling to PostgreSQL enum/uuid/inet/citext DataTypes so typeCast() returns null for null input and setSqlValue() binds SQL NULL instead of throwing NullPointerException.
  • Add null guard in GenericEnumType.setSqlValue() to call PreparedStatement.setNull(Types.OTHER) and return early when the enumObject is null.
  • Change GenericEnumType.typeCast() to return null when arg0 is null and otherwise return arg0.toString(), with documentation clarifying behavior.
  • Add null guard in CitextType.setSqlValue() to bind SQL NULL via PreparedStatement.setNull(Types.OTHER) when the value is null before invoking getCitext().
  • Change CitextType.typeCast() to return null when arg0 is null and otherwise return arg0.toString().
  • Add null guard in InetType.setSqlValue() to bind SQL NULL via PreparedStatement.setNull(Types.OTHER) when the value is null before invoking getInet().
  • Change InetType.typeCast() to return null when arg0 is null and otherwise return arg0.toString().
  • Add null guard in UuidType.setSqlValue() to bind SQL NULL via PreparedStatement.setNull(Types.OTHER) when the value is null before invoking getUUID().
src/main/java/org/dbunit/ext/postgresql/GenericEnumType.java
src/main/java/org/dbunit/ext/postgresql/CitextType.java
src/main/java/org/dbunit/ext/postgresql/InetType.java
src/main/java/org/dbunit/ext/postgresql/UuidType.java
Add unit tests to verify null-safe behavior of typeCast(), compare(), and setSqlValue() for CitextType, GenericEnumType, InetType, and UuidType using AssertJ and Mockito.
  • Extend CitextTypeTest with MockitoExtension and a mocked PreparedStatement, and add tests that typeCast(null) returns null, compare(null, nonNull) does not throw and treats null as less, and setSqlValue(null, ...) binds SQL NULL via setNull(Types.OTHER).
  • Extend GenericEnumTypeTest with MockitoExtension and a mocked PreparedStatement, and add analogous null-handling tests for typeCast(), compare(), and setSqlValue().
  • Extend InetTypeTest with MockitoExtension and a mocked PreparedStatement, and add analogous null-handling tests for typeCast(), compare(), and setSqlValue().
  • Extend UuidTypeTest with MockitoExtension and a mocked PreparedStatement, and add tests for compare(null, nonNullUuidString) behavior and setSqlValue(null, ...) binding SQL NULL, in addition to the existing typeCast(null) test.
src/test/java/org/dbunit/ext/postgresql/CitextTypeTest.java
src/test/java/org/dbunit/ext/postgresql/GenericEnumTypeTest.java
src/test/java/org/dbunit/ext/postgresql/InetTypeTest.java
src/test/java/org/dbunit/ext/postgresql/UuidTypeTest.java
Add an integration test to prove that CLEAN_INSERT round-trips null uuid/inet/citext column values as real SQL NULLs against a live PostgreSQL instance.
  • Create PostgresqlNullableOtherTypesIT that sets up a temporary test table with uuid, inet, and citext columns, uses PostgresqlDataTypeFactory, and performs CLEAN_INSERT with a FlatXmlDataSet containing both non-null and [NULL] placeholder values.
  • Configure ReplacementDataSet to replace "[NULL]" with null and assert that retrieved values from the table include expected non-null values in the first row and actual nulls in the second row for UID, IP, and NOTE.
  • Guard the integration test with @EnabledIfSystemProperty("dbunit.profile", "postgresql") and clean up the table and connection in @AfterEach.
src/test/java/org/dbunit/ext/postgresql/PostgresqlNullableOtherTypesIT.java
Update project documentation and changelog to record the null-handling fix and mention the GenericEnumType data-type selection quirk (issue 933).
  • Add a new entry to changes.xml describing the fix for issue 677 and the related null-handling corrections for UuidType, InetType, and CitextType, referencing issue 930 and JsonType’s prior pattern.
  • Update the PostgreSQL database documentation (postgresql.adoc) to include a Known Quirks note that custom PostgreSQL enum columns are reported as VARCHAR and thus GenericEnumType is not selected by PostgresqlDataTypeFactory (issue 933), even though its null-handling is fixed.
src/changes/changes.xml
src/site/asciidoc/databases/postgresql.adoc

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@jeffjensen, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 36 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 9e4f0430-365f-4f61-995a-53650123ca5e

📥 Commits

Reviewing files that changed from the base of the PR and between 05054e2 and a32bf4e.

📒 Files selected for processing (2)
  • src/main/java/org/dbunit/ext/postgresql/CitextType.java
  • src/main/java/org/dbunit/ext/postgresql/InetType.java
📝 Walkthrough

Walkthrough

PostgreSQL enum, UUID, inet, and citext types now handle null values safely. Unit and integration tests cover null casting, comparison, SQL NULL binding, and nullable round trips. PostgreSQL enum metadata limitations are documented.

Changes

PostgreSQL nullable type handling

Layer / File(s) Summary
Null-safe PostgreSQL type implementations
src/main/java/org/dbunit/ext/postgresql/*, src/site/asciidoc/databases/postgresql.adoc, src/changes/changes.xml
The PostgreSQL types return null for null casts and bind null values with Types.OTHER. The enum metadata limitation and changelog entry are documented.
Nullable type regression coverage
src/test/java/org/dbunit/ext/postgresql/*
Unit tests cover null casting, comparison, and SQL NULL binding. PostgreSQL integration coverage verifies nullable uuid, inet, and citext round trips.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 41.18% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the PostgreSQL null-handling fix for NullPointerException during comparison or writing.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 677-postgresql-null-datatypes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sourcery-ai sourcery-ai Bot 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.

Hey - I've found 1 issue, and left some high level feedback:

  • The Javadoc for CitextType.setSqlValue and InetType.setSqlValue still refer to the parameter as "uuid", which looks like a copy/paste artifact and should be updated to match the actual semantics to avoid confusion.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The Javadoc for CitextType.setSqlValue and InetType.setSqlValue still refer to the parameter as "uuid", which looks like a copy/paste artifact and should be updated to match the actual semantics to avoid confusion.

## Individual Comments

### Comment 1
<location path="src/main/java/org/dbunit/ext/postgresql/InetType.java" line_range="62-63" />
<code_context>
         return resultSet.getString(column);
     }

+    /**
+     * {@inheritDoc} Binds sql {@code NULL} when {@code uuid} is
+     * {@code null}, instead of dereferencing it while building the
+     * PGobject.
</code_context>
<issue_to_address>
**suggestion (typo):** Javadoc for InetType#setSqlValue uses `uuid` terminology, which doesn’t match the inet type.

The Javadoc here still refers to "uuid", which is inconsistent with `InetType` and the inet parameter semantics. Please update the documented parameter name to something inet-specific or a generic name like `value` to avoid confusion.

Suggested implementation:

```java
    /**
     * {@inheritDoc} Binds SQL {@code NULL} when the inet {@code value} is
     * {@code null}, instead of dereferencing it while building the
     * {@code PGobject}.
     */
    public void setSqlValue(Object uuid, int column,
                            PreparedStatement statement) throws SQLException, TypeCastException {

```

```java
        if (uuid == null) {

```

```java
        statement.setObject(column, getInet(uuid, statement.getConnection()));

```

If there are other Javadocs or comments in this class (or related types) that still use `uuid` terminology for inet values, they should be updated similarly to use `inet`-specific wording or a generic term like `value` for consistency.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/main/java/org/dbunit/ext/postgresql/InetType.java Outdated

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/java/org/dbunit/ext/postgresql/CitextType.java`:
- Around line 64-69: Correct the type-specific JavaDoc for setSqlValue: in
src/main/java/org/dbunit/ext/postgresql/CitextType.java lines 64-69, replace
UUID terminology with value and describe a citext value; make the equivalent
terminology correction in src/main/java/org/dbunit/ext/postgresql/InetType.java
lines 62-67, describing an inet value.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: cfe44d60-4f59-4038-a698-e1ce1645dcdd

📥 Commits

Reviewing files that changed from the base of the PR and between f92ff08 and 05054e2.

📒 Files selected for processing (11)
  • src/changes/changes.xml
  • src/main/java/org/dbunit/ext/postgresql/CitextType.java
  • src/main/java/org/dbunit/ext/postgresql/GenericEnumType.java
  • src/main/java/org/dbunit/ext/postgresql/InetType.java
  • src/main/java/org/dbunit/ext/postgresql/UuidType.java
  • src/site/asciidoc/databases/postgresql.adoc
  • src/test/java/org/dbunit/ext/postgresql/CitextTypeTest.java
  • src/test/java/org/dbunit/ext/postgresql/GenericEnumTypeTest.java
  • src/test/java/org/dbunit/ext/postgresql/InetTypeTest.java
  • src/test/java/org/dbunit/ext/postgresql/PostgresqlNullableOtherTypesIT.java
  • src/test/java/org/dbunit/ext/postgresql/UuidTypeTest.java

Comment thread src/main/java/org/dbunit/ext/postgresql/CitextType.java
… enum, uuid, inet, or citext value

GenericEnumType.typeCast() had no null guard, so AbstractDataType.compare()
threw NullPointerException instead of reporting a mismatch whenever exactly
one side of a compared pair was null. UuidType, InetType, and CitextType
shared the same two-part gap: an unguarded typeCast(), plus a setSqlValue()
override that bypasses typeCast() entirely to call a private PGobject-
building helper that also dereferences the value unconditionally.

* Return null from typeCast() for a null input in all four classes,
  matching every other DataType implementation.
* Bind sql NULL from setSqlValue() before ever reaching the PGobject-
  building helper, matching the pattern JsonType already established.
* Add unit coverage per class: typeCast(null) returns null,
  compare(null, nonNullValue) no longer throws (the literal reported
  crash), and a Mockito-based setSqlValue(null, ...) proof that sql
  NULL is bound instead of thrown.
* Add PostgresqlNullableOtherTypesIT, round-tripping a null
  uuid/inet/citext row through CLEAN_INSERT against a live PostgreSQL
  16 container. GenericEnumType is proven at the unit level only: a
  separate, pre-existing defect (issue 933, filed but not fixed here)
  leaves it unreachable for a real table column regardless of null
  handling, discovered while writing this test.
* Document the issue 933 caveat in postgresql.adoc's Known Quirks.

Refs: 677
Refs: 930

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XBjSJ2bgVtwqyxGv2nvwP9
@jeffjensen
jeffjensen force-pushed the 677-postgresql-null-datatypes branch from 05054e2 to a32bf4e Compare August 11, 2026 18:58
@jeffjensen
jeffjensen merged commit 387e4db into main Aug 11, 2026
39 of 40 checks passed
@jeffjensen
jeffjensen deleted the 677-postgresql-null-datatypes branch August 11, 2026 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant