Skip to content

[apex] False positives with ApexCRUDViolation  #1418

Open
@trentchilders

Description

@trentchilders

Affects PMD Version:
All
Rule:
ApexCrudViolation
Description:
Hello, I have encountered what I believe to be a false positive in the ApexCRUDViolation rule. Specifically the READ portion of that rule (or an isAccessible() check in Apex). I created a helper method so that I could dynamically do this check rather than hardcode every single field that I'm querying for. However, this still causes ApexCRUDViolation to fail its read check.
Code Sample demonstrating the issue:
Helper method:

public with sharing class Utilities {
  public static void checkReadAccess(Map<String,Set<String>> fieldMap){
        Set<String> keySet = fieldMap.keySet();
        
        for(String key : keySet){
            
            Schema.SObjectType objectType = Schema.getGlobalDescribe().get(key);
            
            Schema.DescribeSObjectResult describeSObject = objectType.getDescribe(); 
            if(!describeSObject.isAccessible()){
                throw new System.NoAccessException();
            }
            Map<String,Schema.SObjectField> schemaFieldMap = describeSObject.fields.getMap();
            for(String fieldName : fieldMap.get(key)){
                Schema.DescribeFieldResult describeField = schemaFieldMap.get(fieldName).getDescribe();
                if(!describeField.isAccessible()){
                    throw new System.NoAccessException();
                }
            }
        }
}

And then to call it:

Set<String> fieldSet;
fieldSet = new Set<String>{'Id','Name'};
Map<String,Set<String>> fieldMap;
fieldMap = new Map<String,Set<String>>{'Account'=>fieldSet};
Utilities.checkReadAccess(fieldMap);
List<Account> templateList = [SELECT Id, Name FROM Account];

Running PMD through: Codacy

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions