Skip to content

Fix #968: validate Last Name mandatory field in CSV bulk upload#969

Open
areychana wants to merge 2 commits intoavniproject:masterfrom
areychana:master
Open

Fix #968: validate Last Name mandatory field in CSV bulk upload#969
areychana wants to merge 2 commits intoavniproject:masterfrom
areychana:master

Conversation

@areychana
Copy link

@areychana areychana commented Mar 11, 2026

problem

when uploading a CSV file with the Last Name field empty (marked as mandatory), no validation error was shown and the record was created with an empty Last Name.

#968 - fixes

root cause

in SubjectWriter.java, first name and gender had explicit mandatory value checks but last name was being set directly without any validation.

##fix
Added a setLastName() method following the same pattern as setFirstName(), which validates the field is non-empty before setting it on the individual.

tests

  • Updated existing test missingMandatoryCoreValues to include Last Name error
  • Added new test missingLastName to verify the fix
  • All 24 tests passing (100% success)

note

avni-server-data:test is failing but this is a pre-existing issue unrelated to these changes.

Summary by CodeRabbit

  • Bug Fixes
    • Last name is now properly enforced as mandatory for person-type subjects during data import, with clearer validation and error messages when missing or empty.
  • Tests
    • Added an integration test covering missing last name to ensure the new validation and error reporting behave as expected.

@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2026

📝 Walkthrough

Walkthrough

The change conditions last-name assignment in SubjectWriter to only set last names for person-type subjects via a new private helper that validates non-empty values and records mandatory-field errors. Tests were updated to expect and exercise the new missing-last-name validation.

Changes

Cohort / File(s) Summary
Last Name Validation Logic
avni-server-api/src/main/java/org/avni/server/importer/batch/csv/writer/SubjectWriter.java
Replace unconditional last-name assignment with conditional logic for person-type subjects. Add private static setLastName(Row, Individual, List<String>) to validate non-empty last name, record mandatory-field errors when missing, and set the name when valid.
Test Coverage Updates
avni-server-api/src/test/java/org/avni/server/importer/batch/csv/writer/SubjectWriterIntegrationTest.java
Update missingMandatoryCoreValues expected errors to include Last Name. Add missingLastName() test that supplies an empty Last Name and asserts the mandatory-field error is reported.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I nibble bytes and check each name,
A surname gone? I call its name!
For people first, no blanks allowed,
Tests hop in, all proud and loud.
Data neat — a rabbit's vow. 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: validating the Last Name mandatory field in CSV bulk upload, and references the associated issue #968.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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 and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
avni-server-api/src/main/java/org/avni/server/importer/batch/csv/writer/SubjectWriter.java (1)

212-219: Fix indentation inside the if block.

The body of the if statement (lines 215-217) is not indented correctly, making it inconsistent with the setFirstName method and harder to read.

🔧 Proposed fix for indentation
 private static void setLastName(Row row, Individual individual, List<String> allErrorMsgs) {
     String lastName = row.get(SubjectHeadersCreator.lastName);
     if (!StringUtils.hasText(lastName)) {
-    allErrorMsgs.add(String.format("Value required for mandatory field: '%s'", SubjectHeadersCreator.lastName));
-    return;
+        allErrorMsgs.add(String.format("Value required for mandatory field: '%s'", SubjectHeadersCreator.lastName));
+        return;
     }
     individual.setLastName(lastName);
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@avni-server-api/src/main/java/org/avni/server/importer/batch/csv/writer/SubjectWriter.java`
around lines 212 - 219, The if-block in setLastName is misindented; reformat the
block so the statements inside the if are indented consistently with
setFirstName: the lines that add the error to allErrorMsgs and the return should
be indented one level inside the if (i.e. align with the opening brace), leaving
individual.setLastName(lastName) at the method level; locate the method
setLastName and adjust indentation for the lines referencing
SubjectHeadersCreator.lastName, allErrorMsgs.add(...), and return.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In
`@avni-server-api/src/main/java/org/avni/server/importer/batch/csv/writer/SubjectWriter.java`:
- Around line 212-219: The if-block in setLastName is misindented; reformat the
block so the statements inside the if are indented consistently with
setFirstName: the lines that add the error to allErrorMsgs and the return should
be indented one level inside the if (i.e. align with the opening brace), leaving
individual.setLastName(lastName) at the method level; locate the method
setLastName and adjust indentation for the lines referencing
SubjectHeadersCreator.lastName, allErrorMsgs.add(...), and return.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5b1e8a63-af94-4fcb-97b0-553f96c272a8

📥 Commits

Reviewing files that changed from the base of the PR and between b294003 and d6cb1e7.

📒 Files selected for processing (2)
  • avni-server-api/src/main/java/org/avni/server/importer/batch/csv/writer/SubjectWriter.java
  • avni-server-api/src/test/java/org/avni/server/importer/batch/csv/writer/SubjectWriterIntegrationTest.java

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
avni-server-api/src/main/java/org/avni/server/importer/batch/csv/writer/SubjectWriter.java (1)

212-219: Implementation correctly mirrors setFirstName validation pattern.

The validation logic is consistent with other mandatory field checks in this class. The early return on empty value and error message format are appropriate.

Minor style nit: Consider adding a blank line before this method to match the spacing convention between other methods in this file.

,

🧹 Optional: Add blank line for consistency
     individual.setGender(gender);
     }
+
     private static void setLastName(Row row, Individual individual, List<String> allErrorMsgs) {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@avni-server-api/src/main/java/org/avni/server/importer/batch/csv/writer/SubjectWriter.java`
around lines 212 - 219, Add a blank line before the private static method
setLastName(Row row, Individual individual, List<String> allErrorMsgs) so its
spacing matches the other methods in the class (maintain existing implementation
and validation using SubjectHeadersCreator.lastName and allErrorMsgs).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In
`@avni-server-api/src/main/java/org/avni/server/importer/batch/csv/writer/SubjectWriter.java`:
- Around line 212-219: Add a blank line before the private static method
setLastName(Row row, Individual individual, List<String> allErrorMsgs) so its
spacing matches the other methods in the class (maintain existing implementation
and validation using SubjectHeadersCreator.lastName and allErrorMsgs).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fda43d63-8c36-488d-9e56-3771f0a2211c

📥 Commits

Reviewing files that changed from the base of the PR and between d6cb1e7 and b9307d9.

📒 Files selected for processing (1)
  • avni-server-api/src/main/java/org/avni/server/importer/batch/csv/writer/SubjectWriter.java

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.

1 participant