From 5991e33fa600e1afcf7024cc8d2d4e5c5fc84525 Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= <99647462+leo-geoffroy-sonarsource@users.noreply.github.com> Date: Fri, 27 May 2022 15:56:10 +0200 Subject: [PATCH] SONAR-16302 Adhoc description sections are not populated correctly on web api (#5996) --- .../org/sonar/server/rule/ws/RuleMapper.java | 7 +++++ .../sonar/server/rule/ws/ShowActionTest.java | 28 +++++++++++++------ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java index 5102d4b3490..959d1687705 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java @@ -160,6 +160,13 @@ public class RuleMapper { if (adHocDescription != null && shouldReturnField(fieldsToReturn, FIELD_HTML_DESCRIPTION)) { ruleResponse.setHtmlDesc(macroInterpreter.interpret(adHocDescription)); } + + if (shouldReturnField(fieldsToReturn, FIELD_DESCRIPTION_SECTIONS) && adHocDescription != null) { + ruleResponse.clearDescriptionSections(); + ruleResponse.addDescriptionSectionsBuilder() + .setKey(RuleDescriptionSectionDto.DEFAULT_KEY) + .setContent(macroInterpreter.interpret(adHocDescription)); + } } private static void setAdHocSeverity(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set fieldsToReturn) { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java index a2228b01e00..7e3f99087c3 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java @@ -329,15 +329,20 @@ public class ShowActionTest { @Test public void show_adhoc_rule() { - RuleDto externalRule = db.rules().insert(r -> r - .setIsExternal(true) - .setIsAdHoc(true) - .setAdHocName("adhoc name") - .setAdHocDescription("
desc
") - .setAdHocSeverity(Severity.BLOCKER) - .setAdHocType(RuleType.VULNERABILITY) - .setNoteData(null) - .setNoteUserUuid(null)); + RuleDto externalRule = db.rules().insert(r -> { + r.setIsExternal(true) + .setIsAdHoc(true) + .setAdHocName("adhoc name") + .setAdHocDescription("
desc
") + .setAdHocSeverity(Severity.BLOCKER) + .setAdHocType(RuleType.VULNERABILITY) + .setNoteData(null) + .setNoteUserUuid(null); + //Ad-hoc description has no description sections defined + r.getRuleDescriptionSectionDtos().clear(); + } + ); + doReturn("<div>desc2</div>").when(macroInterpreter).interpret(externalRule.getAdHocDescription()); ShowResponse result = ws.newRequest() @@ -348,6 +353,11 @@ public class ShowActionTest { assertThat(resultRule) .extracting(Rule::getName, Rule::getHtmlDesc, Rule::getSeverity, Rule::getType) .containsExactlyInAnyOrder("adhoc name", "<div>desc2</div>", Severity.BLOCKER, VULNERABILITY); + + assertThat(resultRule.getDescriptionSectionsList()).hasSize(1); + assertThat(resultRule.getDescriptionSectionsList().get(0)) + .extracting(r -> r.getKey(), r -> r.getContent()) + .containsExactly(DEFAULT_KEY, "<div>desc2</div>"); } @Test -- 2.39.5