Error creating Obs: "converting class org.openmrs.Obs to ...DefaultRepresentation"

Tags: #<Tag:0x00007fe2e19acba0>

Hi everyone,

I am trying to debug an API request.

I’m getting an error when creating an Observation. This is the request being sent to OpenMRS:

POST https://www.example.com/openmrs/ws/rest/v1/obs

{
  "concept": "f09e9b39-d83e-4cdb-b590-f39626f0708b",
  "encounter": "f109efe8-bffb-4eb3-b972-a2e90b1dd3c0",
  "location": "8d6c993e-c2cc-11de-8d13-0010c6dffd0f",
  "obsDatetime": "2019-09-12T11:11:12.941+0000",
  "person": "f287b47a-3827-4c11-a35f-13dc8e783c4b",
  "value": true
}

OpenMRS is responding with status code 400:

{
  "error": {
    "code": "org.openmrs.module.webservices.rest.web.ConversionUtil:409",
    "detail": "org.openmrs.module.webservices.rest.web.response.ConversionException: converting class org.openmrs.Obs to org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation@27507c1
        at org.openmrs.module.webservices.rest.web.ConversionUtil.convertToRepresentation(ConversionUtil.java:409)
        ...",
    "message": "[converting class org.openmrs.Obs to org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation@27507c1]"
  }
}

I took a look at the source code, and based on the data type, org.openmrs.Obs, it seems the Observation has been created successfully before this error occurs. If we check the database, we can confirm that it is being created. We can fetch it using the API. e.g.

GET https://www.example.com/openmrs/ws/rest/v1/obs/409a5817-8980-4f7c-9c93-abca3b641080?v=ref

{
  "uuid": "409a5817-8980-4f7c-9c93-abca3b641080",
  "display": "U2-Abnormal temperature: true",
  "links": [
    {
      "rel": "self",
      "uri": "https://www.example.com/openmrs/ws/rest/v1/obs/409a5817-8980-4f7c-9c93-abca3b641080"
    }
  ]
}

Any idea what is causing the error, and how to avoid it?

Let me know if the full stacktrace would help, or the full details of the Observation that’s created.

Kind regards,

Norman

1 Like

@norman can you post the he full stacktrace in https://pastebin.com/

Though its most likely to be an issue with the dateformat you posted. see https://issues.openmrs.org/browse/RESTWS-644

cc @dkayiwa that Pr needs to get merged

Thanks @mozzy!

Here is the stacktrace: https://pastebin.com/SYPPK2WC

Are you using bahmni?

Hi @dkayiwa, yes, we’re using Bahmni.

Moved to the bahmni category.

Thanks @dkayiwa.

Just so I understand this issue better, and the reason this topic belongs in the Bahmni category, does Bahmni affect REST Web Services? And if so, where can I find out more details about that?

For more context, I can reproduce this error when I send this request from a web browser. But this specific request is part of a series of requests sent by CommCare to create a Visit + an Encounter + a bunch of Observations when a particular form is submitted by a mobile worker to CommCare.

Is it possible that this error is only occurring because Bahmni is installed on the OpenMRS server that this request is being sent to? Could this request succeed if Bahmni was not being used? If this error is related to Bahmni, is it possible to send a request to create this Observation which would work both with and without Bahmni? From a CommCare point of view, it would be great to build a more robust integration that would not need to take Bahmni into consideration when constructing API requests and workflows of requests. But if it must change requests depending on Bahmni, I’d like to know how I can fix this request specifically, but also more generally, what changes CommCare needs to make for all requests to succeed when using Bahmni.

So, if I understand this - this request is not made from “Bahmni” UI as such. To answer your questions briefly,

  • Bahmni uses Webservices-rest and EMR APIs. So anything that works with OpenMRS Platform will work in Bahmni context as well. There are some specific API (course grained APIs) that Bahmni uses over EMR API - but this is clearly not the case you mentioned.
  • the API, the POST request to “/ws/rest/v1/obs” - will be handled by webservices-rest.

Are you sure you are sending all the required attributes for obs creation? Also what version of OpenMRS core and Webservices are you using? For example, from demo.openmrs.org, if you see the API documentation for Obs POST:

