Skip to content

#288 - Fix Global Properties loading for OpenMRS 2.7+ #289

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

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ See the [documentation on Initializer's logging properties](readme/rtprops.md#lo

## Releases notes

#### Version 2.9.0
* Fix for InitializerSerializer to ensure compatibility with OpenMRS version 2.7.0+

#### Version 2.8.0
* Ampath forms translation files will now generate checksums.
* Enhancement to ensure that when an Ampath forms file is loaded, a new resource with the existing Ampath forms translations is created.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package org.openmrs.module.initializer.api;

import java.io.InputStream;

import org.openmrs.GlobalProperty;
import org.openmrs.module.idgen.IdentifierSource;
import org.openmrs.module.initializer.api.gp.GlobalPropertiesConfig;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
Expand All @@ -14,12 +8,22 @@
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.mapper.Mapper;
import com.thoughtworks.xstream.mapper.MapperWrapper;
import org.openmrs.GlobalProperty;
import org.openmrs.module.idgen.IdentifierSource;
import org.openmrs.module.initializer.api.gp.GlobalPropertiesConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.lang.reflect.Method;

/**
* Use this serializer instead of a bare {@link XStream} if you want to ignore unmapped fields.
*/
public class InitializerSerializer extends XStream {

private static final Logger log = LoggerFactory.getLogger(InitializerSerializer.class);

public InitializerSerializer() {
super();
}
Expand Down Expand Up @@ -53,6 +57,15 @@ public static XStream getGlobalPropertiesConfigSerializer() {
xs.alias("config", GlobalPropertiesConfig.class);
xs.alias("globalProperty", GlobalProperty.class);
xs.aliasField("value", GlobalProperty.class, "propertyValue");
try {
Method allowTypeHierarchy = XStream.class.getMethod("allowTypeHierarchy", Class.class);
allowTypeHierarchy.invoke(xs, GlobalPropertiesConfig.class);
allowTypeHierarchy.invoke(xs, GlobalProperty.class);
log.debug("Successfully configured global properties config serializer with allowed types");
}
catch (Exception e) {
log.debug("Error configuring global properties config serializer with allowed types", e);
}
Comment on lines +60 to +68
Copy link
Contributor

Choose a reason for hiding this comment

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

This logic seems to imply that it fails on older versions of core. We might not necessarily need a separate InitializerSerializer_2_7, but it could be useful to have some unit test(s) enforcing the logic .

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure what you mean about this logic seems to imply that it fails on older versions of core. I don't think it implies that. But your point about having some unit tests that actually show this failure and prove that this fixes it is totally valid. I copied the test from the api project (which tests using OpenMRS 2.1) into the api-2.7 project (which tests using OpenMRS 2.7). This test fails without my fix above, which is easily testable if you comment out lines 62 and 63 above.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure what you mean about this logic seems to imply that it fails on older versions of core.

@mseaton, sorry for the confusion. I was just wondering why the error is logged instead of actually re-throwing a runtime exception.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I see @Ruhanga . It is logged as it is assumed that any Exception thrown by this block is due to the fact that this method is not present in the Xstream library, which is an expected condition for any platform < 2.7.

Copy link
Contributor

Choose a reason for hiding this comment

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

Alright, makes sense to me. Thanks @mseaton.

return xs;
}

Expand Down