]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16302 Adhoc description sections are not populated correctly on web api (#5996)
authorLéo Geoffroy <99647462+leo-geoffroy-sonarsource@users.noreply.github.com>
Fri, 27 May 2022 13:56:10 +0000 (15:56 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 27 May 2022 20:03:00 +0000 (20:03 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java

index 5102d4b3490eeebe1b4155f4316caa02ed3c5fde..959d168770589f6bf38357962b8e8a140dd1ac16 100644 (file)
@@ -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<String> fieldsToReturn) {
index a2228b01e008b5e3b5be69026732412586029fe6..7e3f99087c308df54c2d75d18adf2ab6b4003acc 100644 (file)
@@ -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("<div>desc</div>")
-      .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("<div>desc</div>")
+          .setAdHocSeverity(Severity.BLOCKER)
+          .setAdHocType(RuleType.VULNERABILITY)
+          .setNoteData(null)
+          .setNoteUserUuid(null);
+        //Ad-hoc description has no description sections defined
+        r.getRuleDescriptionSectionDtos().clear();
+      }
+    );
+
     doReturn("&lt;div&gt;desc2&lt;/div&gt;").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", "&lt;div&gt;desc2&lt;/div&gt;", Severity.BLOCKER, VULNERABILITY);
+
+    assertThat(resultRule.getDescriptionSectionsList()).hasSize(1);
+    assertThat(resultRule.getDescriptionSectionsList().get(0))
+      .extracting(r -> r.getKey(), r -> r.getContent())
+      .containsExactly(DEFAULT_KEY, "&lt;div&gt;desc2&lt;/div&gt;");
   }
 
   @Test