[webservices.rest] Error accessing properties of SimpleObject using key which is a String

Context: (Bahmni team) Using openmrs 1.12-SNAPSHOT, emrapi 1.13-SNAPSHOT, webservices.rest 2.12

Issue: Accessing a property of a SimpleObject (subclass of LinkedHashMap[String, Object]) using the get method throws a java.lang.NoSuchMethodError

Line of Code: https://github.com/openmrs/openmrs-module-emrapi/blob/master/omod/src/main/java/org/openmrs/module/emrapi/rest/resource/PatientProfileResource.java#L72

Exception : (Full Stack trace attached)

[http-nio-8080-exec-4] ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/openmrs].[openmrs]- Servlet.service() for servlet [openmrs] in context with path [/openmrs] threw exception [Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.openmrs.module.webservices.rest.SimpleObject.get(Ljava/lang/String;)Ljava/lang/Object;] with root cause java.lang.NoSuchMethodError: org.openmrs.module.webservices.rest.SimpleObject.get(Ljava/lang/String;)Ljava/lang/Object;

stack_trace.txt (7.2 KB)

Some observations -

  • Exceptions started coming up on December 14, and consistenly happening on all our machines. None of the emrapi code was changed - this was working for us before December 14th, and stopped on that date.
  • Issue is very strange because in general a method that expects an Object can be called with String without explicitly casting as Object, since String is Object
  • If String is cast to Object and the method is called, it works fine.
  • We have tried building older versions, but on any build post Dec 14th, we are facing the issue
  • For now, we have added a new method on SimpleObject (Preethi | Patched up NosuchMethodError thrown in SimpleObject · Bahmni/openmrs-module-webservices.rest@7076026 · GitHub) on a fork, and this seems to be working fine, but we don’t want to be on a fork.

This is normal behavior and caused by the fact that you compiled your emrapi module against a version of the rest web services module that had a SimpleObject.get(Ljava/lang/String;) that expects a string parameter. But at runtime, that method is not found.

So you have two option to fix this:

  1. Either deploy the rest webservices module that has this SimpleObject.get(Ljava/lang/String;) that expects a string parameter.
  2. Or recompile the emrapi module with a version of the rest web services module that does not have this get method which expects a string parameter.

Does this make sense? :smile:

1 Like

And the reason why builds started to fail for you after Dec 14th is that on Dec 13th I upgraded the distro to use webservices.rest 2.13-SNAPSHOT, see https://github.com/openmrs/openmrs-distro-referenceapplication/commit/dedd6a5513f47cf374bbff2613a809785c88a257

Probably we could remove public T get(String key) at https://github.com/openmrs/openmrs-module-webservices.rest/blob/master/omod-common/src/main/java/org/openmrs/module/webservices/rest/SimpleObject.java#L86

I’m not sure why it was added.

I actually like the T get(String key) because it simplifies coding by not requiring me to make some unnecessary casts. So @lluismf i feel you! :smile:

Feel free to remove it, I didn’t expect it to cause backwards incompatibilities. :confused:

It is not causing any backwards incompatibilities. They just happened to compile with it but run without it. :smile:

Thanks a lot for your replies @dkayiwa @raff and @lluismf, it helped understanding the issue at hand.

Unfortunately we cannot move to webservices.rest 2.13-SNAPSHOT just as yet. And as mentioned, @lluismf’s change is fine as it is, we have already forked the omod to add the same method. We will be working towards depending on a version and not a spanshot in the coming weeks.

Thanks again, r.