Skip to content

(fix) O3-5607: Prevent NPE when assignTicket payload fields are missing#110

Open
sanks011 wants to merge 2 commits into
openmrs:mainfrom
sanks011:O3-5607-fix-npe-in-assign-ticket
Open

(fix) O3-5607: Prevent NPE when assignTicket payload fields are missing#110
sanks011 wants to merge 2 commits into
openmrs:mainfrom
sanks011:O3-5607-fix-npe-in-assign-ticket

Conversation

@sanks011
Copy link
Copy Markdown

@sanks011 sanks011 commented Apr 12, 2026

Requirements

  • This PR has a title that briefly describes the work done including the ticket number. If there is a ticket, make sure your PR title includes a conventional commit label. See existing PR titles for inspiration.
  • [] My work is based on designs, which are linked or shown either in the Jira ticket or the description below. (N/A - Critical backend logic fix)
  • My work includes tests or is validated by existing tests.

Summary

This PR fixes a critical NullPointerException (HTTP 500 API crash) and completes hardening of the Legacy1xRestController/assignTicketToServicePoint endpoint. Three issues were addressed based on Copilot AI review:

1. NullPointerException on missing JSON keys (original bug)
Jackson's get(property) returns null for missing fields — calling .textValue() or .isEmpty() on null crashed the endpoint.

2. Silent type coercion (Copilot comment 1)
The initial fix used asText("") which would coerce non-string types (e.g. {"status": 123}) into valid strings, violating the API contract. Now using explicit isTextual() + isMissingNode() + isNull() checks per Copilot's suggestion — non-string values are now rejected with 400.

3. Malformed JSON body crash (Copilot comment 2)
mapper.readTree("") throws JsonProcessingException for blank or garbage input, which bubbled up as a 500. Now caught and returned as 400 Bad Request.

4. Unit tests added (Copilot comment 3)
Added Legacy1xRestControllerAssignTicketTest covering 8 scenarios: empty body, invalid JSON, missing fields, explicit null values, non-string types, empty strings, and the valid happy path.

Related Issue

https://issues.openmrs.org/browse/O3-5607

Other

No UI changes. Backend only.

Copilot AI review requested due to automatic review settings April 12, 2026 16:56
Copy link
Copy Markdown

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

Fixes a NullPointerException in the legacy /queueutil/assignticket endpoint when expected JSON fields are missing or null, so the endpoint returns a 400 instead of crashing with a 500.

Changes:

  • Replaced actualObj.get(...).textValue() with actualObj.path(...).asText("") for safer JSON field extraction.
  • Ensures missing/null payload fields fall through to existing required-field validation (isEmpty() -> 400).

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

Comment thread omod/src/main/java/org/openmrs/module/queue/web/Legacy1xRestController.java Outdated
Comment thread omod/src/main/java/org/openmrs/module/queue/web/Legacy1xRestController.java Outdated
Comment thread omod/src/main/java/org/openmrs/module/queue/web/Legacy1xRestController.java Outdated
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.

2 participants