pmd / pmd

An extensible multilanguage static code analyzer.

Home Page:https://pmd.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[apex] SOQL syntax error with TYPEOF in sub-query

YodaDaCoda opened this issue · comments

Affects PMD Version:

v7.0.0

Description:

When a sub-query uses TYPEOF keyword to query related fields from a polymorphic field object, PMD throws a ParseException.

Exception Stacktrace:

> pmd check --dir .\force-app\main\default\classes\TypeofTest.cls --rulesets=.ci/pmd-apex.xml
.\force-app\main\default\classes\TypeofTest.cls	-	ParseException: Parse exception: com.google.summit.SummitAST$ParseException: Failed to parse .\force-app\main\default\classes\TypeofTest.cls: Syntax error at 18:7: missing 'from' at 'WHEN'
Syntax error at 18:18: missing ')' at 'THEN'
Syntax error at 18:23: missing 'from' at 'Name'
Syntax error at 19:12: mismatched input 'User' expecting {'for', 'update', 'with', 'using', 'where', 'order', 'limit', 'group', 'all', 'offset', ']', ','}
Syntax error at 19:22: missing ';' at 'Name'
Syntax error at 21:5: missing ';' at 'FROM'
Syntax error at 22:4: mismatched input ')' expecting {'for', 'update', 'with', 'using', 'where', 'order', 'limit', 'group', 'all', 'offset', ']', ','}
Syntax error at 24:2: extraneous input ']' expecting ';'

Code Sample demonstrating the issue:

public class TypeofTest {
	static void test() {
		Case c = [
			SELECT
				Id,
				Subject,
				Description,
				TYPEOF Owner
					WHEN Group THEN Name
					WHEN User THEN Name
				END,
				(
					SELECT
						Id,
						Subject,
						Description,
						TYPEOF Owner
							WHEN Group THEN Name
							WHEN User THEN Name
						END
					FROM Cases
				)
			FROM Case
		];
	}
}

Steps to reproduce:

Please provide detailed steps for how we can reproduce the bug.

  1. Run PMD against the provided sample class

Running PMD through:
CLI

Additional information:

This appears to be an issue with the apex-parser library v3.6.0 not properly supporting the TYPEOF in sub-queries. The unreleased v4.0.0 does, but summit-ast will need to be updated for the TriggerBlock changes.