aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src
diff options
context:
space:
mode:
authorWojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com>2023-01-10 10:25:09 +0100
committersonartech <sonartech@sonarsource.com>2023-01-10 20:03:01 +0000
commit61999c500e5bac37b73f868a954d87710c7f8574 (patch)
treef60a29eb58d20b57356df157cbe8892a80f16c75 /sonar-scanner-engine/src
parent3e48de517be3d38329631f7e0b245f1b7f3659ab (diff)
downloadsonarqube-61999c500e5bac37b73f868a954d87710c7f8574.tar.gz
sonarqube-61999c500e5bac37b73f868a954d87710c7f8574.zip
SONAR-18180 Fix import of SARIF issues with empty range.
Diffstat (limited to 'sonar-scanner-engine/src')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RegionMapper.java18
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RegionMapperTest.java14
2 files changed, 25 insertions, 7 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RegionMapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RegionMapper.java
index c84fca8e3c4..8c55a873a9d 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RegionMapper.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RegionMapper.java
@@ -35,18 +35,22 @@ public class RegionMapper {
return Optional.empty();
}
int startLine = Objects.requireNonNull(region.getStartLine(), "No start line defined for the region.");
- Integer endLine = region.getEndLine();
- if (endLine != null) {
- int startColumn = Optional.ofNullable(region.getStartColumn()).map(RegionMapper::adjustSarifColumnIndexToSqIndex).orElse(0);
- int endColumn = Optional.ofNullable(region.getEndColumn()).map(RegionMapper::adjustSarifColumnIndexToSqIndex)
- .orElseGet(() -> file.selectLine(endLine).end().lineOffset());
- return Optional.of(file.newRange(startLine, startColumn, endLine, endColumn));
- } else {
+ int endLine = Optional.ofNullable(region.getEndLine()).orElse(startLine);
+ int startColumn = Optional.ofNullable(region.getStartColumn()).map(RegionMapper::adjustSarifColumnIndexToSqIndex).orElse(0);
+ int endColumn = Optional.ofNullable(region.getEndColumn()).map(RegionMapper::adjustSarifColumnIndexToSqIndex)
+ .orElseGet(() -> file.selectLine(endLine).end().lineOffset());
+ if (rangeIsEmpty(startLine, endLine, startColumn, endColumn)) {
return Optional.of(file.selectLine(startLine));
+ } else {
+ return Optional.of(file.newRange(startLine, startColumn, endLine, endColumn));
}
}
private static int adjustSarifColumnIndexToSqIndex(int index) {
return index - 1;
}
+
+ private static boolean rangeIsEmpty(int startLine, int endLine, int startColumn, int endColumn) {
+ return startLine == endLine && startColumn == endColumn;
+ }
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RegionMapperTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RegionMapperTest.java
index e480a971143..2b305833b52 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RegionMapperTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RegionMapperTest.java
@@ -159,6 +159,20 @@ public class RegionMapperTest {
assertThat(textRange.end().lineOffset()).isEqualTo(7);
}
+ @Test
+ public void mapRegion_whenRangeIsEmpty_shouldSelectWholeLine() {
+ Region fullRegion = mockRegion(8, 8, 3, 3);
+
+ Optional<TextRange> optTextRange = regionMapper.mapRegion(fullRegion, INPUT_FILE);
+
+ assertThat(optTextRange).isPresent();
+ TextRange textRange = optTextRange.get();
+ assertThat(textRange.start().line()).isEqualTo(fullRegion.getStartLine());
+ assertThat(textRange.start().lineOffset()).isZero();
+ assertThat(textRange.end().line()).isEqualTo(fullRegion.getEndLine());
+ assertThat(textRange.end().lineOffset()).isEqualTo(LINE_END_OFFSET);
+ }
+
private static Region mockRegion(@Nullable Integer startColumn, @Nullable Integer endColumn, @Nullable Integer startLine, @Nullable Integer endLine) {
Region region = mock(Region.class);
when(region.getStartColumn()).thenReturn(startColumn);