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

[apex] ApexCRUDViolation false positive reported #3514

Open
BrunoS4G opened this issue Sep 20, 2021 · 1 comment
Open

[apex] ApexCRUDViolation false positive reported #3514

BrunoS4G opened this issue Sep 20, 2021 · 1 comment
Labels
a:false-positive PMD flags a piece of code that is not problematic

Comments

@BrunoS4G
Copy link

BrunoS4G commented Sep 20, 2021

Affects PMD Version: 6.38

Rule: ApexCRUDViolation

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/latest/pmd_rules_apex_security.html#apexcrudviolation

Description:
PMD is reporting an error when checking if it possible to update it or not and the example in documentation does not resolve it. If you change that line by Case.SObjectType.getDescribe().isUpdateable() which is almost the same PMD does not report the issue.

Code Sample demonstrating the issue:

public with sharing class CaseSafeInitialQueueWorker extends AbstractWorker {

    private List<Case> casesToSetInitialQueue = new List<Case>();
    private Set<String> caseIdsToProcess = new Set<String>();
    @TestVisible
    private Map<String, Group> queueNameByQueueId = new Map<String, Group>([
            SELECT Id, Name
            FROM Group
            WHERE Type = 'Queue'
    ]);

    public override void bulkBeforeInsert() {
        Set<String> caseIds = new Set<String>();
        for (AgentWork agentWork : (List<AgentWork>) super.getNewRecords()) {
            if (agentWork.WorkItemId.getSobjectType() == Case.getSObjectType()) {
                caseIds.add(agentWork.WorkItemId);
            }
        }
        if (!caseIds.isEmpty()) {
            for (Case theCase : [SELECT Id, InitialQueue__c FROM Case WHERE Id IN :caseIds WITH SECURITY_ENFORCED]) {
                if (String.isBlank(theCase.InitialQueue__c)) {
                    this.caseIdsToProcess.add(theCase.Id);
                }
            }
        }
    }

    public override void beforeInsert(SObject so) {
        AgentWork agentWork = (AgentWork) so;
        if (this.caseIdsToProcess.contains(agentWork.WorkItemId)) {
            this.casesToSetInitialQueue.add(
                    new Case(
                            Id = agentWork.WorkItemId
                            , InitialQueue__c = this.queueNameByQueueId.get(agentWork.OriginalQueueId).Name
                    )
            );
        }
    }

    public override void andFinallyBefore() {
        if (!this.casesToSetInitialQueue.isEmpty() && Schema.SObjectType.Case.isUpdateable()) {
            update this.casesToSetInitialQueue;
        }
    }

}

Expected outcome:

PMD reports a violation at line 42, but that's wrong. That's a false positive.

Running PMD through: [Other] IntelliJ using Illuminated Cloud

@BrunoS4G BrunoS4G added the a:false-positive PMD flags a piece of code that is not problematic label Sep 20, 2021
@jonathanwiesel
Copy link
Contributor

According to the docs it would seem that we currently asume the only way to obtain the DescribeSObjectResult from which to collect the CRUD checks from is from the getDescribe() method; however, the sObjectType static member variable also returns it:

Obtaining sObject Describe Results Using Tokens
To access the describe result for an sObject, use one of the following methods:

  • Call the getDescribe method on an sObject token.
  • Use the Schema sObjectType static variable with the name of the sObject. For example, Schema.sObjectType.Lead.

    Schema.DescribeSObjectResult is the data type for an sObject describe result.

    The following example uses the getDescribe method on an sObject token:
    Schema.DescribeSObjectResult dsr = Account.sObjectType.getDescribe();

    The following example uses the Schema sObjectType static member variable:
    Schema.DescribeSObjectResult dsr = Schema.SObjectType.Account;

@adangel adangel changed the title [Apex] ApexCRUDViolation false positive reported [apex] ApexCRUDViolation false positive reported Oct 7, 2021
@jsotuyod jsotuyod added the needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale label Mar 17, 2024
@jsotuyod jsotuyod removed the needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale label Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

No branches or pull requests

3 participants