Skip to content

Commit 8bfdf46

Browse files
move serialization to JsonMapper
1 parent c69190d commit 8bfdf46

File tree

3 files changed

+121
-102
lines changed

3 files changed

+121
-102
lines changed

backend/server-connection/src/main/java/org/sonarsource/sonarlint/core/serverconnection/issues/ServerIssue.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,6 @@ protected ServerIssue(@Nullable UUID id, String key, boolean resolved, @Nullable
6161
this.impacts = impacts;
6262
}
6363

64-
/**
65-
* constructor for backward compatibility, after finalization of migration from Xodus to H2 should not be used
66-
* when using with H2 UUID should always be set
67-
*/
68-
protected ServerIssue(String key, boolean resolved, @Nullable IssueStatus resolutionStatus, String ruleKey,
69-
String message, Path filePath, Instant creationDate, @Nullable IssueSeverity userSeverity, RuleType type,
70-
Map<SoftwareQuality, ImpactSeverity> impacts) {
71-
this(null, key, resolved, resolutionStatus, ruleKey, message, filePath, creationDate, userSeverity, type, impacts);
72-
}
73-
7464
@CheckForNull
7565
public UUID getId() {
7666
return id;
@@ -119,11 +109,21 @@ public Map<SoftwareQuality, ImpactSeverity> getImpacts() {
119109
return impacts;
120110
}
121111

112+
public G setId(@Nullable UUID id) {
113+
this.id = id;
114+
return (G) this;
115+
}
116+
122117
public G setKey(String key) {
123118
this.key = key;
124119
return (G) this;
125120
}
126121

122+
public G setResolutionStatus(@Nullable IssueStatus resolutionStatus) {
123+
this.resolutionStatus = resolutionStatus;
124+
return (G) this;
125+
}
126+
127127
public G setRuleKey(String ruleKey) {
128128
this.ruleKey = ruleKey;
129129
return (G) this;

backend/server-connection/src/main/java/org/sonarsource/sonarlint/core/serverconnection/storage/JsonMapper.java

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,25 @@
1919
*/
2020
package org.sonarsource.sonarlint.core.serverconnection.storage;
2121

22+
import com.fasterxml.jackson.core.type.TypeReference;
2223
import com.fasterxml.jackson.databind.ObjectMapper;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.Objects;
27+
import java.util.Set;
28+
import java.util.stream.Collectors;
29+
import javax.annotation.Nullable;
30+
import org.jooq.JSON;
31+
import org.sonarsource.sonarlint.core.commons.ImpactSeverity;
32+
import org.sonarsource.sonarlint.core.commons.SoftwareQuality;
33+
import org.sonarsource.sonarlint.core.commons.api.SonarLanguage;
34+
import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger;
35+
import org.sonarsource.sonarlint.core.serverconnection.issues.ServerDependencyRisk;
2336
import org.sonarsource.sonarlint.core.serverconnection.issues.ServerIssue;
2437
import org.sonarsource.sonarlint.core.serverconnection.issues.ServerTaintIssue;
2538

2639
public class JsonMapper {
27-
40+
private static final SonarLintLogger LOG = SonarLintLogger.get();
2841
private final ObjectMapper objectMapper = new ObjectMapper();
2942

3043
public String serializeImpacts(ServerIssue<?> issue) {
@@ -44,4 +57,94 @@ public String serializeFlows(ServerTaintIssue issue) {
4457
}
4558
}
4659

60+
61+
public static JSON serializeTransitions(@Nullable List<ServerDependencyRisk.Transition> transitions) {
62+
if (transitions == null) {
63+
return null;
64+
}
65+
try {
66+
var objectMapper = new ObjectMapper();
67+
var stringList = transitions.stream().map(Enum::name).toList();
68+
return JSON.valueOf(objectMapper.writeValueAsString(stringList));
69+
} catch (Exception e) {
70+
LOG.error("Failed to serialize transitions {}", transitions, e);
71+
return JSON.valueOf("{}");
72+
}
73+
}
74+
75+
public static Map<SoftwareQuality, ImpactSeverity> deserializeImpacts(@Nullable JSON impactsJson) {
76+
if (impactsJson == null) {
77+
return Map.of();
78+
}
79+
try {
80+
var objectMapper = new ObjectMapper();
81+
var map = objectMapper.readValue(impactsJson.data(), new TypeReference<Map<String, String>>() {
82+
});
83+
return map.entrySet().stream()
84+
.collect(Collectors.toMap(entry -> SoftwareQuality.valueOf(entry.getKey()), entry -> ImpactSeverity.valueOf(entry.getValue())));
85+
} catch (Exception e) {
86+
LOG.error("Failed to deserialize impacts {}", impactsJson.data(), e);
87+
return Map.of();
88+
}
89+
}
90+
91+
public static List<ServerDependencyRisk.Transition> deserializeTransitions(@Nullable JSON json) {
92+
if (json == null) {
93+
return List.of();
94+
}
95+
try {
96+
var objectMapper = new ObjectMapper();
97+
var transitions = objectMapper.readValue(json.data(), new TypeReference<List<String>>() {
98+
});
99+
return transitions.stream()
100+
.map(transition -> {
101+
try {
102+
return ServerDependencyRisk.Transition.valueOf(transition);
103+
} catch (Exception e) {
104+
return null;
105+
}
106+
})
107+
.filter(Objects::nonNull)
108+
.toList();
109+
} catch (Exception e) {
110+
LOG.error("Failed to deserialize transitions {}", json.data(), e);
111+
return List.of();
112+
}
113+
}
114+
115+
public static Set<SonarLanguage> deserializeLanguages(@Nullable JSON json) {
116+
if (json == null) {
117+
return Set.of();
118+
}
119+
try {
120+
var objectMapper = new ObjectMapper();
121+
var languages = objectMapper.readValue(json.data(), new TypeReference<List<String>>() {
122+
});
123+
return languages.stream()
124+
.map(language -> {
125+
try {
126+
return SonarLanguage.valueOf(language);
127+
} catch (Exception e) {
128+
return null;
129+
}
130+
})
131+
.filter(Objects::nonNull)
132+
.collect(Collectors.toUnmodifiableSet());
133+
} catch (Exception e) {
134+
LOG.error("Failed to deserialize enabled languages {}", json.data(), e);
135+
return Set.of();
136+
}
137+
}
138+
139+
@Nullable
140+
public static JSON serializeLanguages(Set<SonarLanguage> enabledLanguages) {
141+
try {
142+
var objectMapper = new ObjectMapper();
143+
var languageNames = enabledLanguages.stream().map(Enum::name).toList();
144+
return JSON.valueOf(objectMapper.writeValueAsString(languageNames));
145+
} catch (Exception e) {
146+
LOG.error("Failed to serialize enabled languages {}", enabledLanguages, e);
147+
}
148+
return null;
149+
}
47150
}

backend/server-connection/src/main/java/org/sonarsource/sonarlint/core/serverconnection/storage/ServerFindingRepository.java

Lines changed: 7 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,23 @@
2626
import java.util.Collection;
2727
import java.util.List;
2828
import java.util.Map;
29-
import java.util.Objects;
3029
import java.util.Optional;
3130
import java.util.Set;
3231
import java.util.UUID;
3332
import java.util.function.Consumer;
34-
import com.fasterxml.jackson.core.type.TypeReference;
3533
import com.fasterxml.jackson.databind.ObjectMapper;
36-
import java.util.stream.Collectors;
37-
import javax.annotation.Nullable;
3834
import org.jooq.Configuration;
3935
import org.jooq.JSON;
4036
import org.jooq.TableField;
4137
import org.sonarsource.sonarlint.core.commons.CleanCodeAttribute;
4238
import org.sonarsource.sonarlint.core.commons.HotspotReviewStatus;
43-
import org.sonarsource.sonarlint.core.commons.ImpactSeverity;
4439
import org.sonarsource.sonarlint.core.commons.IssueSeverity;
4540
import org.sonarsource.sonarlint.core.commons.IssueStatus;
4641
import org.sonarsource.sonarlint.core.commons.RuleType;
47-
import org.sonarsource.sonarlint.core.commons.SoftwareQuality;
4842
import org.sonarsource.sonarlint.core.commons.VulnerabilityProbability;
4943
import org.sonarsource.sonarlint.core.commons.api.SonarLanguage;
5044
import org.sonarsource.sonarlint.core.commons.api.TextRange;
5145
import org.sonarsource.sonarlint.core.commons.api.TextRangeWithHash;
52-
import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger;
5346
import org.sonarsource.sonarlint.core.commons.storage.SonarLintDatabase;
5447
import org.sonarsource.sonarlint.core.commons.storage.model.Tables;
5548
import org.sonarsource.sonarlint.core.commons.storage.model.tables.records.ServerBranchesRecord;
@@ -66,9 +59,13 @@
6659
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.SERVER_FINDINGS;
6760
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.SERVER_BRANCHES;
6861
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.SERVER_DEPENDENCY_RISKS;
62+
import static org.sonarsource.sonarlint.core.serverconnection.storage.JsonMapper.deserializeImpacts;
63+
import static org.sonarsource.sonarlint.core.serverconnection.storage.JsonMapper.deserializeLanguages;
64+
import static org.sonarsource.sonarlint.core.serverconnection.storage.JsonMapper.deserializeTransitions;
65+
import static org.sonarsource.sonarlint.core.serverconnection.storage.JsonMapper.serializeLanguages;
66+
import static org.sonarsource.sonarlint.core.serverconnection.storage.JsonMapper.serializeTransitions;
6967

7068
public class ServerFindingRepository implements ProjectServerIssueStore {
71-
private static final SonarLintLogger LOG = SonarLintLogger.get();
7269

7370
private final SonarLintDatabase database;
7471
private final String connectionId;
@@ -352,14 +349,7 @@ private void upsertBranchMetadata(String branchName,
352349
Instant syncTimestamp, Set<SonarLanguage> enabledLanguages) {
353350

354351
var ldt = LocalDateTime.ofInstant(syncTimestamp, ZoneId.systemDefault());
355-
JSON langsJson = null;
356-
try {
357-
var objectMapper = new ObjectMapper();
358-
var languageNames = enabledLanguages.stream().map(Enum::name).toList();
359-
langsJson = JSON.valueOf(objectMapper.writeValueAsString(languageNames));
360-
} catch (Exception e) {
361-
LOG.error("Failed to serialize enabled languages {}", enabledLanguages, e);
362-
}
352+
var langsJson = serializeLanguages(enabledLanguages);
363353

364354
int updated = database.dsl().update(SERVER_BRANCHES)
365355
.set(tsField, ldt)
@@ -388,81 +378,7 @@ private Set<SonarLanguage> readLanguages(String branchName,
388378
if (rec == null) {
389379
return Set.of();
390380
}
391-
var json = rec.value1();
392-
if (json == null) {
393-
return Set.of();
394-
}
395-
try {
396-
var objectMapper = new ObjectMapper();
397-
var languages = objectMapper.readValue(json.data(), new TypeReference<List<String>>() {
398-
});
399-
return languages.stream()
400-
.map(language -> {
401-
try {
402-
return SonarLanguage.valueOf(language);
403-
} catch (Exception e) {
404-
return null;
405-
}
406-
})
407-
.filter(Objects::nonNull)
408-
.collect(Collectors.toUnmodifiableSet());
409-
} catch (Exception e) {
410-
return Set.of();
411-
}
412-
}
413-
414-
private static JSON serializeTransitions(@Nullable List<ServerDependencyRisk.Transition> transitions) {
415-
if (transitions == null) {
416-
return null;
417-
}
418-
try {
419-
var objectMapper = new ObjectMapper();
420-
var stringList = transitions.stream().map(Enum::name).toList();
421-
return JSON.valueOf(objectMapper.writeValueAsString(stringList));
422-
} catch (Exception e) {
423-
LOG.error("Failed to serialize transitions {}", transitions, e);
424-
return JSON.valueOf("{}");
425-
}
426-
}
427-
428-
private static Map<SoftwareQuality, ImpactSeverity> deserializeImpacts(@Nullable JSON impactsJson) {
429-
if (impactsJson == null) {
430-
return Map.of();
431-
}
432-
try {
433-
var objectMapper = new ObjectMapper();
434-
var map = objectMapper.readValue(impactsJson.data(), new TypeReference<Map<String, String>>() {
435-
});
436-
return map.entrySet().stream()
437-
.collect(Collectors.toMap(entry -> SoftwareQuality.valueOf(entry.getKey()), entry -> ImpactSeverity.valueOf(entry.getValue())));
438-
} catch (Exception e) {
439-
LOG.error("Failed to deserialize impacts {}", impactsJson.data(), e);
440-
return Map.of();
441-
}
442-
}
443-
444-
private static List<ServerDependencyRisk.Transition> deserializeTransitions(@Nullable JSON json) {
445-
if (json == null) {
446-
return List.of();
447-
}
448-
try {
449-
var objectMapper = new ObjectMapper();
450-
var transitions = objectMapper.readValue(json.data(), new TypeReference<List<String>>() {
451-
});
452-
return transitions.stream()
453-
.map(transition -> {
454-
try {
455-
return ServerDependencyRisk.Transition.valueOf(transition);
456-
} catch (Exception e) {
457-
return null;
458-
}
459-
})
460-
.filter(Objects::nonNull)
461-
.toList();
462-
} catch (Exception e) {
463-
LOG.error("Failed to deserialize transitions {}", json.data(), e);
464-
return List.of();
465-
}
381+
return deserializeLanguages(rec.value1());
466382
}
467383

468384
private static ServerIssue<?> adaptIssue(ServerFindingsRecord rec) {

0 commit comments

Comments
 (0)