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

Safer type.forName #149

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

yippie
Copy link

@yippie yippie commented Jun 27, 2024

If packaged, Actions and Finalizers use an API that cannot access classes outside of the same package. Salesforce Documentation on the Type.forName warns of this and suggests using an alternative API https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_type.htm#apex_System_Type_forName

This adds new Apex_Class_Namespace fields and uses the more explicit and safer version of Type.ForName. I also included a fallback to make sure no existing functionality is lost.

  • Tests pass
  • Appropriate changes to README are included in PR

@mitchspano
Copy link
Owner

Please sign the CLA so I can review these contributions.

https://github.com/mitchspano/apex-trigger-actions-framework/blob/main/docs/contributing.md

try {
dynamicInstance = Type.forName(className).newInstance();
try {
Copy link
Owner

Choose a reason for hiding this comment

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

Double nested try blocks?

dynamicInstance = Type.forName(className).newInstance();
try {
dynamicInstance = Type.forName(namespace, className).newInstance();
} catch (Exception e) {
Copy link
Owner

Choose a reason for hiding this comment

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

Can we catch an explicit type of exception here?

triggerAction = Type.forName(triggerMetadata.Apex_Class_Name__c)
.newInstance();
String namespace = triggerMetadata.Apex_Class_Namespace__c;
try {
Copy link
Owner

@mitchspano mitchspano Jul 1, 2024

Choose a reason for hiding this comment

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

Double nested try blocks?

String namespace = triggerMetadata.Apex_Class_Namespace__c;
try {
triggerAction = Type.forName(namespace, triggerMetadata.Apex_Class_Name__c).newInstance();
} catch (Exception e) {
Copy link
Owner

Choose a reason for hiding this comment

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

Can we catch an explicit type of exception here?

Copy link
Owner

@mitchspano mitchspano left a comment

Choose a reason for hiding this comment

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

Please add corresponding unit tests within FinalizerHandlerTest and MetadataTriggerHandlerTest which explicitly define the specifications for how the system is supposed to behave in all scenarios including but not limited to:

  • Namespace provided in Apex_Class_Namespace__c
  • Namespace included in Apex_Class_Name__c
  • Namespace provided in both
  • etc.

@yippie
Copy link
Author

yippie commented Jul 1, 2024

@mitchspano I added some unit tests and realized my try/catch was unnecessary so I fixed that. I also think I signed the CLA.

However, I have the awkward situation that I don't think I have any way to test one scenario I wrote code for. The fallback of the namespace being included in the Apex_Class_Name__c field. To test this, I would need a valid Apex class from a different Namespace available to the unit test. In otherwords, the test would need to depend on another, namespaced, package. It couldn't be fully self contained.

Not sure how you would like to handle this. One option is to not handle backward compatability and simply straight require proper usage of the new namespace field so if someone pulls in the lastest changes to Trigger Action Framework they might need to also adjust their configuration of it to not break.

// try shared Namespace
if( actionType == null ) {
// Get the namespace of the current class.
String[] parts = String.valueOf(MetadataTriggerHandler.class).split('\\.', 2);
Copy link
Owner

Choose a reason for hiding this comment

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

This looks a bit odd to me. MetadataTriggerHandler class will always have no namespace in the unlocked package.

Is this intended for when someone uses the Trigger Actions Framework within their own Managed package?

Copy link
Author

Choose a reason for hiding this comment

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

Or namespace unlocked package, but yes, that is exactly the intent here as that is how we are using the framework internally (in Namepsaced packages)

@yippie
Copy link
Author

yippie commented Jul 9, 2024

Fixes #152

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