Skip to content

Commit

Permalink
SCANJLIB-223 Never send null or missing values to the scanner engine
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju committed May 30, 2024
1 parent ede15d9 commit 8ff68ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -118,10 +119,12 @@ private static List<String> split(String value) {

private static String buildJsonProperties(Map<String, String> properties) {
JsonArray propertiesArray = new JsonArray();
properties.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(prop -> {
properties.entrySet().stream()
.filter(prop -> prop.getKey() != null)
.sorted(Map.Entry.comparingByKey()).forEach(prop -> {
JsonObject property = new JsonObject();
property.addProperty("key", prop.getKey());
property.addProperty("value", prop.getValue());
property.addProperty("value", Optional.ofNullable(prop.getValue()).orElse(""));
propertiesArray.add(property);
});
JsonObject jsonObject = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonarsource.scanner.lib.internal;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -63,6 +64,23 @@ void execute() {
any());
}

@Test
void replace_null_values_by_empty_in_json_and_ignore_null_key() {
var scannerEngine = temp.resolve("scanner-engine.jar");

ScannerEngineLauncher launcher = new ScannerEngineLauncher(javaRunner, new CachedFile(scannerEngine, true));

Map<String, String> properties = new HashMap<>();
properties.put("sonar.myProp", null);
properties.put(null, "someValue");
launcher.execute(properties);

verify(javaRunner).execute(
eq(List.of("-jar", scannerEngine.toAbsolutePath().toString())),
eq("{\"scannerProperties\":[{\"key\":\"sonar.myProp\",\"value\":\"\"}]}"),
any());
}

@Test
void tryParse_shouldParseLogMessages() {
ScannerEngineLauncher.tryParse("{\n" +
Expand Down

0 comments on commit 8ff68ee

Please sign in to comment.