Skip to content

TRUNK-6651: Make @OpenmrsProfile self-enforcing via @Component + @Conditional#6189

Open
solomonfortune wants to merge 1 commit into
openmrs:masterfrom
solomonfortune:TRUNK-6651
Open

TRUNK-6651: Make @OpenmrsProfile self-enforcing via @Component + @Conditional#6189
solomonfortune wants to merge 1 commit into
openmrs:masterfrom
solomonfortune:TRUNK-6651

Conversation

@solomonfortune

Copy link
Copy Markdown

TRUNK-6651: Make @OpenmrsProfile self-enforcing via @component + @conditional

Description of what I changed

@OpenmrsProfile was previously enforced only by two TypeFilters (OpenmrsProfileIncludeFilter / OpenmrsProfileExcludeFilter) wired into core's <context:component-scan base-package="org.openmrs">. Because TypeFilters are scan-local, a module declaring its own <context:component-scan> over org.openmrs.module.* (with default filters and no OpenMRS-specific filters) would register a @Component @OpenmrsProfile(...) bean even when its profile did not
match — the scan sees @Component and has nothing to exclude it.

To fix this, @OpenmrsProfile is now meta-annotated with:

  • @Component — makes any @OpenmrsProfile-annotated class a scan candidate under default filters, replacing the role of OpenmrsProfileIncludeFilter.
  • @Conditional(OpenmrsProfileCondition.class) — gates bean registration on profile match in every component-scan, @Bean method, and @Import, replacing the role of OpenmrsProfileExcludeFilter.

Specific changes:

  • api/src/main/java/org/openmrs/annotation/OpenmrsProfile.java
    • Added @Component and @Conditional(OpenmrsProfileCondition.class)
      meta-annotations
    • Added a value() attribute (default "") so callers can name the bean as
      they would with @Component("name")
  • api/src/main/java/org/openmrs/annotation/OpenmrsProfileCondition.java (new)
    • New Condition implementation that reuses
      OpenmrsProfileExcludeFilter.matchOpenmrsProfileAttributes(...) so
      platform-version / required-module matching logic stays centralized and
      unchanged
  • api/src/main/resources/applicationContext-service.xml
    • Removed the now-redundant OpenmrsProfileIncludeFilter and
      OpenmrsProfileExcludeFilter entries from the org.openmrs component-scan

Behavioral changes (release-note worthy):

  • A @Component @OpenmrsProfile bean whose profile does not match will no longer load via any component-scan, including module scans that previously lacked the OpenMRS TypeFilters.
  • A class using @OpenmrsProfile purely as a non-bean marker will now be registered as a bean when its profile matches, since @OpenmrsProfile is now a stereotype annotation itself.

Issue I worked on

see https://issues.openmrs.org/browse/TRUNK-6651

Checklist: I completed these to help reviewers :)

  • My IDE is configured to follow the code style of this project.
  • I have added tests to cover my changes.
  • I ran mvn clean package right before creating this pull request and added all formatting changes to my commit.
  • All new and existing tests passed.
  • My pull request is based on the latest changes of the master branch.

@sonarqubecloud

Copy link
Copy Markdown

* stereotype annotation with a non-empty value, the two names must agree; a conflict will cause a
* Spring startup exception.
*
* @since 2.8

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Given that the current project version is 3.0.0-SNAPSHOT, ensure this is the intended version for this feature. If it's not being backported to a 2.x release, it should be
@SInCE 3.0.0.

profile.put("openmrsPlatformVersion", annotationAttributes.getOrDefault("openmrsPlatformVersion", ""));
profile.put("modules", annotationAttributes.getOrDefault("modules", new String[0]));

return new OpenmrsProfileExcludeFilter().matchOpenmrsProfileAttributes(profile);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
return new OpenmrsProfileExcludeFilter().matchOpenmrsProfileAttributes(profile);
return OpenmrsProfileExcludeFilter.matchOpenmrsProfileAttributes(profile);

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