Skip to content

Commit 763e9f4

Browse files
WeltraumschafManuelNeuer
authored andcommitted
#36 Use Map instead of MultiValueMap as options type
During the last refactorings we wondered why MultiValueMaps are used for options. The original developer has no clue, why he used it. First we encountered the problem that the value generic type was untyped Object, which is bad due to type confusion bugs. So we changed it to String, since all values are somehow converted to Strings in the end (its HTTP). This resulted in a compile error, when adding these options to the request body because of type erasure problems (Type<String> is not the same as Type<Object>). So we changed the addition of options to the request body by looping over the options and add them explicitly. This change resulted in a runtime error on HTTP request: org.springframework.http.converter.HttpMessageNotWritableException: Could not write request: no suitable HttpMessageConverter found for request type [java.util.ArrayList] The reason for this is that values in a MultiValueMap may be a single value or multiple values (which will be a List of values). Before our change this worked by accident because nobody added multiple values. That's my assumption Since we do not need multiple values under the same key I change the API to a simple Map<String,String>. Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent 51b50a9 commit 763e9f4

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/main/java/io/securecodebox/persistence/defectdojo/service/DefaultImportScanService.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import java.nio.charset.StandardCharsets;
3737
import java.util.List;
38+
import java.util.Map;
3839

3940
/*
4041
* https://defectdojo.security.iteratec.dev/api/v2/oa3/swagger-ui/#operations-tag-import-scan
@@ -62,23 +63,23 @@ class DefaultImportScanService implements ImportScanService {
6263
}
6364

6465
@Override
65-
public ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType, MultiValueMap<String, String> options) {
66-
options.add("engagement", Long.toString(engagementId));
66+
public ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType, Map<String, String> options) {
67+
options.put("engagement", Long.toString(engagementId));
6768

6869
return this.createFindings(scanFile, "import-scan", lead, currentDate, scanType, testType, options);
6970
}
7071

7172
@Override
72-
public ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType, MultiValueMap<String, String> options) {
73-
options.add("test", Long.toString(testId));
73+
public ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType, Map<String, String> options) {
74+
options.put("test", Long.toString(testId));
7475

7576
return this.createFindings(scanFile, "reimport-scan", lead, currentDate, scanType, testType, options);
7677
}
7778

7879
/*
7980
* Before version 1.5.4. testName (in DefectDojo _test_type_) must be defectDojoScanName, afterward, you can have something else.
8081
*/
81-
private ImportScanResponse createFindings(ScanFile scanFile, String endpoint, long lead, String currentDate, ScanType scanType, long testType, MultiValueMap<String, String> options) {
82+
private ImportScanResponse createFindings(ScanFile scanFile, String endpoint, long lead, String currentDate, ScanType scanType, long testType, Map<String, String> options) {
8283
final var headers = createDefectDojoAuthorizationHeaders();
8384
// We use multipart because we send two "parts" in the request body:
8485
// 1. generic info as key=value&key=value...

src/main/java/io/securecodebox/persistence/defectdojo/service/ImportScanService.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
import io.securecodebox.persistence.defectdojo.models.ScanFile;
1212
import lombok.Data;
1313
import lombok.Getter;
14-
import org.springframework.util.LinkedMultiValueMap;
15-
import org.springframework.util.MultiValueMap;
14+
15+
import java.util.HashMap;
16+
import java.util.Map;
1617

1718
/**
1819
* Service to re/import findings into DefectDojo
@@ -29,16 +30,16 @@ static ImportScanService createDefault(final DefectDojoConfig config) {
2930
}
3031

3132
default ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType) {
32-
return this.importScan(scanFile, engagementId, lead, currentDate, scanType, testType, new LinkedMultiValueMap<>());
33+
return this.importScan(scanFile, engagementId, lead, currentDate, scanType, testType, new HashMap<>());
3334
}
3435

35-
ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType, MultiValueMap<String, String> options);
36+
ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType, Map<String, String> options);
3637

3738
default ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType) {
38-
return this.reimportScan(scanFile, testId, lead, currentDate, scanType, testType, new LinkedMultiValueMap<>());
39+
return this.reimportScan(scanFile, testId, lead, currentDate, scanType, testType, new HashMap<>());
3940
}
4041

41-
ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType, MultiValueMap<String, String> options);
42+
ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType, Map<String, String> options);
4243

4344
@Data
4445
class ImportScanResponse {

0 commit comments

Comments
 (0)