Skip to content

Conversation

@dineshsuthar123
Copy link

Summary

Fixes a NullPointerException in RoleValidator.validate() when validating a Role with a null role name.

Changes

  • Added null check before trailing-space validation
  • Enabled the previously commented unit test
  • Updated Javadoc to reflect expected validation behavior

Testing

  • Ran RoleValidatorTest

Copilot AI review requested due to automatic review settings January 19, 2026 17:15
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes a NullPointerException that occurred in RoleValidator when validating a Role object with a null role name. The fix adds a null check before attempting to validate for trailing spaces, and updates the Javadoc to accurately reflect the validation behavior (fails validation instead of throwing NPE).

Changes:

  • Added null check in RoleValidator.validate() before trailing-space validation to prevent NPE
  • Enabled previously commented test case that validates null role name behavior
  • Updated Javadoc from "throw NullPointerException" to "fail validation" to reflect correct behavior

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
api/src/main/java/org/openmrs/validator/RoleValidator.java Added null check before trailing space validation and updated Javadoc to reflect proper validation behavior instead of NPE
api/src/test/java/org/openmrs/validator/RoleValidatorTest.java Enabled null role name test case and applied whitespace/formatting cleanup

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


// reject any role that has a leading or trailing space
if (!role.getRole().equals(role.getRole().trim())) {
if (role.getRole() != null && !role.getRole().equals(role.getRole().trim())) {
Copy link

Copilot AI Jan 19, 2026

Choose a reason for hiding this comment

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

Consider storing the result of role.getRole() in a local variable to avoid multiple method calls. While not critical, this would improve readability and marginally improve performance by calling the method only once instead of three times.

Suggested change
if (role.getRole() != null && !role.getRole().equals(role.getRole().trim())) {
String roleName = role.getRole();
if (roleName != null && !roleName.equals(roleName.trim())) {

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

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

Kindly for readability, is it okay to store role.getRole() in a local variable instead of calling it multiple times.

Copy link
Author

Choose a reason for hiding this comment

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

I’ve updated the code to store role.getRole() in a local variable for improved readability.

Copy link
Author

Choose a reason for hiding this comment

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

I’ve applied the readability improvements where the repeated calls occurred and cleaned up the related Javadoc.
If there’s any other specific section you’d like me to update for consistency, please let me know.

@jwnasambu
Copy link
Contributor

@dineshsuthar123 Kindly do you have a Jira issue for this PR? if yes attach the ticket link on this PR.

public boolean supports(Class<?> c) {
return c.equals(Role.class);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we discard these formatting changes?

ValidateUtil.validateFieldLengths(errors, obj.getClass(), "role", "description");
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same applies here and the rest of the files.

@dineshsuthar123
Copy link
Author

@dineshsuthar123 Kindly do you have a Jira issue for this PR? if yes attach the ticket link on this PR.

Thanks for the reminder
I’m creating an OpenMRS ID and will link the Jira ticket shortly.

@sonarqubecloud
Copy link

@Chinmay7070
Copy link
Contributor

Can we make the PR and commit titles same as the ticket title?

@dineshsuthar123
Copy link
Author

Can we make the PR and commit titles same as the ticket title?

Yes, that works

@dineshsuthar123 dineshsuthar123 changed the title TRUNK-XXXX: Fix NPE when validating Role with null name TRUNK-6519: Fix NPE when validating Role with null name Jan 20, 2026
@dineshsuthar123
Copy link
Author

@dineshsuthar123 Kindly do you have a Jira issue for this PR? if yes attach the ticket link on this PR.

Thanks for your patience 🙏
Jira issue created: https://issues.openmrs.org/browse/TRUNK-6519

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.

3 participants