Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TRUNK-5721: Fix ObsService.getObs error when Obs.valueComplex null #4599

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gayanW
Copy link
Member

@gayanW gayanW commented Mar 24, 2024

Description of what I changed

When saving a complex observation with ObsService#saveObs, the ComplexObsHandler#saveObs method gets called only if complexData.getData() is non-null.

Concept concept = obs.getConcept();
if (null != concept && concept.isComplex()
&& null != complexData && null != complexData.getData()) {
// save or update complexData object on this obs
// this is done before the database save so that the obs.valueComplex
// can be filled in by the handler.
ComplexObsHandler handler = getHandler(obs);
if (null != handler) {
handler.saveObs(obs);

However, it will still be saved to the database.

Then if we call ObsService#getObs for that same observation, it will throw the following exception, since the ComplexObsHandler#getObs still tries to read that non-existent file.

java.lang.NullPointerException
	at org.openmrs.obs.handler.AbstractHandler.getComplexDataFile(AbstractHandler.java:149)
	at org.openmrs.obs.handler.TextHandler.getObs(TextHandler.java:61)
	at org.openmrs.api.impl.ObsServiceImpl.getObs(ObsServiceImpl.java:270)

public static File getComplexDataFile(Obs obs) {
String[] names = obs.getValueComplex().split("\\|");
String filename = names.length < 2 ? names[0] : names[names.length - 1];

I Included a test case to reproduce the issue originally reported in TRUNK-5721

The actual code refactoring will be done, once the expected behavior is confirmed.

Issue I worked on

https://openmrs.atlassian.net/issues/TRUNK-5721

Checklist: I completed these to help reviewers :)

  • My IDE is configured to follow the code style of this project.

    No? Unsure? -> configure your IDE, format the code and add the changes with git add . && git commit --amend

  • I have added tests to cover my changes. (If you refactored
    existing code that was well tested you do not have to add tests)

    No? -> write tests and add them to this commit git add . && git commit --amend

  • I ran mvn clean package right before creating this pull request and
    added all formatting changes to my commit.

    No? -> execute above command

  • All new and existing tests passed.

    No? -> figure out why and add the fix to your commit. It is your responsibility to make sure your code works.

  • My pull request is based on the latest changes of the master branch.

    No? Unsure? -> execute command git pull --rebase upstream master

@@ -812,6 +813,46 @@ public void getObs_shouldGetObsMatchingGivenObsId() {
assertEquals(5089, obs.getConcept().getId().intValue());
}

/**
* This is a test to verify the behavior reported in TRUNK-5721.
* The test name and the content will be refactored once the expected behavior is known
Copy link
Member

Choose a reason for hiding this comment

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

What do you mean by once the expected behaviour is known?

Copy link
Member Author

@gayanW gayanW Mar 28, 2024

Choose a reason for hiding this comment

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

I’d like to confirm whether we should allow, a complx observation with ComplexData#data null to be saved in the database?

If it is okay to be saved, then I could make the changes, so ObsService#getObs would allow retrieving complex observation with no data files.

Copy link
Member

Choose a reason for hiding this comment

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

I would not find it correct to save complex data without data. 😊

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, thank you.. I'll also make a community post, if there's more things to clarify.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants