From: Léo Geoffroy <99647462+leo-geoffroy-sonarsource@users.noreply.github.com> Date: Fri, 27 May 2022 13:56:10 +0000 (+0200) Subject: SONAR-16302 Adhoc description sections are not populated correctly on web api (#5996) X-Git-Tag: 9.5.0.56709~79 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5991e33fa600e1afcf7024cc8d2d4e5c5fc84525;p=sonarqube.git SONAR-16302 Adhoc description sections are not populated correctly on web api (#5996) --- 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