{
  "person": "uuid",
  "obsDatetime": "2019-09-16T06:34:59.091Z",
  "concept": "uuid",
  "location": "string",
  "order": "string",
  "encounter": "string",
  "accessionNumber": "string",
  "groupMembers": [
    "string"
  ],
  "valueCodedName": "string",
  "comment": "string",
  "voided": true,
  "value": "string",
  "valueModifier": "string",
  "formFieldPath": "string",
  "formFieldNamespace": "string",
  "status": "PRELIMINARY",
  "interpretation": "NORMAL"
}

Not that all attributes will be required, but some of the additional information might be missing. Can you check “/var/log/openmrs/openmrs.log” for more information.

@norman , On looking at the details of your logs , i can see this ,

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.openmrs.Concept.descriptions, could not initialize proxy - no Session

. There seems to be a problem with the concept you posted. I have just tried out exactly with the same attributes you posted . and it shoud be fine.

  • can we look at the details of the concept you posted ??
  • What version of webservices module are you trying out with?
  • Can you reproduce that error here on the demo server ??

@norman

  • Are you passing the Authentication headers?
  • Can you tell me the exact URL you are posting to? I didn’t see your pastebin earlier - but I can see now that there is a BahmniConceptResource in the stacktrace. However, the URL you posted is not handled by Bahmni APIs.

Hi @angshuonline and @mozzy

Thank you for your responses!

@angshuonline,

So, if I understand this - this request is not made from “Bahmni” UI as such.

That is correct (which is why I was unsure how the problem related to Bahmni).

I should reiterate that the Observation is being created successfully. I posted a “ref” of the Observation in my question (as JSON), and I’ve pasted the full details in Pastebin (as XML) here: https://pastebin.com/8YRG6hDe

So all required attributes have been included, and the authentication headers were set correctly.

@mozzy I think you might be onto something regarding the Concept. Here are the full details (as XML): https://pastebin.com/LDd4UJCf

“Administration” > “API Documentation” gives the API version as “base url: /v1 , api version: 2.1.1”.

I will try to reproduce the error on the demo server.

can you use swagger or post man to get a very smart json format of the concept? like this.

@mozzy the server seems to be offline at the moment. (It belongs to a customer; I don’t manage it.) I’ll post the JSON as soon as I can.

In the meantime, I have learned a bit more from troubleshooting. The Observation that’s giving the problem, its Concept’s data type is boolean. If I use a numerical concept, I don’t get the error. And if I use a different boolean concept, I get the same error. So it seems to be happening with Observations of boolean Concepts only.

I tried the same API call on demo.openmrs.org using the “fatigue” Concept (also a boolean), and that succeeded!

When the server is back online, I will try again using the “fatigue” Concept. I want to establish whether the server fails with a “known good” boolean concept. If it fails, maybe it needs to be updated. If it succeeds, I will look at the difference is between “fatigue” and the Concepts that are failing.

sure, the problem is around the concept ,So after you post the full proper format of that concept , we should be able to figure out the problem

However , when i was trouble shooting on my local server , i tried out with a boolean concept but it was fine,so we should look at the details of that concept, especially the descriptions field , i cant understand why hibernate failed to lazily innitialize it.

@norman what happens when you try it on the bahmni online demo server? https://bahmni.atlassian.net/wiki/spaces/BAH/pages/61997323/Bahmni+Online+Demo

Good idea @dkayiwa … It succeeded.

I used the “Abnormal Temperature” concept, because I had tested with the same concept on the problem server, and it failed. But on the Bahmni demo server it succeeds.

So it seems that the API request is OK. And the “Abnormal Temperature” concept is created by Bahmni, so it should be the same between the two servers.

If it’s not the request, and it’s not the concept … I’m not sure what it could be.

Are you running the same platform and module versions as those installed on the bahmni demo server?

have you tried rebuilding the “index”?

administration / search index / rebuild search index.

Also, how is the other client program communicating with the openmrs/bahmni server? are they passing the authentication headers?

Thanks @angshuonline, we’ll try rebuild the index.

The client program is sending POST requests over HTTPS, using basic authentication, passing credentials in the request header.

The server is using Bahmni 0.90, and the requests succeeded on the Bahmni demo server running Bahmni 0.91. We will try to reproduce the problem on a different server running Bahmni 0.90. We will also verify that it works on Bahmni 0.92.